fix: add diff, env

This commit is contained in:
Orion Kindel 2023-06-08 20:29:13 -05:00
parent e52348cdd1
commit db9cad3520
Signed by untrusted user who does not match committer: orion
GPG Key ID: 6D4165AE4C928719
4 changed files with 49 additions and 10 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
.env
tmp

View File

@ -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

23
scripts/diff.sh Executable file
View File

@ -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

3
scripts/env.sh Executable file
View File

@ -0,0 +1,3 @@
#! /usr/bin/bash
while read line; do export $line; done < ./.env