db/scripts/diff.sh

36 lines
1007 B
Bash
Raw Normal View History

#! /bin/bash
2023-06-09 01:29:13 +00:00
set -e
2023-06-09 01:29:13 +00:00
source ./scripts/env.sh ./.env.schema
2023-06-09 01:29:13 +00:00
2023-07-17 03:47:16 +00:00
base="$1"
head_arg="$2"
head=$(git show --format=format:%h -q | xargs)
2023-07-16 02:36:32 +00:00
if [[ -n "$head_arg" ]]; then
head="$head_arg"
fi;
2023-07-17 03:47:16 +00:00
migration="./migrations/${base}_to_${head}.sql"
2023-06-09 01:29:13 +00:00
2023-07-19 23:53:13 +00:00
pg_host=${PG_HOST:-localhost}
base_url="postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@$pg_host:5432/dnim"
head_url="postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@$pg_host:5433/dnim"
2023-07-17 03:47:16 +00:00
2023-07-17 19:44:40 +00:00
if [[ -z "$base" ]] || [[ -z "$head" ]]; then
2023-07-17 03:47:16 +00:00
echo "revisions to diff are required ex. ./scripts/diff.sh abc bcd" 1>&2;
2023-06-09 01:29:13 +00:00
exit 1;
fi
if [[ ! -f "$migration" ]]; then
2023-07-19 02:31:56 +00:00
./scripts/build.sh base "$base" 1>&2 || (echo "base failed to build" && exit 1)
./scripts/build.sh head "$head" 1>&2 || (echo "head failed to build" && exit 1)
2023-06-27 23:43:04 +00:00
2023-07-19 23:59:13 +00:00
until pg_isready -h "$pg_host" -p 5432 1>/dev/null && pg_isready -h "$pg_host" -p 5433 1>/dev/null; do true; done;
2023-07-17 03:47:16 +00:00
2023-07-19 02:31:56 +00:00
migra --unsafe "$base_url" "$head_url" || echo "migra exited with code $?. this is /probably/ fine" 1>&2
else
cat "$migration"
fi