From: Thierry Parmentelat Date: Fri, 25 Nov 2022 17:41:24 +0000 (+0100) Subject: formatted the 'db' script using vscode X-Git-Url: http://git.onelab.eu/?p=plcapi.git;a=commitdiff_plain;h=c3fc031363ac794e6b1245c6ed1a05329cba69c9 formatted the 'db' script using vscode --- diff --git a/plc.d/db b/plc.d/db index 3b84b59..d3af6da 100755 --- a/plc.d/db +++ b/plc.d/db @@ -19,143 +19,134 @@ set -x export PGPORT=$PLC_DB_PORT # Install extensions -function extend_db() -{ - shopt -s nullglob - for file in /usr/share/plc_api/extensions/*-up*; do - script=${file##*/} - name=${script%-up*} - extension=${script##*.} - version=$(psql -U $PLC_DB_USER --quiet --tuples-only --no-align -c \ - "SELECT version FROM plc_db_extensions WHERE name='$name' LIMIT 1" \ - $PLC_DB_NAME 2>/dev/null | awk 'BEGIN { ver=0 } /^[0-9]+$/ { ver=$1 } END { print ver }') - if [ $version -eq 0 ]; then - if [ "$extension" = "sql" ] ; then - dialog " - $script (dbdumped)" - dump_planetlab_db "before-$script" - psql -U $PLC_DB_USER -f $file $PLC_DB_NAME - elif [ -x $file ] ; then - dialog " - $script (dbdumped)" - dump_planetlab_db "before-$script" - $file - else - dialog "\nWarning: extension $file not executable" - fi - check - fi - for file in /usr/share/plc_api/extensions/$name/migrations/[0-9]*-up-*; do - script=${file##*/} - index=${script%-up-*} - extension=${script##*.} - if [ $index -gt $version ] ; then - if [ "$extension" = "sql" ] ; then - dialog " - $script (dbdumped)" - dump_planetlab_db "before-$script" - psql -U $PLC_DB_USER -f $file $PLC_DB_NAME - elif [ -x $file ] ; then - dialog " - $script (dbdumped)" - dump_planetlab_db "before-$script" - $file - else - dialog "\nWarning: migration $file not executable" +function extend_db() { + shopt -s nullglob + for file in /usr/share/plc_api/extensions/*-up*; do + script=${file##*/} + name=${script%-up*} + extension=${script##*.} + version=$(psql -U $PLC_DB_USER --quiet --tuples-only --no-align -c \ + "SELECT version FROM plc_db_extensions WHERE name='$name' LIMIT 1" \ + $PLC_DB_NAME 2>/dev/null | awk 'BEGIN { ver=0 } /^[0-9]+$/ { ver=$1 } END { print ver }') + if [ $version -eq 0 ]; then + if [ "$extension" = "sql" ]; then + dialog " - $script (dbdumped)" + dump_planetlab_db "before-$script" + psql -U $PLC_DB_USER -f $file $PLC_DB_NAME + elif [ -x $file ]; then + dialog " - $script (dbdumped)" + dump_planetlab_db "before-$script" + $file + else + dialog "\nWarning: extension $file not executable" + fi + check fi - check - fi + for file in /usr/share/plc_api/extensions/$name/migrations/[0-9]*-up-*; do + script=${file##*/} + index=${script%-up-*} + extension=${script##*.} + if [ $index -gt $version ]; then + if [ "$extension" = "sql" ]; then + dialog " - $script (dbdumped)" + dump_planetlab_db "before-$script" + psql -U $PLC_DB_USER -f $file $PLC_DB_NAME + elif [ -x $file ]; then + dialog " - $script (dbdumped)" + dump_planetlab_db "before-$script" + $file + else + dialog "\nWarning: migration $file not executable" + fi + check + fi + done done - done } # 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 - dialog " - $script (dbdumped)" - dump_planetlab_db "before-$script" - psql -U $PLC_DB_USER -f $file $PLC_DB_NAME - elif [ -x $file ] ; then - dialog " - $script (dbdumped)" - dump_planetlab_db "before-$script" - $file - else - dialog "\nWarning: migration $file not executable" - fi - check - fi - done +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 + dialog " - $script (dbdumped)" + dump_planetlab_db "before-$script" + psql -U $PLC_DB_USER -f $file $PLC_DB_NAME + elif [ -x $file ]; then + dialog " - $script (dbdumped)" + dump_planetlab_db "before-$script" + $file + else + dialog "\nWarning: migration $file not executable" + fi + check + fi + done } -function checkpoint_planetlab_db() -{ - dumpfile=$1 - pg_dump -U $PLC_DB_USER $PLC_DB_NAME > $dumpfile - check +function checkpoint_planetlab_db() { + dumpfile=$1 + pg_dump -U $PLC_DB_USER $PLC_DB_NAME >$dumpfile + check } -function restore_planetlab_db() -{ - dumpfile=$1 - if [ -n "$dumpfile" ] ; then - [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER $PLC_DB_NAME < $dumpfile - check - fi +function restore_planetlab_db() { + dumpfile=$1 + if [ -n "$dumpfile" ]; then + [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER $PLC_DB_NAME <$dumpfile + check + fi } # use a single date of this script invocation for the dump_*_db functions. DATE=$(date +"%Y-%m-%d-%H-%M-%S") # Dumps the database - optional argument to specify filename suffix -function dump_planetlab_db() -{ - if [ -n "$1" ] ; then suffix="-$1" ; else suffix="" ; fi - dumpfile=/var/lib/pgsql/backups/$(date +"${PLC_DB_NAME}.${DATE}${suffix}.sql") - checkpoint_planetlab_db $dumpfile +function dump_planetlab_db() { + if [ -n "$1" ]; then suffix="-$1"; else suffix=""; fi + dumpfile=/var/lib/pgsql/backups/$(date +"${PLC_DB_NAME}.${DATE}${suffix}.sql") + checkpoint_planetlab_db $dumpfile } -function restore_drupal_db() -{ - dumpfile=$1 - if [ -n "$dumpfile" ] ; then - [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER drupal < $1 - check - fi +function restore_drupal_db() { + dumpfile=$1 + if [ -n "$dumpfile" ]; then + [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER drupal <$1 + check + fi } -function checkpoint_drupal_db() -{ - dumpfile=$1 - pg_dump -U $PLC_DB_USER drupal > $dumpfile - check +function checkpoint_drupal_db() { + dumpfile=$1 + pg_dump -U $PLC_DB_USER drupal >$dumpfile + check } -function dump_drupal_db() -{ - dumpfile=/var/lib/pgsql/backups/$(date +"drupal.${DATE}.sql") - checkpoint_drupal_db $dumpfile - check +function dump_drupal_db() { + dumpfile=/var/lib/pgsql/backups/$(date +"drupal.${DATE}.sql") + checkpoint_drupal_db $dumpfile + 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 +function clean_dumps() { + find /var/lib/pgsql/backups '(' -name "$PLC_DB_NAME.*.sql" -o -name "drupal.*.sql" ')' -a -atime +15 | xargs rm -f + check } [ $PLC_DB_ENABLED -ne 1 ] && exit 0 case "$1" in - start) +start) MESSAGE=$"Bootstrapping the database" dialog "$MESSAGE" @@ -187,7 +178,7 @@ EOF result "$MESSAGE" ;; - migrate) +migrate) MESSAGE=$"Migrating the database" dialog "$MESSAGE" @@ -195,8 +186,8 @@ EOF result "$MESSAGE" ;; - dump) - MESSAGE=$"Dumping the databases in /var/lib/pgsql/backups" +dump) + MESSAGE=$"Dumping the databases in /var/lib/pgsql/backups" dialog "$MESSAGE" dump_planetlab_db @@ -204,34 +195,34 @@ EOF result "$MESSAGE" ;; - checkpoint) +checkpoint) MESSAGE=$"Checkpointing the databases" checkpoint_planetlab_db $2 checkpoint_drupal_db $3 ;; - restore) +restore) MESSAGE=$"Restoring the databases from checkpoint files" restore_planetlab_db $2 restore_drupal_db $3 ;; - clean-dump) - MESSAGE=$"Cleaning old database dumps" +clean-dump) + MESSAGE=$"Cleaning old database dumps" dialog "$MESSAGE" clean_dumps result "$MESSAGE" ;; - stop) +stop) MESSAGE="Ignoring request to stop myplc databases" dialog "$MESSAGE" result "" ;; - *) - echo "Usage: $0 [start|migrate|dump|checkpoint|restore|clean-dump|stop]" +*) + echo "Usage: $0 [start|migrate|dump|checkpoint|restore|clean-dump|stop]" exit 1 ;; esac