]> err.no Git - scalable-opengroupware.org/blob - Scripts/sql-update-20070822.sh
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1256 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / Scripts / sql-update-20070822.sh
1 #!/bin/bash
2
3 # this script only work with PostgreSQL
4
5 defaultusername=$USER
6 defaulthostname=localhost
7 defaultdatabase=$USER
8 indextable=sogo_folder_info
9
10 read -p "Username ($defaultusername): " username
11 read -p "Hostname ($defaulthostname): " hostname
12 read -p "Database ($defaultdatabase): " database
13
14 if [ -z "$username" ]
15 then
16   username=$defaultusername
17 fi
18 if [ -z "$hostname" ]
19 then
20   hostname=$defaulthostname
21 fi
22 if [ -z "$database" ]
23 then
24   database=$defaultdatabase
25 fi
26
27 echo ""
28 echo "You will now be requested your password twice..."
29 echo "After that, a list of SQL operations will scroll."
30 echo ""
31
32 sqlscript=""
33
34 function addField() {
35     oldIFS="$IFS"
36     IFS=" "
37     part="`echo -e \"ALTER TABLE $table ADD COLUMN c_deleted INTEGER;\\n\"`";
38     sqlscript="$sqlscript$part"
39     IFS="$oldIFS"
40 }
41
42 tables=`psql -t -U $username -h $hostname $database -c "select split_part(c_location, '/', 5) from $indextable where c_folder_type != 'Container';"`
43
44 for table in $tables;
45 do
46   addField
47 done
48
49 sqlscript="$sqlscript;update $indextable set c_path4 = 'personal', c_path = '/Users/' || c_path2 || '/Calendar/personal' where c_path3 = 'Calendar' and c_path4 is null;"
50
51 function updateCalendarLocation() {
52     oldIFS="$IFS"
53     IFS=" "
54     user="`echo $table | cut -f 1 -d :`"
55     tablename="`echo $table | cut -f 2 -d :`"
56     newstart="/$user/Calendar/personal";
57     part="update $tablename set c_object = replace(c_object, '/$user/Calendar', '$newstart') where c_object not like '$newstart%';";
58     sqlscript="$sqlscript$part"
59     IFS="$oldIFS"
60 }
61
62 tables=`psql -t -U $username -h $hostname $database -c "select c_path2 || ':' || split_part(c_acl_location, '/', 5) from $indextable where c_folder_type = 'Appointment';"`
63 for table in $tables;
64 do
65   updateCalendarLocation
66 done
67
68 echo "$sqlscript" | psql -q -e -U $username -h $hostname $database > /dev/null
69
70 echo "Please ignore the errors above. They just mean that the migration was already done for the elements in question.";