Finishing migration scripts

This commit is contained in:
2025-02-22 11:03:32 +11:00
parent 5d0a26579b
commit 489c684091
6 changed files with 69 additions and 28 deletions

2
.gitignore vendored
View File

@@ -4,7 +4,7 @@ query.sql
.logs/
server.log
tmp/
psmigrate
prmigrate
projectreshoot
static/css/output.css
view/**/*_templ.go

View File

@@ -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

View File

@@ -1,10 +1,7 @@
#!/bin/bash
# Exit on error
set -e
if [[ -z "$1" ]]; then
echo "Usage: $0 <environment> up-to|down-to <version>"
echo "Usage: $0 <environment> <version>"
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 <environment> up-to|down-to <version>"
echo "Usage: $0 <environment> <version>"
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 <environment> up-to|down-to <version>"
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

27
deploy/db/migrationcleanup.sh Executable file
View File

@@ -0,0 +1,27 @@
#!/bin/bash
# Exit on error
set -e
if [[ -z "$1" ]]; then
echo "Usage: $0 <environment> <version>"
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 <environment> <version>"
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 {} +

View File

@@ -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,
}

View File

@@ -20,7 +20,7 @@ var migrationsFS embed.FS
func main() {
if len(os.Args) != 4 {
fmt.Println("Usage: psmigrate <file_path> up-to|down-to <version>")
fmt.Println("Usage: prmigrate <file_path> up-to|down-to <version>")
os.Exit(1)
}