Finishing migration scripts
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -4,7 +4,7 @@ query.sql
|
|||||||
.logs/
|
.logs/
|
||||||
server.log
|
server.log
|
||||||
tmp/
|
tmp/
|
||||||
psmigrate
|
prmigrate
|
||||||
projectreshoot
|
projectreshoot
|
||||||
static/css/output.css
|
static/css/output.css
|
||||||
view/**/*_templ.go
|
view/**/*_templ.go
|
||||||
|
|||||||
2
Makefile
2
Makefile
@@ -34,4 +34,4 @@ clean:
|
|||||||
migrate:
|
migrate:
|
||||||
go mod tidy && \
|
go mod tidy && \
|
||||||
go generate && \
|
go generate && \
|
||||||
go build -ldflags="-w -s" -o psmigrate ./migrate
|
go build -ldflags="-w -s" -o prmigrate ./migrate
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Exit on error
|
|
||||||
set -e
|
|
||||||
|
|
||||||
if [[ -z "$1" ]]; then
|
if [[ -z "$1" ]]; then
|
||||||
echo "Usage: $0 <environment> up-to|down-to <version>"
|
echo "Usage: $0 <environment> <version>"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
ENVR="$1"
|
ENVR="$1"
|
||||||
@@ -13,37 +10,47 @@ if [[ "$ENVR" != "production" && "$ENVR" != "staging" ]]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
if [[ -z "$2" ]]; then
|
if [[ -z "$2" ]]; then
|
||||||
echo "Usage: $0 <environment> up-to|down-to <version>"
|
echo "Usage: $0 <environment> <version>"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
CMD="$2"
|
TGT_VER="$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"
|
|
||||||
re='^[0-9]+$'
|
re='^[0-9]+$'
|
||||||
if ! [[ $VER =~ $re ]] ; then
|
if ! [[ $TGT_VER =~ $re ]] ; then
|
||||||
echo "Error: version not a number" >&2; exit 1
|
echo "Error: version not a number" >&2
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
BACKUP_FILE=$(/bin/bash ./backup.sh "$ENVR" "$VER" | grep -oP '(?<=Backup created: ).*')
|
BACKUP_OUTPUT=$(/bin/bash ./backup.sh "$ENVR" 2>&1)
|
||||||
if [[ "$BACKUP_FILE" == "" ]]; then
|
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"
|
echo "Error: backup failed"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
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")
|
TIMESTAMP=$(date +"%Y-%m-%d-%H%M")
|
||||||
|
|
||||||
ACTIVE_DIR="/home/deploy/$ENVR"
|
ACTIVE_DIR="/home/deploy/$ENVR"
|
||||||
DATA_DIR="/home/deploy/data/$ENVR"
|
DATA_DIR="/home/deploy/data/$ENVR"
|
||||||
BACKUP_DIR="/home/deploy/data/backups/$ENVR"
|
BACKUP_DIR="/home/deploy/data/backups/$ENVR"
|
||||||
UPDATED_BACKUP="$BACKUP_DIR/${VER}-${TIMESTAMP}.db"
|
UPDATED_BACKUP="$BACKUP_DIR/${TGT_VER}-${TIMESTAMP}.db"
|
||||||
UPDATED_COPY="$DATA_DIR/${VER}.db"
|
UPDATED_COPY="$DATA_DIR/${TGT_VER}.db"
|
||||||
UPDATED_LINK="$ACTIVE_DIR/${VER}.db"
|
UPDATED_LINK="$ACTIVE_DIR/${TGT_VER}.db"
|
||||||
|
|
||||||
cp $BACKUP_FILE $UPDATED_BACKUP
|
cp $BACKUP_FILE $UPDATED_BACKUP
|
||||||
failed_cleanup() {
|
failed_cleanup() {
|
||||||
@@ -51,9 +58,8 @@ failed_cleanup() {
|
|||||||
}
|
}
|
||||||
trap 'if [ $? -ne 0 ]; then failed_cleanup; fi' EXIT
|
trap 'if [ $? -ne 0 ]; then failed_cleanup; fi' EXIT
|
||||||
|
|
||||||
echo "Migration in progress"
|
echo "Migration in progress from $CUR_VER to $TGT_VER"
|
||||||
echo $UPDATED_BACKUP $CMD $VER
|
./prmigrate $UPDATED_BACKUP $CMD $TGT_VER
|
||||||
./psmigrate $UPDATED_BACKUP $CMD $VER
|
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo "Migration failed"
|
echo "Migration failed"
|
||||||
exit 1
|
exit 1
|
||||||
|
|||||||
27
deploy/db/migrationcleanup.sh
Executable file
27
deploy/db/migrationcleanup.sh
Executable 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 {} +
|
||||||
8
main.go
8
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 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
|
var logfile *os.File = nil
|
||||||
if config.LogOutput == "both" || config.LogOutput == "file" {
|
if config.LogOutput == "both" || config.LogOutput == "file" {
|
||||||
logfile, err = logging.GetLogFile(config.LogDir)
|
logfile, err = logging.GetLogFile(config.LogDir)
|
||||||
@@ -186,6 +192,7 @@ func main() {
|
|||||||
host := flag.String("host", "", "Override host to listen on")
|
host := flag.String("host", "", "Override host to listen on")
|
||||||
port := flag.String("port", "", "Override port to listen on")
|
port := flag.String("port", "", "Override port to listen on")
|
||||||
test := flag.Bool("test", false, "Run test function instead of main program")
|
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")
|
loglevel := flag.String("loglevel", "", "Set log level")
|
||||||
logoutput := flag.String("logoutput", "", "Set log destination (file, console or both)")
|
logoutput := flag.String("logoutput", "", "Set log destination (file, console or both)")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@@ -195,6 +202,7 @@ func main() {
|
|||||||
"host": *host,
|
"host": *host,
|
||||||
"port": *port,
|
"port": *port,
|
||||||
"test": strconv.FormatBool(*test),
|
"test": strconv.FormatBool(*test),
|
||||||
|
"dbver": strconv.FormatBool(*dbver),
|
||||||
"loglevel": *loglevel,
|
"loglevel": *loglevel,
|
||||||
"logoutput": *logoutput,
|
"logoutput": *logoutput,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ var migrationsFS embed.FS
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) != 4 {
|
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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user