This file is indexed.

/usr/bin/update_beancounter is in beancounter 0.8.10.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#! /bin/bash -e
#
#   update_beancounter --- Modify beancounter database
#  
#   Copyright (C) 2000 - 2006  Dirk Eddelbuettel <edd@debian.org>
#  
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#  
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#  
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#   $Id: update_beancounter,v 1.16 2006/03/22 04:15:45 edd Exp $

# If we really want to be independent of the underlying database, this 
# probably needs to written in Perl using DBI/DBD
#
# This once started as being PostgreSQL specific, and now also supports MySQL

progname=$(basename $0)

if [ "$USER" = "" ]
then
    USER=$(whoami)
fi

VERSION='0.8.10'
DB_SCHEMA='0.6.0'
#DATABASE='testbean'
DATABASE='beancounter'
#PASSWORD=
#DBSYSTEM='MySQL'
DBSYSTEM='PostgreSQL'

# -------------------------- Functions ----------------------------------
function usage_and_exit
{
    cat<<EOF
Update beancounter tables
Usage: $progname [-m] [-n databasename] [-h] 
Options:
    -m		Use MySQL instead of PostgreSQL
    -n SOMENAME Create database as SOMENAME instead of beancounter
    -h		Show this help
EOF
    exit 0
}

function set_version_to_current
{
    query="update beancounter set version = '$DB_SCHEMA'"
    echo $query | $DBCOMMAND 
}

# test if symbols are numeric (finance-yahooquote cannot retrieve
# symbols like 555750.DE but deals fine with the more mnemonic symbols_
function check_for_numeric_symbols
{
    rc=0
    query="select distinct symbol from stockinfo where active"
    echo $query | $DBCOMMAND -t | grep -q "\b[[:digit:]]"
    rc=$?

    if [ "$rc" -eq 0 ]; then
	cat <<EOF

Numeric symbols in database:
  Your database appears to have numeric symbols. Beancounter has switched
  to using Finance::YahooQuote as its backend for quote retrieval, and
  Finance::YahooQuote may experience problems with stock symbols such as
  '555750.F' (for Deutsche Telekom in Frankfurt, Germany) whereas the
  equivalent 'DTEGN.F' is fully supported.

  In order to use beancounter, you may need to convert the numeric symbols.
  A helper script 'symbol_alias.sh' is included with Finance::YahooQuote;
  it will find the mnemonic alias for a given numeric symbol. The script
  'flip_symbol.sh' included with Beancounter can then be used to convert
  the beancounter database from a given (numeric) symbol to a given textual
  symbol.

EOF
    fi
    return 0
}

# add 'active' column to stockinfo
function add_beancounter_table_if_needed
{
    rc=0
    table=beancounter

    set +e
    if [ "$DBSYSTEM" = "MySQL" ]; then
	query="show tables"
    else
	query="\d"
    fi

    echo $query | $DBCOMMAND | grep -q $table
    rc=$?

    if [ "$rc" -ne 0 ]; then
        if [ "$DBSYSTEM" = "MySQL" ]; then
	    cmd="create table $table (version varchar(12) not null, data_last_updated datetime)"
	else
	    cmd="create table $table (version varchar(12) not null, data_last_updated timestamp with time zone)"
	fi
	echo "Running $cmd"
	echo $cmd | $DBCOMMAND

	cmd="insert into beancounter values('$VERSION','now')"
	echo "Running $cmd"
	echo $cmd | $DBCOMMAND
    fi
    set -e

    return 0
}

# add 'active' column to stockinfo
function add_active_if_needed
{
    rc=0
    table=stockinfo
    column=active

    set +e
    if [ "$DBSYSTEM" = "MySQL" ]; then
	query="show columns from $table"
    else
	query="\d $table"
    fi

    echo $query | $DBCOMMAND | grep -q $column
    rc=$?

    if [ "$rc" -ne 0 ]; then
	if [ "$DBSYSTEM" = "MySQL" ]; then
	    cmd="alter table $table add column active bool default '1'"
	    echo $cmd | $DBCOMMAND
	    echo "Running $cmd"
	else
	    cmd="alter table $table add column active boolean"
	    echo $cmd | $DBCOMMAND
	    echo "Running $cmd"
	    cmd="alter table $table alter column active set default true"
	    echo $cmd | $DBCOMMAND
	    echo "Running $cmd"
	    cmd="update table $table set active = true where active is null"
	    echo $cmd | $DBCOMMAND
	    echo "Running $cmd"
	fi
    fi
    set -e

    return 0
}

# test if given column (argument 1) exists in a given table (argument 2)
# and add it with given specs (argument 3) if it does not already exist
function add_unless_exists_column
{
    column=$1
    table=$2
    spec=$3
    rc=0

    set +e
    if [ "$DBSYSTEM" = "MySQL" ]; then
	query="show columns from $table"
    else
	query="\d $table"
    fi
    ##echo "Query $query -> $DBCOMMAND"
    echo $query | $DBCOMMAND | grep -q $column
    rc=$?

    if [ "$rc" -ne 0 ]; then
	cmd="alter table $table add $column $spec;"
	echo $cmd | $DBCOMMAND
	## echo "Running $cmd"
    fi
    set -e

    return 0
}

function reindex_portfolio_table
{
   if [ "$DBSYSTEM" = "MySQL" ]; then
	cmd="drop index portfolio_pkey on portfolio; create unique index portfolio_pkey on portfolio (symbol, owner, date);"
    else
	cmd="drop index portfolio_pkey; create unique index portfolio_pkey on portfolio (symbol, owner, date);"
    fi
    echo $cmd | $DBCOMMAND

    return 0
}

# test if given column (argument 1) exists in a given table (argument 2)
# and if so rename it to new name (argument 3) and spec (arg 4; mysql only)
function rename_if_not_exists_column
{
    oldname=$1
    table=$2
    column=$3
    spec=$4
    rc=0

    set +e
    if [ "$DBSYSTEM" = "MySQL" ]; then
	query="show columns from $table"
    else
	query="\d $table"
    fi

    echo $query | $DBCOMMAND | grep -q "$column"
    rc=$?

    if [ "$rc" -ne 0 ]; then
	if [ "$DBSYSTEM" = "MySQL" ]; then
	    # nothing to do as we only added this for 0.4.0
	    true
	else    
	    cmd="alter table $table rename $oldname to $column;"
	    echo $cmd | $DBCOMMAND
	    echo "Running $cmd"
	fi    
    fi
    set -e

    return 0
}

# -------------------------- Main ---------------------------------------

while getopts ":mn:h" opt
do
    case $opt in
	m)
	    DBSYSTEM='MySQL'
	    #echo "Now using $DBSYSTEM"
	    ;;
	n)
	    DATABASE=$OPTARG
	    #echo "Now using database name $DATABASE"
	    ;;
	h)
	    usage_and_exit
	    ;;
	?)
	    echo "Ignoring unknown argument, try '$progname -h' for help."
	    ;;
    esac
done	    
echo "Examining database $DATABASE on $DBSYSTEM"

if [ "$DBSYSTEM" = "MySQL" ]
then
    # mysql(1) arguments -- you could add host, port, ... here
    if [ -z "$PASSWORD" ]
    then
	DBCOMMAND="mysql $DATABASE"
    else
	DBCOMMAND="mysql -p$PASSWORD $DATABASE"
    fi
else
    if [ -z "$PASSWORD" ]
    then
	DBCOMMAND="psql -q $DATABASE"
    else
	DBCOMMAND="psql -q -W $PASSWORD $DATABASE"
    fi
fi

add_unless_exists_column type  portfolio "varchar(16) default null"
add_unless_exists_column owner portfolio "varchar(16) default null"
add_unless_exists_column cost  portfolio "float default null"
add_unless_exists_column date  portfolio "date  default null"

reindex_portfolio_table

rename_if_not_exists_column change stockprices day_change "float default null"
rename_if_not_exists_column change fxprices day_change "float default null"
rename_if_not_exists_column index indices stockindex 'varchar(12) not null default ""'

add_active_if_needed

add_beancounter_table_if_needed

set_version_to_current

check_for_numeric_symbols

echo "Done."
exit 0

=head1 NAME

update_beancounter - Convert older BeanCounter databases

=head1 SYNOPSIS

update_beancounter [-m] [-n NAME] [-h] 

=head1 DESCRIPTION

B<update_beancounter> converts the databases used by 
B<beancounter> from an older release to the current one.

=head1 OPTIONS

 -m       Use MySQL as the backend over the default PostgreSQL
 -s name  Use name as the database instead of B<beancounter
 -h       Show a simple help message

=head1 SEE ALSO

B<beancounter>(1), B<BeanCounter>(1), B<setup_beancounter>(1)

=head1 AUTHOR

Dirk Eddelbuettel edd@debian.org

=cut