Skip to content

Instantly share code, notes, and snippets.

@Dyrcona
Last active February 8, 2024 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dyrcona/00bd6b6290b6fbbb579c7f93b360ab0d to your computer and use it in GitHub Desktop.
Save Dyrcona/00bd6b6290b6fbbb579c7f93b360ab0d to your computer and use it in GitHub Desktop.
A bash script to make custom database upgrade scripts for Evergreen ILS.
#!/bin/bash
# ---------------------------------------------------------------
# Copyright © 2018, 2021-2024 Jason J.A. Stephenson <jason@sigio.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# ---------------------------------------------------------------
#
# The contents of this script were mostly copy/pasted from Evergreen's
# build tools' make_release script.
#
# Make sure that we're run from an Evergreen git repository.
if [[ ! -d ".git" || ! -d "Open-ILS" ]]; then
echo "We don't appear to be in an Evergreen-ILS git repository."
exit 1
fi
GIT_BRANCH=`git rev-parse --abbrev-ref HEAD | sed 's|.*/||'`
# Options:
VERSION=AUTO # -v (version)
PREV_BRANCH=AUTO # -f (from)
PREV_VERSION=AUTO # -F (from version)
UPGRADE_PREVIEW=NO # -r (upgrade preview/edit)
PREFIX="" # -P (prefix to filename)
APPEND_FILE="" # -a (SQL file to append to end of upgrade script)
PREPEND_FILE="" # -p (SQL file to prepend to beginning of upgrade script)
OUTPUT_DIRECTORY="Open-ILS/src/sql/Pg/version-upgrade" # -O (directory where to write the db upgrade script)
CLOBBER="NO" # -C Clobber (overwrite) existing output flle if it exists.
while getopts "v:f:F:P:a:p:O:rC" opt
do
case $opt in
v)
VERSION=$OPTARG
;;
f)
PREV_BRANCH=$OPTARG
;;
F)
PREV_VERSION=$OPTARG
;;
P)
PREFIX=$OPTARG
;;
r)
UPGRADE_PREVIEW="YES"
;;
a)
APPEND_FILE="$OPTARG"
;;
p)
PREPEND_FILE="$OPTARG"
;;
O)
OUTPUT_DIRECTORY="$OPTARG"
;;
C)
CLOBBER="YES"
;;
\?)
exit 1
;;
esac
done
if [ "$VERSION" == "AUTO" ]; then
# Auto-pick version based on branch name
VERSION=`echo $GIT_BRANCH | sed 's/.*rel_\([0-9]\+\)_\([0-9]\+\)_\([0-9]\+\)_\(.\+\)$/\1.\2.\3-\4/'`
VERSION=`echo $VERSION | sed 's/.*rel_\([0-9]\+\)_\([0-9]\+\)_\([0-9]\+\)$/\1.\2.\3/'`
VERSION=`echo $VERSION | sed 's/.*rel_\([0-9]\+\)_\([0-9]\+\)_\(.\+\)$/\1.\2-\3/'`
VERSION=`echo $VERSION | sed 's/.*rel_\([0-9]\+\)_\([0-9]\+\)$/\1.\2/'`
if [ "$VERSION" = "$GIT_BRANCH" ]; then
echo "AUTO VERSION FAILED."
exit 1
fi
fi
if [[ "$PREV_BRANCH" == "AUTO" ]]
then
echo "Must specify from branch with -f"
exit 1
fi
if [[ "$PREV_VERSION" == "AUTO" ]]
then
PREV_BRANCH_END=`echo $PREV_BRANCH | sed 's|.*/||'`
PREV_VERSION=`echo $PREV_BRANCH_END | sed 's/rel_\([0-9]\+\)_\([0-9]\+\)_\([0-9]\+\)_\(.\+\)$/\1.\2.\3-\4/'`
PREV_VERSION=`echo $PREV_VERSION | sed 's/rel_\([0-9]\+\)_\([0-9]\+\)_\([0-9]\+\)$/\1.\2.\3/'`
PREV_VERSION=`echo $PREV_VERSION | sed 's/rel_\([0-9]\+\)_\([0-9]\+\)_\(.\+\)$/\1.\2-\3/'`
PREV_VERSION=`echo $PREV_VERSION | sed 's/rel_\([0-9]\+\)_\([0-9]\+\)$/\1.\2/'`
if [ "$PREV_VERSION" == "$PREV_BRANCH_END" ]; then
echo "AUTO PREVIOUS VERSION FAILED."
exit 1
fi
fi
UPGRADE_FILE="$OUTPUT_DIRECTORY/$PREFIX$PREV_VERSION-$VERSION-upgrade-db.sql"
if [ -f "$UPGRADE_FILE" -a "$CLOBBER" = "NO" ]; then
echo "$UPGRADE_FILE already exists. Skipping."
UPGRADE_FILE=""
else
if [ -f "$UPGRADE_FILE" -a "$CLOBBER" = "YES" ]; then
truncate -s0 "$UPGRADE_FILE"
fi
echo "Checking for DB upgrade potential...."
git ls-tree --name-only $PREV_BRANCH -- Open-ILS/src/sql/Pg/upgrade/ | sort > old_upgrades.txt
git ls-tree --name-only HEAD -- Open-ILS/src/sql/Pg/upgrade/ | sort > new_upgrades.txt
UPGRADE_CHECK=`diff old_upgrades.txt new_upgrades.txt | grep '^>' | cut -d' ' -f2`
echo "Building Upgrade Script: $UPGRADE_FILE"
if [ -f "$PREPEND_FILE" ]; then
cat "$PREPEND_FILE" >> $UPGRADE_FILE
echo "-- End of prepended code." >> $UPGRADE_FILE
echo "" >> $UPGRADE_FILE
fi
echo "--Upgrade Script for $PREV_VERSION to $VERSION" >> $UPGRADE_FILE
echo "\set eg_version '''$VERSION'''" >> $UPGRADE_FILE
echo "BEGIN;" >> $UPGRADE_FILE
grep "config.upgrade_log.*$VERSION" Open-ILS/src/sql/Pg/002.schema.config.sql >> $UPGRADE_FILE
for F in $UPGRADE_CHECK; do
cat $F 2>/dev/null | grep -v '^\s*\(BEGIN\|COMMIT\);\s*$' >> $UPGRADE_FILE
done;
echo "COMMIT;" >> $UPGRADE_FILE
echo "" >> $UPGRADE_FILE
echo "-- Update auditor tables to catch changes to source tables." >> $UPGRADE_FILE
echo "-- Can be removed/skipped if there were no schema changes." >> $UPGRADE_FILE
echo "SELECT auditor.update_auditors();" >> $UPGRADE_FILE
if [ -f "$APPEND_FILE" ]; then
echo "" >> $UPGRADE_FILE
echo "-- Beginning of appended code." >> $UPGRADE_FILE
cat "$APPEND_FILE" >> $UPGRADE_FILE
fi
# Cleanup not found in make_release.
rm old_upgrades.txt new_upgrades.txt
if [ "$UPGRADE_PREVIEW" = "YES" ]; then
read -p "Please manually check the upgrade file."
${EDITOR:-vi} $UPGRADE_FILE
fi;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment