db/scripts/diff.sh
Orion Kindel 86bb83498d
All checks were successful
gen-migrations / gen-migrations (push) Successful in 7s
fix: gen-migrations
2023-07-20 13:33:04 -05:00

42 lines
920 B
Bash
Executable File

#! /bin/bash
set -e
source ./scripts/common.sh
source ./scripts/env.sh ./.env.schema
base="$1"
head_arg="$2"
head=$(git show --format=format:%h -q | xargs)
if [[ -n "$head_arg" ]]; then
head="$head_arg"
fi;
migration="./migrations/${base}_to_${head}.sql"
db() {
echo -n "postgresql+pg8000:///dnim?port=$1&user=$POSTGRES_USER&password=$POSTGRES_PASSWORD"
}
if [[ -z "$base" ]] || [[ -z "$head" ]]; then
echo "revisions to diff are required ex. ./scripts/diff.sh abc bcd" 1>&2;
exit 1;
fi
set +e
if ! (schema_changed "$base" "$head"); then
echo ""
exit 0;
fi;
set -e
if [[ ! -f "$migration" ]]; then
./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)
migra --unsafe "$(db 5432)" "$(db 5433)" || echo "migra exited with code $?. this is /probably/ fine" 1>&2
else
cat "$migration"
fi