diff --git a/.gitignore b/.gitignore index 0764e3b..ea736c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .env data tmp +head +base diff --git a/.gitmodules b/.gitmodules index a948013..2a92371 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "migrations"] path = migrations url = git@git.orionkindel.com:dnim/db + branch = migrations +[submodule "migraionts"] + branch = migrations diff --git a/docker-compose.yml b/docker-compose.yml index 6336640..c1ecbe9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,8 +6,6 @@ services: image: "postgres:15.3-bullseye" ports: - "5432:5432" - volumes: - - "./data/base:/var/lib/postgresql/data" env_file: - "./.env.schema" head: @@ -16,7 +14,5 @@ services: restart: "always" ports: - "5433:5432" - volumes: - - "./data/head:/var/lib/postgresql/data" env_file: - "./.env.schema" diff --git a/migrations b/migrations index 66ce56e..85aca00 160000 --- a/migrations +++ b/migrations @@ -1 +1 @@ -Subproject commit 66ce56ed6af6cb63dbc4164e2f8146988dcda92b +Subproject commit 85aca00ea85ebaf371e63aa12a0f69a4a7e75c2c diff --git a/scripts/build.sh b/scripts/build.sh index 7cca97d..a4565d4 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -2,34 +2,51 @@ set -e +source ./scripts/common.sh source ./scripts/env.sh ./.env.schema -docker compose down - base_or_head="$1" rev="$2" -git worktree remove "./$base_or_head" || true -git worktree add "./$base_or_head" --detach "$rev" - -mkdir -p "./$base_or_head/data/$base_or_head" -ln -sr "../data/$base_or_head" "./$base_or_head/data/$base_or_head" -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 else port=5432 fi -url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:$port +docker compose down "$base_or_head" -dropdb=$(mktemp) -echo "drop database dnim with (force); create database dnim;" > "$dropdb" -psql --quiet "$url/postgres" --file="$dropdb" +url="postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:$port" -ls ./schema/ | xargs -I{} psql --quiet "$url/dnim" --file=./schema/{} +isready() { + until pg_isready -p "$port" 1>/dev/null; do true; done; +} + +initdb() { + dropdb=$(mktemp) + echo "drop database dnim with (force);" > "$dropdb" + psql --quiet "$url/postgres" -f "$dropdb" || true + psql --quiet "$url/postgres" -c "create database dnim" +} + +if [[ "$rev" = "empty" ]]; then + docker compose up -d "$base_or_head" + isready + initdb + exit 0 +fi + +git worktree remove --force "./$base_or_head" || true +rm -rf "./$base_or_head" || true +git worktree add "./$base_or_head" --detach "$rev" + +cp docker-compose.yml "./$base_or_head/docker-compose.yml" +cp .env.schema "./$base_or_head/.env.schema" + +cd "./$base_or_head" +docker compose up -d "$base_or_head" +cd "../$base_or_head" + +isready +initdb +ls ./schema/ | xargs -I{} bash -c "$(declare -f query_file); query_file \"$url/dnim\" ./schema/{}" diff --git a/scripts/common.sh b/scripts/common.sh new file mode 100644 index 0000000..9ccd839 --- /dev/null +++ b/scripts/common.sh @@ -0,0 +1,9 @@ +#! /bin/bash + +query_file() { + psql -v ON_ERROR_STOP=1 --single-transaction --quiet "$1" --file="$2" 1>&2 +} + +query() { + psql -v ON_ERROR_STOP=1 --single-transaction --quiet "$1" --command="$2" 1>&2 +} diff --git a/scripts/diff.sh b/scripts/diff.sh index d39426d..50636f6 100755 --- a/scripts/diff.sh +++ b/scripts/diff.sh @@ -12,16 +12,12 @@ if [[ -n "$head_arg" ]]; then head="$head_arg" fi; -cd ./migrations 1>&2 -git pull -cd ../ 1>&2 - migration="./migrations/${base}_to_${head}.sql" base_url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/dnim head_url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5433/dnim -if [[ -z "$base" ]] || [ -z "$head" ]; then +if [[ -z "$base" ]] || [[ -z "$head" ]]; then echo "revisions to diff are required ex. ./scripts/diff.sh abc bcd" 1>&2; exit 1; fi @@ -30,7 +26,7 @@ if [[ ! -f "$migration" ]]; then ./scripts/build.sh base "$base" 1>&2 ./scripts/build.sh head "$head" 1>&2 - until (pg_isready -p 5432 && pg_isready -p 5433) 1>/dev/null; do true; done; + until pg_isready -p 5432 1>/dev/null && 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 @@ -38,7 +34,7 @@ if [[ ! -f "$migration" ]]; then cd ./migrations git add --all git commit -m "$migration" - git push + cd ../ fi echo "$migration" diff --git a/scripts/migrate.sh b/scripts/migrate.sh index 510ba44..6864bf3 100755 --- a/scripts/migrate.sh +++ b/scripts/migrate.sh @@ -2,6 +2,7 @@ set -e +source ./scripts/common.sh source ./scripts/env.sh ./.env if [[ -n $(git status --porcelain) ]]; then @@ -12,21 +13,21 @@ fi to_tag="$1" get_dnim_database_count="copy (select count(*) from pg_database where datname = 'dnim') to stdout with null as '';" -dnim_database_count=$(psql "$POSTGRES_URI/postgres" -c "$get_dnim_database_count") +dnim_database_count=$(query "$POSTGRES_URI/postgres" "$get_dnim_database_count") if [[ "$dnim_database_count" = "0" ]]; then echo "fresh database" - psql "$POSTGRES_URI/postgres" -c "create database dnim;" - ls ./schema/ | xargs -I{} psql --quiet "$POSTGRES_URI/dnim" --file=./schema/{} + query "$POSTGRES_URI/postgres" "create database dnim;" + ls ./schema/ | xargs -I{} bash -c "$(declare -f query_file); query_file \"$POSTGRES_URI/dnim\" ./schema/{}" rev_last='empty' script='' else get_rev_last="copy (select to_revision from migration order by performed_on desc limit 1) to stdout with null as '';" - rev_last=$(psql "$POSTGRES_URI/dnim" -c "$get_rev_last") + rev_last=$(query "$POSTGRES_URI/dnim" "$get_rev_last") migration=$(./scripts/diff.sh "$rev_last" "$to_tag") if [[ "$2" = "--greenlight" ]]; then - psql "$POSTGRES_URI/dnim" -v ON_ERROR_STOP=1 --single-transaction -f "$migration" + query_file "$POSTGRES_URI/dnim" "$migration" else echo "migration available at $migration" echo "review and rerun with --greenlight to apply" @@ -35,5 +36,5 @@ else fi insert_migration="insert into migration (from_revision, to_revision) values ('$rev_last', '$to_tag');" -psql "$POSTGRES_URI/dnim" -c "$insert_migration" +query "$POSTGRES_URI/dnim" "$insert_migration" echo "inserted migration"