db/scripts/build.sh
2023-07-18 22:06:57 -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 [[ "$base_or_head" = "head" ]]; then
port=5433
else
port=5432
fi
docker compose down "$base_or_head" &>/dev/null
url="postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:$port"
isready() {
until pg_isready -p "$port" 1>/dev/null; do true; done;
}
initdb() {
dropdb=$(mktemp)
echo "drop database dnim with (force);" > "$dropdb"
psql --quiet "$url/postgres" -f "$dropdb" &>/dev/null || true
psql --quiet "$url/postgres" -c "create database dnim" &>/dev/null
}
if [[ "$rev" = "empty" ]]; then
docker compose up -d "$base_or_head" &>/dev/null
isready
initdb
exit 0
fi
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"
docker compose up -d "$base_or_head" &>/dev/null
cd "../$base_or_head"
isready
initdb
ls ./schema/ | xargs -I{} bash -c "set -e; $(declare -f query_file); query_file \"$url/dnim\" ./schema/{} 2>/dev/null"