From db9cad3520294424702a17a4e95d4b9ee7fdc98d Mon Sep 17 00:00:00 2001 From: Orion Kindel Date: Thu, 8 Jun 2023 20:29:13 -0500 Subject: [PATCH] fix: add diff, env --- .gitignore | 1 + scripts/build.sh | 32 ++++++++++++++++++++++---------- scripts/diff.sh | 23 +++++++++++++++++++++++ scripts/env.sh | 3 +++ 4 files changed, 49 insertions(+), 10 deletions(-) create mode 100755 scripts/diff.sh create mode 100755 scripts/env.sh diff --git a/.gitignore b/.gitignore index 4c49bd7..a16add5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .env +tmp diff --git a/scripts/build.sh b/scripts/build.sh index b9b5038..0de9704 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,9 +1,11 @@ #! /usr/bin/bash +source ./scripts/env.sh + rev=${1:-HEAD} if [[ -n $(git status --porcelain) ]]; then - echo "git working tree dirty"; + echo "git working tree dirty" 1>&2; exit 1; fi @@ -12,18 +14,28 @@ if [[ "$rev" = "HEAD" ]]; then port=5432 else head=$(git show --format=format:%h -q) - git reset "$rev" + git reset "$rev" --quiet --hard port=5433 fi -while read line; do export $line; done < ./.env +switch_back_to_head() { + if [[ -n "$head" ]]; then + git reset --quiet --hard "$head" + fi +} +trap 'switch_back_to_head' EXIT -url=postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:$port/$POSTGRES_DB +url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:$port/$POSTGRES_DB -psql --echo-all $url --command="drop schema public cascade; create schema public;" +psql \ + --quiet \ + $url \ + --command="drop schema public cascade; create schema public;" \ + 1>/dev/null -ls ./migrations/ | xargs -I{} psql --echo-all $url --file=./migrations/{} - -if [[ -n "$head" ]]; then - git reset "$head" -fi +ls ./migrations/ | xargs -I{} \ + psql \ + --quiet \ + $url \ + --file=./migrations/{} \ + 1>/dev/null diff --git a/scripts/diff.sh b/scripts/diff.sh new file mode 100755 index 0000000..9fc432a --- /dev/null +++ b/scripts/diff.sh @@ -0,0 +1,23 @@ +#! /usr/bin/bash + +set -e + +source ./scripts/env.sh + +rev=${1:-} + +head_url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB +base_url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5433/$POSTGRES_DB + +if [[ -z "$rev" ]]; then + echo "revision to diff is required"; + exit 1; +fi + +docker compose up -d + +./scripts/build.sh $rev +./scripts/build.sh HEAD + +echo "migrate from $rev => HEAD" 1>&2 +migra $base_url $head_url diff --git a/scripts/env.sh b/scripts/env.sh new file mode 100755 index 0000000..fde2126 --- /dev/null +++ b/scripts/env.sh @@ -0,0 +1,3 @@ +#! /usr/bin/bash + +while read line; do export $line; done < ./.env