4 BASENAME=$(basename $COMMAND .sh)
7 DIRNAME=$(cd ${DIRNAME}; /bin/pwd)
9 MIGRATION_SED=$DIRNAME/${BASENAME}.sed
10 MIGRATION_SQL=$DIRNAME/${BASENAME}.sql
12 SCHEMA_SQL=$(dirname $DIRNAME)/planetlab5.sql
14 DATE=$(date +%Y-%m-%d-%H-%M)
15 DATE_=$(date +%Y_%m_%d_%H_%M)
16 LOG=${DIRNAME}/${DATE}.log
17 DUMP=${DIRNAME}/${DATE}-pl4.sql
18 RESTORE=${DIRNAME}/${DATE}-pl5.sql
19 FAKE=${DIRNAME}/input-pl4.sql
20 VIEWS_SQL=$DIRNAME/${DATE}-views5.sql
23 . /etc/planetlab/plc_config
25 # return 0 (yes) or 1 (no) whether the database exists
26 function check_for_database () {
28 psql --user=$PLC_DB_USER --quiet -c "SELECT subversion from plc_db_version LIMIT 1" $dbname > /dev/null 2>&1
32 # when 'service plc start' gets run, the planetlab5 DB gets created
33 # so this script will drop the planetlab5 DB and re-create it from scratch
34 # with the contents of the planetlab4 DB that is epxected to exist
37 echo "This script is designed to ease the migration from myplc 4.2 to 5.0"
38 echo "You can run it before of after you install a 5.0 myplc"
39 echo "It will attempt to re-create the planetlab5 database from planetlab4"
40 echo "The planetlab5 database is renamed, not dropped, if it is found on the system"
41 echo -n "Are you sure you want to proceed y/[n] ? "
44 y|Y) echo See log in $LOG ;;
45 *) echo "Bye" ; exit 1 ;;
50 [ -f $MIGRATION_SED ] || { echo $MIGRATION_SED not found - exiting ; exit 1; }
51 [ -f $MIGRATION_SQL ] || { echo $MIGRATION_SQL not found - exiting ; exit 1; }
52 [ -f $SCHEMA_SQL ] || { echo $SCHEMA_SQL not found - exiting ; exit 1; }
58 if [ -n "$DEBUG" ] ; then
59 echo -n "Type enter to run next step XXX $message XXX ... "
64 echo "==================================================" >> $LOG
75 # check that planetlab4 exists
76 if check_for_database planetlab4 ; then
77 echo "OK : FOUND db planetlab4"
79 echo "ERROR : planetlab4 NOT FOUND - bye"
83 # check if planetlab5 exists
84 if check_for_database planetlab5 ; then
85 rename=planetlab5_${DATE_}
86 echo -n "There is an existing DB named planetlab5, drop or rename into $rename d/[r] ? "
90 run "Dropping planetlab5" psql --user=postgres template1 -c "DROP DATABASE planetlab5" || true
93 if check_for_database $rename ; then
94 echo "$rename already exists - exiting"
97 run "Renaming planetlab5 into $rename" \
98 psql --user=postgres template1 -c "ALTER DATABASE planetlab5 RENAME TO $rename"
104 # again: now it should not exist
105 if check_for_database planetlab5 ; then
106 echo "ERROR : FOUND planetlab5 - should not happen - exiting"
109 echo "OK, we're clear, let's go"
114 if [ ! -f $FAKE ] ; then
115 run "Dumping planetlab4 in $DUMP" pg_dump --user=$PLC_DB_USER -f $DUMP planetlab4
118 echo 'xxxx WARNING WARNING WARNING WARNING WARNING xxx'
120 echo Using fake input for tests $FAKE
122 echo 'xxxx WARNING WARNING WARNING WARNING WARNING xxx'
127 run "Copying into $RESTORE" cp $DUMP $RESTORE
128 run "Renaming identifiers in $RESTORE" sed -f $MIGRATION_SED -i $RESTORE
130 run "Creating planetlab5 database" createdb --user=postgres --encoding=UNICODE --owner=$PLC_DB_USER planetlab5
131 run "Loading language plpgsql" createlang -U postgres plpgsql planetlab5 || true
132 run "Populating planetlab5 from $RESTORE" psql --user=postgres -f $RESTORE planetlab5
133 run "Fine-tuning it with $MIGRATION_SQL" psql --user=$PLC_DB_USER -f $MIGRATION_SQL planetlab5
134 run "Extracting views definitions from $SCHEMA_SQL" ./extract-views.py $SCHEMA_SQL $VIEWS_SQL
135 run "Inserting views definitions in planetlab5" \
136 psql --user=$PLC_DB_USER -f $VIEWS_SQL planetlab5
139 function manage_link () {
143 echo "Managing link $dest"
150 result=${DIRNAME}/${DATE}-output.sql
151 run "Dumping result in $result" pg_dump --user=$PLC_DB_USER -f $result planetlab5
153 manage_link latest.log $LOG
154 manage_link latest-pl4.sql $DUMP
155 manage_link latest-pl5.sql $RESTORE
156 manage_link latest-views5.sql $VIEWS_SQL
157 manage_link latest-output.sql $result