db/scripts/diff.sh

36 lines
889 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-20 00:03:48 +00:00
db() {
2023-07-20 00:05:36 +00:00
echo -n "postgresql:///dnim?port=$1&user=$POSTGRES_USER&password=$POSTGRES_PASSWORD"
2023-07-20 00:03:48 +00:00
}
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-20 00:03:48 +00:00
until pg_isready -p 5432 1>/dev/null && pg_isready -p 5433 1>/dev/null; do true; done;
2023-07-17 03:47:16 +00:00
2023-07-20 00:03:48 +00:00
migra --unsafe $(db 5432) $(db 5433) || echo "migra exited with code $?. this is /probably/ fine" 1>&2
2023-07-19 02:31:56 +00:00
else
cat "$migration"
fi