diff --git a/.gitignore b/.gitignore index c2f2f8c..9e3f92b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ query.sql .logs/ server.log tmp/ -psmigrate +prmigrate projectreshoot static/css/output.css view/**/*_templ.go diff --git a/Makefile b/Makefile index f3535a3..a8d363a 100644 --- a/Makefile +++ b/Makefile @@ -34,4 +34,4 @@ clean: migrate: go mod tidy && \ go generate && \ - go build -ldflags="-w -s" -o psmigrate ./migrate + go build -ldflags="-w -s" -o prmigrate ./migrate diff --git a/deploy/db/migrate.sh b/deploy/db/migrate.sh index 629d5f4..9285475 100755 --- a/deploy/db/migrate.sh +++ b/deploy/db/migrate.sh @@ -1,10 +1,7 @@ #!/bin/bash -# Exit on error -set -e - if [[ -z "$1" ]]; then - echo "Usage: $0 up-to|down-to " + echo "Usage: $0 " exit 1 fi ENVR="$1" @@ -13,37 +10,47 @@ if [[ "$ENVR" != "production" && "$ENVR" != "staging" ]]; then exit 1 fi if [[ -z "$2" ]]; then - echo "Usage: $0 up-to|down-to " + echo "Usage: $0 " exit 1 fi -CMD="$2" -if [[ "$CMD" != "up-to" && "$CMD" != "down-to" ]]; then - echo "Error: Command must be 'up-to' or 'down-to'." - exit 1 -fi -if [[ -z "$3" ]]; then - echo "Usage: $0 up-to|down-to " - exit 1 -fi -VER="$3" +TGT_VER="$2" re='^[0-9]+$' -if ! [[ $VER =~ $re ]] ; then - echo "Error: version not a number" >&2; exit 1 +if ! [[ $TGT_VER =~ $re ]] ; then + echo "Error: version not a number" >&2 + exit 1 fi -BACKUP_FILE=$(/bin/bash ./backup.sh "$ENVR" "$VER" | grep -oP '(?<=Backup created: ).*') -if [[ "$BACKUP_FILE" == "" ]]; then +BACKUP_OUTPUT=$(/bin/bash ./backup.sh "$ENVR" 2>&1) +echo "$BACKUP_OUTPUT" +if [[ $? -ne 0 ]]; then + exit 1 +fi +BACKUP_FILE=$(echo "$BACKUP_OUTPUT" | grep -oP '(?<=Backup created: ).*') +if [[ -z "$BACKUP_FILE" ]]; then echo "Error: backup failed" exit 1 fi + +FILE_NAME=${BACKUP_FILE##*/} +CUR_VER=${FILE_NAME%%-*} +if [[ $((+$TGT_VER)) == $((+$CUR_VER)) ]]; then + echo "Version same, skipping migration" + exit 0 +fi +if [[ $((+$TGT_VER)) > $((+$CUR_VER)) ]]; then + CMD="up-to" +fi +if [[ $((+$TGT_VER)) < $((+$CUR_VER)) ]]; then + CMD="down-to" +fi TIMESTAMP=$(date +"%Y-%m-%d-%H%M") ACTIVE_DIR="/home/deploy/$ENVR" DATA_DIR="/home/deploy/data/$ENVR" BACKUP_DIR="/home/deploy/data/backups/$ENVR" -UPDATED_BACKUP="$BACKUP_DIR/${VER}-${TIMESTAMP}.db" -UPDATED_COPY="$DATA_DIR/${VER}.db" -UPDATED_LINK="$ACTIVE_DIR/${VER}.db" +UPDATED_BACKUP="$BACKUP_DIR/${TGT_VER}-${TIMESTAMP}.db" +UPDATED_COPY="$DATA_DIR/${TGT_VER}.db" +UPDATED_LINK="$ACTIVE_DIR/${TGT_VER}.db" cp $BACKUP_FILE $UPDATED_BACKUP failed_cleanup() { @@ -51,9 +58,8 @@ failed_cleanup() { } trap 'if [ $? -ne 0 ]; then failed_cleanup; fi' EXIT -echo "Migration in progress" -echo $UPDATED_BACKUP $CMD $VER -./psmigrate $UPDATED_BACKUP $CMD $VER +echo "Migration in progress from $CUR_VER to $TGT_VER" +./prmigrate $UPDATED_BACKUP $CMD $TGT_VER if [ $? -ne 0 ]; then echo "Migration failed" exit 1 diff --git a/deploy/db/migrationcleanup.sh b/deploy/db/migrationcleanup.sh new file mode 100755 index 0000000..9de3c5a --- /dev/null +++ b/deploy/db/migrationcleanup.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# Exit on error +set -e + +if [[ -z "$1" ]]; then + echo "Usage: $0 " + exit 1 +fi + +ENVR="$1" +if [[ "$ENVR" != "production" && "$ENVR" != "staging" ]]; then + echo "Error: environment must be 'production' or 'staging'." + exit 1 +fi +if [[ -z "$2" ]]; then + echo "Usage: $0 " + exit 1 +fi +TGT_VER="$2" +re='^[0-9]+$' +if ! [[ $TGT_VER =~ $re ]] ; then + echo "Error: version not a number" >&2 + exit 1 +fi +ACTIVE_DIR="/home/deploy/$ENVR" +find "$ACTIVE_DIR" -type l -name "*.db" ! -name "${TGT_VER}.db" -exec rm -v {} + diff --git a/main.go b/main.go index ede093c..a68fb5e 100644 --- a/main.go +++ b/main.go @@ -94,6 +94,12 @@ func run(ctx context.Context, w io.Writer, args map[string]string) error { return errors.Wrap(err, "server.GetConfig") } + // Return the version of the database required + if args["dbver"] == "true" { + fmt.Printf("Database version: %s\n", config.DBName) + return nil + } + var logfile *os.File = nil if config.LogOutput == "both" || config.LogOutput == "file" { logfile, err = logging.GetLogFile(config.LogDir) @@ -186,6 +192,7 @@ func main() { host := flag.String("host", "", "Override host to listen on") port := flag.String("port", "", "Override port to listen on") test := flag.Bool("test", false, "Run test function instead of main program") + dbver := flag.Bool("dbver", false, "Get the version of the database required") loglevel := flag.String("loglevel", "", "Set log level") logoutput := flag.String("logoutput", "", "Set log destination (file, console or both)") flag.Parse() @@ -195,6 +202,7 @@ func main() { "host": *host, "port": *port, "test": strconv.FormatBool(*test), + "dbver": strconv.FormatBool(*dbver), "loglevel": *loglevel, "logoutput": *logoutput, } diff --git a/migrate/migrate.go b/migrate/migrate.go index d42531a..dd8a99e 100644 --- a/migrate/migrate.go +++ b/migrate/migrate.go @@ -20,7 +20,7 @@ var migrationsFS embed.FS func main() { if len(os.Args) != 4 { - fmt.Println("Usage: psmigrate up-to|down-to ") + fmt.Println("Usage: prmigrate up-to|down-to ") os.Exit(1) }