db/scripts/build.sh

59 lines
1.3 KiB
Bash
Raw Permalink Normal View History

#! /bin/bash
2023-06-09 00:38:49 +00:00
2023-07-20 05:26:52 +00:00
set -e
2023-06-27 23:43:04 +00:00
2023-07-17 19:44:40 +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_or_head="$1"
rev="$2"
2023-06-09 00:38:49 +00:00
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
2023-07-20 00:03:48 +00:00
db() {
2023-07-20 00:05:36 +00:00
echo -n "postgresql:///$1?port=$port&user=$POSTGRES_USER&password=$POSTGRES_PASSWORD"
2023-07-20 00:03:48 +00:00
}
2023-07-17 19:44:40 +00:00
initdb() {
dropdb=$(mktemp)
echo "drop database dnim with (force);" > "$dropdb"
2023-07-20 00:03:48 +00:00
psql --quiet $(db postgres) -f "$dropdb" || true
psql --quiet $(db postgres) -c "create database dnim"
2023-07-17 19:44:40 +00:00
}
if [[ "$rev" = "empty" ]]; then
2023-07-20 05:51:24 +00:00
docker compose up --wait --wait-timeout 30 -d "$base_or_head"
2023-07-17 19:44:40 +00:00
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
2023-07-17 19:44:40 +00:00
cp docker-compose.yml "./$base_or_head/docker-compose.yml"
cp .env.schema "./$base_or_head/.env.schema"
2023-07-17 19:44:40 +00:00
cd "./$base_or_head"
fi
2023-07-17 03:47:16 +00:00
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/{}"
2023-07-21 17:38:43 +00:00
if [[ -n "$rev" ]]; then
cd ../
fi
2023-07-21 16:35:55 +00:00
2023-07-20 20:20:05 +00:00
query_file $(db dnim) ./migrations/ensure_seeded.sql