db/scripts/diff.sh

45 lines
1.0 KiB
Bash
Raw Normal View History

#! /bin/bash
2023-06-09 01:29:13 +00:00
2023-07-20 18:06:03 +00:00
set -e
2023-06-09 01:29:13 +00:00
2023-07-20 18:03:07 +00:00
source ./scripts/common.sh
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() {
echo -n "postgresql+pg8000:///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
set +e
2023-07-20 18:04:39 +00:00
if (schema_changed "$base" "$head"); then
2023-07-20 17:00:43 +00:00
echo ""
exit 0;
fi;
2023-07-20 18:03:07 +00:00
set -e
2023-07-20 17:00:43 +00:00
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 18:08:05 +00:00
until pg_isready --quiet --username "$POSTGRES_USER" -p 5432 && \
pg_isready --quiet --username "$POSTGRES_USER" -p 5433; do true; done;
2023-07-20 17:00:43 +00:00
2023-07-20 00:09:20 +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