3 # based on example code from
4 # http://blog.larik.nl:80/articles/2006/03/13/upgrade-your-postgresql-databases-to-unicode
5 # by frodo larik / blog.larik.nl
9 # This script updates all dbs to use unicode
13 #odir=${HOME}/freeside_unicode_upgrade
14 odir=/home/ivan/FREESIDE_unicode_upgrade
16 if [ "${db}X" == "X" ]
18 echo "I need a db for host ${dbhost} and username ${username} $db"
24 mkdir $odir || exit "Exit at mkdir"
27 #echo -n "Enter a comma-separated list of country codes to keep [US,CA]:"
29 #if [ "${countries}X" == "X" ]
34 echo "delete from cust_main_county where 0 = ( select count(*) from cust_main where cust_main_county.country = cust_main.country );" | su freeside -c 'psql freeside'
36 dump_sql=${odir}/${db}_out.sql
37 conv_sql=${odir}/${db}_conv.sql
38 result_sql=${odir}/${db}_result.txt
39 sql_diff=${odir}/${db}.diff
43 /etc/init.d/freeside stop || die "can't stop freeside"
44 /etc/init.d/apache stop || die "can't stop apache"
45 /etc/init.d/apache2 stop || die "can't stop apache"
47 echo "Dumping $db database to $dump_sql"
49 su $username -c "pg_dump --host=$dbhost --username=$username -D --format=p $db" >$dump_sql || exit "exit at pg_dump"
51 echo "Removing invalid characters from the dump"
53 iconv -c -f UTF-8 -t UTF-8 ${dump_sql} > ${conv_sql} || exit "exit at iconv"
55 echo "*** Making a diff from the dump: check $sql_diff ***"
57 diff $dump_sql $conv_sql > $sql_diff
59 echo "Removing current database"
61 su $freeside -c "dropdb --host=$dbhost --username=$username $db" || exit "exit at dropdb"
63 echo "Creating a new databse"
65 su freeside -c "createdb --encoding='unicode' --host=$dbhost --username=$username $db" || exit "exit at createdb"
67 echo "Loading data into new database"
68 su freeside -c "psql -f $conv_sql -o $result_sql -h $dbhost -U $username $db" || exit "exit at psql ${extra_string}"
71 /etc/init.d/freeside start || die "oh no, can't start freeside"
72 /etc/init.d/apache start || die "oh no, can't start apache"