fix: support manually fiddling with migrations
Some checks failed
migrate-devel / migrate-devel (push) Failing after 4s
migrate-stage / migrate-stage (push) Failing after 3s

This commit is contained in:
Orion Kindel 2023-07-02 19:40:19 -05:00
parent b3de72daf2
commit 5f310779b3
Signed by untrusted user who does not match committer: orion
GPG Key ID: 6D4165AE4C928719
3 changed files with 28 additions and 17 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.env
data
tmp
migrations

View File

@ -1,25 +1,38 @@
#! /bin/bash
set -ex
set -e
source ./scripts/env.sh ./.env.schema
rev=${1:-}
rev=$(echo "$1" | xargs)
head=$(git show --format=format:%h -q | xargs)
migration="./migrations/${rev}_to_${head}.sql"
head_url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/dnim
base_url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5433/dnim
if [[ -z "$rev" ]]; then
echo "revision to diff is required";
echo "revision to diff is required" 1>&2;
exit 1;
fi
docker compose up -d
docker compose up -d 1>&2
./scripts/build.sh $rev
./scripts/build.sh HEAD
rm -r ./migrations || true
git fetch --all 1>&2
git restore ./migrations --source origin/manual_migrations 1>&2
echo "migrate from $rev => HEAD" 1>&2
migra --unsafe $base_url $head_url || echo "migra exited with code $?. this is /probably/ fine" 1>&2
if [[ ! -f "$migration" ]]; then
./scripts/build.sh "$rev"
./scripts/build.sh HEAD
echo "migrate from $rev => HEAD" 1>&2
docker compose down
migra --unsafe $base_url $head_url \
|| echo "migra exited with code $?. this is /probably/ fine" 1>&2 \
> "$migration"
fi
echo "$migration"
docker compose down 1>&2

View File

@ -1,6 +1,6 @@
#! /bin/bash
set -xe
set -e
source ./scripts/env.sh ./.env
@ -23,16 +23,13 @@ if [[ "$dnim_database_count" = "0" ]]; then
else
get_last_revision="copy (select to_revision from migration order by performed_on desc limit 1) to stdout with null as '';"
last_revision=$(psql "$POSTGRES_URI/dnim" -c "$get_last_revision")
script=$(./scripts/diff.sh "$last_revision")
migration_file=$(./scripts/diff.sh "$last_revision")
if [[ "$1" = "--greenlight" ]]; then
psql "$POSTGRES_URI/dnim" -c "$script"
psql "$POSTGRES_URI/dnim" -f "$migration_file"
else
mkdir -p tmp
to_review="tmp/migrate_${last_revision}_to_${head}.sql"
echo "$script" > "$to_review"
echo "script written to $to_review"
echo "review and rerun with --greenlight to apply migration"
echo "migration available at $migration_file"
echo "review and rerun with --greenlight to apply"
exit
fi
fi