chore: rework migrations flow
This commit is contained in:
parent
5f9c3c7b8e
commit
e4984ddce2
@ -1,3 +1,2 @@
|
|||||||
POSTGRES_USER=postgres
|
POSTGRES_USER=postgres
|
||||||
POSTGRES_PASSWORD=password
|
POSTGRES_PASSWORD=password
|
||||||
POSTGRES_DB=dnim
|
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,3 @@
|
|||||||
.env
|
.env
|
||||||
data
|
data
|
||||||
tmp
|
tmp
|
||||||
migrations
|
|
||||||
|
@ -5,7 +5,7 @@ services:
|
|||||||
container_name: "base"
|
container_name: "base"
|
||||||
image: "postgres:15.3-bullseye"
|
image: "postgres:15.3-bullseye"
|
||||||
ports:
|
ports:
|
||||||
- "5433:5432"
|
- "5432:5432"
|
||||||
volumes:
|
volumes:
|
||||||
- "./data/base:/var/lib/postgresql/data"
|
- "./data/base:/var/lib/postgresql/data"
|
||||||
env_file:
|
env_file:
|
||||||
@ -15,7 +15,7 @@ services:
|
|||||||
image: "postgres:15.3-bullseye"
|
image: "postgres:15.3-bullseye"
|
||||||
restart: "always"
|
restart: "always"
|
||||||
ports:
|
ports:
|
||||||
- "5432:5432"
|
- "5433:5432"
|
||||||
volumes:
|
volumes:
|
||||||
- "./data/head:/var/lib/postgresql/data"
|
- "./data/head:/var/lib/postgresql/data"
|
||||||
env_file:
|
env_file:
|
||||||
|
@ -1,41 +1,35 @@
|
|||||||
#! /bin/bash
|
#! /bin/bash
|
||||||
|
|
||||||
set -ex
|
set -e
|
||||||
|
|
||||||
source ./scripts/env.sh ./.env.schema
|
source ./scripts/env.sh ./.env.schema
|
||||||
|
|
||||||
rev=${1:-HEAD}
|
docker compose down
|
||||||
|
|
||||||
if [[ -n $(git status --porcelain) ]]; then
|
base_or_head="$1"
|
||||||
echo "git working tree dirty" 1>&2;
|
rev="$2"
|
||||||
exit 1;
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "$rev" = "HEAD" ]]; then
|
git worktree remove "./$base_or_head" || true
|
||||||
head=""
|
git worktree add "./$base_or_head" --detach "$rev"
|
||||||
port=5432
|
|
||||||
else
|
mkdir -p "./$base_or_head/data/$base_or_head"
|
||||||
head=$(git show --format=format:%h -q)
|
ln -sr "../data/$base_or_head" "./$base_or_head/data/$base_or_head"
|
||||||
git reset --quiet --hard "$rev"
|
cp docker-compose.yml "./$base_or_head/docker-compose.yml"
|
||||||
|
|
||||||
|
cd "./$base_or_head"
|
||||||
|
docker compose up -d "$base_or_head"
|
||||||
|
cd "../$base_or_head"
|
||||||
|
|
||||||
|
if [[ "$base_or_head" = "head" ]]; then
|
||||||
port=5433
|
port=5433
|
||||||
|
else
|
||||||
|
port=5432
|
||||||
fi
|
fi
|
||||||
|
|
||||||
switch_back_to_head() {
|
|
||||||
if [[ -n "$head" ]]; then
|
|
||||||
git reset --quiet --hard "$head"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
trap 'switch_back_to_head' EXIT
|
|
||||||
|
|
||||||
url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:$port
|
url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:$port
|
||||||
|
|
||||||
dropdb=$(mktemp)
|
dropdb=$(mktemp)
|
||||||
echo "drop database dnim with (force); create database dnim;" > "$dropdb"
|
echo "drop database dnim with (force); create database dnim;" > "$dropdb"
|
||||||
psql --quiet "$url/postgres" --file="$dropdb" 1>/dev/null
|
psql --quiet "$url/postgres" --file="$dropdb"
|
||||||
|
|
||||||
ls ./schema/ | xargs -I{} \
|
ls ./schema/ | xargs -I{} psql --quiet "$url/dnim" --file=./schema/{}
|
||||||
psql \
|
|
||||||
--quiet \
|
|
||||||
"$url/dnim" \
|
|
||||||
--file=./schema/{} \
|
|
||||||
1>/dev/null
|
|
||||||
|
@ -4,40 +4,41 @@ set -e
|
|||||||
|
|
||||||
source ./scripts/env.sh ./.env.schema
|
source ./scripts/env.sh ./.env.schema
|
||||||
|
|
||||||
rev=$(echo "$1" | xargs)
|
base="$1"
|
||||||
head_arg=$(echo "$2" | xargs)
|
head_arg="$2"
|
||||||
head=$(git show --format=format:%h -q | xargs)
|
head=$(git show --format=format:%h -q | xargs)
|
||||||
|
|
||||||
if [[ -n "$head_arg" ]]; then
|
if [[ -n "$head_arg" ]]; then
|
||||||
head="$head_arg"
|
head="$head_arg"
|
||||||
fi;
|
fi;
|
||||||
|
|
||||||
migration="./migrations/${rev}_to_${head}.sql"
|
cd ./migrations 1>&2
|
||||||
|
git pull
|
||||||
|
cd ../ 1>&2
|
||||||
|
|
||||||
head_url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/dnim
|
migration="./migrations/${base}_to_${head}.sql"
|
||||||
base_url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5433/dnim
|
|
||||||
|
|
||||||
if [[ -z "$rev" ]]; then
|
base_url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/dnim
|
||||||
echo "revision to diff is required" 1>&2;
|
head_url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5433/dnim
|
||||||
|
|
||||||
|
if [[ -z "$base" ]] || [ -z "$head" ]; then
|
||||||
|
echo "revisions to diff are required ex. ./scripts/diff.sh abc bcd" 1>&2;
|
||||||
exit 1;
|
exit 1;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
docker compose up -d 1>&2
|
|
||||||
|
|
||||||
rm -r ./migrations || true
|
|
||||||
git fetch --all 1>&2
|
|
||||||
git restore ./migrations --source origin/manual_migrations 1>&2
|
|
||||||
|
|
||||||
if [[ ! -f "$migration" ]]; then
|
if [[ ! -f "$migration" ]]; then
|
||||||
./scripts/build.sh "$rev" 1>&2
|
./scripts/build.sh base "$base" 1>&2
|
||||||
./scripts/build.sh HEAD 1>&2
|
./scripts/build.sh head "$head" 1>&2
|
||||||
|
|
||||||
echo "migrate from $rev => HEAD" 1>&2
|
|
||||||
|
|
||||||
migra --unsafe $base_url $head_url >> "$migration" \
|
until (pg_isready -p 5432 && pg_isready -p 5433) 1>/dev/null; do true; done;
|
||||||
|
|
||||||
|
migra --unsafe "$base_url" "$head_url" > "$migration" \
|
||||||
|| echo "migra exited with code $?. this is /probably/ fine" 1>&2
|
|| echo "migra exited with code $?. this is /probably/ fine" 1>&2
|
||||||
|
|
||||||
|
cd ./migrations
|
||||||
|
git add --all
|
||||||
|
git commit -m "$migration"
|
||||||
|
git push
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "$migration"
|
echo "$migration"
|
||||||
|
|
||||||
docker compose down 1>&2
|
|
||||||
|
@ -18,22 +18,22 @@ if [[ "$dnim_database_count" = "0" ]]; then
|
|||||||
echo "fresh database"
|
echo "fresh database"
|
||||||
psql "$POSTGRES_URI/postgres" -c "create database dnim;"
|
psql "$POSTGRES_URI/postgres" -c "create database dnim;"
|
||||||
ls ./schema/ | xargs -I{} psql --quiet "$POSTGRES_URI/dnim" --file=./schema/{}
|
ls ./schema/ | xargs -I{} psql --quiet "$POSTGRES_URI/dnim" --file=./schema/{}
|
||||||
last_revision='empty'
|
rev_last='empty'
|
||||||
script=''
|
script=''
|
||||||
else
|
else
|
||||||
get_last_revision="copy (select to_revision from migration order by performed_on desc limit 1) to stdout with null as '';"
|
get_rev_last="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")
|
rev_last=$(psql "$POSTGRES_URI/dnim" -c "$get_rev_last")
|
||||||
migration_file=$(./scripts/diff.sh "$last_revision" "$to_tag")
|
migration=$(./scripts/diff.sh "$rev_last" "$to_tag")
|
||||||
|
|
||||||
if [[ "$2" = "--greenlight" ]]; then
|
if [[ "$2" = "--greenlight" ]]; then
|
||||||
psql "$POSTGRES_URI/dnim" -v ON_ERROR_STOP=1 --single-transaction -f "$migration_file"
|
psql "$POSTGRES_URI/dnim" -v ON_ERROR_STOP=1 --single-transaction -f "$migration"
|
||||||
else
|
else
|
||||||
echo "migration available at $migration_file"
|
echo "migration available at $migration"
|
||||||
echo "review and rerun with --greenlight to apply"
|
echo "review and rerun with --greenlight to apply"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
insert_migration="insert into migration (from_revision, to_revision) values ('$last_revision', '$to_tag');"
|
insert_migration="insert into migration (from_revision, to_revision) values ('$rev_last', '$to_tag');"
|
||||||
psql "$POSTGRES_URI/dnim" -c "$insert_migration"
|
psql "$POSTGRES_URI/dnim" -c "$insert_migration"
|
||||||
echo "inserted migration"
|
echo "inserted migration"
|
||||||
|
Loading…
Reference in New Issue
Block a user