adds minimal support for db migrations
[myplc.git] / plc.d / db
1 #!/bin/bash
2 #
3 # priority: 800
4 #
5 # Bootstrap the database
6 #
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
9 #
10 # $Id: db,v 1.4 2007/01/24 21:05:30 mlhuang Exp $
11 #
12
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
16
17 # Be verbose
18 set -x
19
20 # Export so that we do not have to specify -p to psql invocations
21 export PGPORT=$PLC_DB_PORT
22
23 ### updates the subversion column in plc_db_version by applying
24 ### all migrations scripts in /usr/share/plc_api/migrations/nnn-up-*.sql
25 ### between the current_subversion+1 and the last one
26 ### migration indices must be contiguous
27 function migrate_db () {
28
29     ### check for (or create) the subversion column in plc_db_version
30     psql -U $PLC_DB_USER -c 'select (subversion) from plc_db_version' $PLC_DB_NAME > /dev/null
31     [ "$?" = 0 ] || psql -U $PLC_DB_USER -c 'ALTER TABLE plc_db_version ADD subversion integer DEFAULT 0' $PLC_DB_NAME
32
33     # get current subversion - OUCH, THIS IS UGLY
34     current_subversion=$(psql -U $PLC_DB_USER -c 'select (subversion) from plc_db_version' $PLC_DB_NAME | \
35                          grep '^  *[0-9][0-9]*$')
36     current_subversion=$(($current_subversion))
37     upgrade_index=$current_subversion
38                          
39     while true; do
40         upgrade_index=$(printf "%03d" $(($upgrade_index+1)))
41         upgrade_scripts=$(ls -1 /usr/share/plc_api/migrations/${upgrade_index}-up-*.sql 2> /dev/null)
42         if [ -z "$upgrade_scripts" ] ; then
43             echo "DB current migration is $current_subversion"
44             psql -U $PLC_DB_USER -c "UPDATE plc_db_version SET subversion=$current_subversion" $PLC_DB_NAME
45             break
46         else
47             for script in $upgrade_scripts; do
48                 echo "Applying migration script $script"
49                 psql -U $PLC_DB_USER -f $script $PLC_DB_NAME 
50             done
51             current_subversion=$(($upgrade_index))
52         fi
53     done
54 }
55
56 #### dumps the database for future restoration
57 # uses /root/dumps since /root is mounted on /data/root
58 function dump_db () {
59
60     dump=/root/dumps/$(date +'%Y-%m-%d-%H-%M.sql')
61     mkdir -p $(dirname $dump)
62     pg_dump --user=$PLC_DB_USER $PLC_DB_NAME > $dump
63     
64 }
65
66 function clean_dumps () {
67     find /root/dumps -name '[0-9]*.sql' -atime +15 | xargs rm
68 }
69
70 ##########
71 if [ "$PLC_DB_ENABLED" != "1" ] ; then
72   exit 0
73 fi
74
75 case "$1" in
76     start)
77
78         MESSAGE=$"Bootstrapping the database"
79         dialog "$MESSAGE"
80
81         # Update the maintenance account username. This can't be
82         # done through the api-config script since it uses the
83         # maintenance account to access the API. The maintenance
84         # account should be person_id 1 since it is created by the
85         # DB schema itself.
86         psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME
87
88         # Update the Drupal site_name variable
89         psql -U $PLC_DB_USER drupal <<EOF
90 DELETE FROM variable WHERE name = 'site_name';
91 INSERT INTO variable (name, value) VALUES ('site_name', 's:${#PLC_NAME}:"$PLC_NAME";');
92 EOF
93
94         # Bootstrap the DB
95         db-config
96         check
97
98         # Handle migrations
99         migrate_db
100
101         result "$MESSAGE"
102         ;;
103
104     migrate)
105
106         MESSAGE=$"Migrating the database"
107         dialog "$MESSAGE"
108
109         migrate_db
110         result "$MESSAGE"
111         ;;
112
113     dump)
114
115         MESSAGE=$"Dumping the database"
116         dialog "$MESSAGE"
117
118         dump_db
119         result "$MESSAGE"
120         ;;
121
122     clean-dump)
123
124         MESSAGE=$"Cleaning old database dumps"
125         dialog "$MESSAGE"
126
127         clean_dumps
128         result "$MESSAGE"
129         ;;
130
131     *)
132         echo "Usage: $0 [start|migrate|dump|clean-dump]"
133         exit 1
134         ;;
135
136       
137 esac
138
139 exit $ERRORS