db/scripts/diff.sh

45 lines
983 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
cd ./migrations 1>&2
git pull
cd ../ 1>&2
2023-06-09 01:29:13 +00:00
2023-07-17 03:47:16 +00:00
migration="./migrations/${base}_to_${head}.sql"
2023-06-09 01:29:13 +00:00
2023-07-17 03:47:16 +00:00
base_url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/dnim
head_url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5433/dnim
if [[ -z "$base" ]] || [ -z "$head" ]; then
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-17 03:47:16 +00:00
./scripts/build.sh base "$base" 1>&2
./scripts/build.sh head "$head" 1>&2
2023-06-27 23:43:04 +00:00
2023-07-17 03:47:16 +00:00
until (pg_isready -p 5432 && pg_isready -p 5433) 1>/dev/null; do true; done;
migra --unsafe "$base_url" "$head_url" > "$migration" \
2023-07-15 03:09:14 +00:00
|| echo "migra exited with code $?. this is /probably/ fine" 1>&2
2023-07-17 03:47:16 +00:00
cd ./migrations
git add --all
git commit -m "$migration"
git push
fi
echo "$migration"