db/scripts/build.sh
Orion Kindel ee6390d723
Some checks failed
migrate-stage / migrate-stage (push) Has been cancelled
gen-migrations / gen-migrations (push) Successful in 15s
migrate-devel / migrate-devel (push) Failing after 5s
fix: migrate and gen_migrations should work
2023-07-20 12:48:07 -05:00

53 lines
1.2 KiB
Bash
Executable File

#! /bin/bash
set -e
source ./scripts/common.sh
source ./scripts/env.sh ./.env.schema
base_or_head="$1"
rev="$2"
if [[ -n "$base_or_head" ]]; then
docker compose kill "$base_or_head"
docker compose rm -vf "$base_or_head"
fi
if [[ "$base_or_head" = "base" ]]; then
port=5432
else
port=5433
fi
db() {
echo -n "postgresql:///$1?port=$port&user=$POSTGRES_USER&password=$POSTGRES_PASSWORD"
}
initdb() {
dropdb=$(mktemp)
echo "drop database dnim with (force);" > "$dropdb"
psql --quiet $(db postgres) -f "$dropdb" || true
psql --quiet $(db postgres) -c "create database dnim"
}
if [[ "$rev" = "empty" ]]; then
docker compose up --wait --wait-timeout 30 -d "$base_or_head"
initdb
exit 0
fi
if [[ -n "$rev" ]]; then
git worktree remove --force "./$base_or_head" &>/dev/null || true
rm -rf "./$base_or_head" &>/dev/null || true
git worktree add "./$base_or_head" --detach "$rev" &>/dev/null
cp docker-compose.yml "./$base_or_head/docker-compose.yml"
cp .env.schema "./$base_or_head/.env.schema"
cd "./$base_or_head"
fi
docker compose up --wait --wait-timeout 30 -d "${base_or_head:-head}"
initdb
ls ./schema/ | xargs -I{} bash -c "set -e; $(declare -f query_file); query_file \"$(db dnim)\" ./schema/{}"