X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=plc.d%2Fdb;h=5bf47848b089d2720b3994bf8228ee9c09fde9ec;hb=c6d0216067f98de62a6590363a120d635ae34daf;hp=0286f6137435e7a4332f18d7f83371c7d37476ad;hpb=d3499039761242277a325a290ded0ef3164d157d;p=myplc.git diff --git a/plc.d/db b/plc.d/db index 0286f61..5bf4784 100755 --- a/plc.d/db +++ b/plc.d/db @@ -7,7 +7,7 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id: api,v 1.3 2006/04/25 21:18:19 mlhuang Exp $ +# $Id: db,v 1.7 2007/01/31 19:53:20 mlhuang Exp $ # # Source function library and configuration @@ -20,15 +20,61 @@ set -x # Export so that we do not have to specify -p to psql invocations export PGPORT=$PLC_DB_PORT -case "$1" in - start) - if [ "$PLC_API_ENABLED" != "1" ] ; then - exit 0 +# Updates the database by applying all migration scripts in +# /usr/share/plc_api/migrations/N-up-*, where N is greater than the +# current subversion. At least one of the migration scripts with the +# same N must update plc_db_version.subversion. +function migrate_db() +{ + subversion=$(psql -U $PLC_DB_USER --quiet --tuples-only --no-align -c \ + "SELECT subversion FROM plc_db_version LIMIT 1" \ + $PLC_DB_NAME 2>/dev/null || echo 0) + shopt -s nullglob + for file in /usr/share/plc_api/migrations/[0-9]*-up-* ; do + script=$(basename $file) + index=${script%-up*} + extension=${script##*.} + if [ $index -gt $subversion ] ; then + if [ "$extension" = "sql" ] ; then + psql -U $PLC_DB_USER -f $file $PLC_DB_NAME + elif [ -x $file ] ; then + $file + fi + check fi + done +} + +# Dumps the database +function dump_db() +{ + dump=/var/lib/pgsql/backups/$(date +"$PLC_DB_NAME.%Y-%m-%d-%H-%M.sql") + pg_dump -U $PLC_DB_USER $PLC_DB_NAME > $dump + check + dump=/var/lib/pgsql/backups/$(date +"drupal.%Y-%m-%d-%H-%M.sql") + pg_dump -U $PLC_DB_USER drupal > $dump + check +} + +# Clean up old backups +function clean_dumps() +{ + find /var/lib/pgsql/backups '(' -name "$PLC_DB_NAME.*.sql" -o -name "drupal.*.sql" ')' -a -atime +15 | xargs rm -f + check +} +if [ "$PLC_DB_ENABLED" != "1" ] ; then + exit 0 +fi + +case "$1" in + start) MESSAGE=$"Bootstrapping the database" dialog "$MESSAGE" + # Apply schema updates + migrate_db + # Update the maintenance account username. This can't be # done through the api-config script since it uses the # maintenance account to access the API. The maintenance @@ -36,12 +82,47 @@ case "$1" in # DB schema itself. psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME + # Update the Drupal site_name variable + psql -U $PLC_DB_USER drupal <