db/scripts/build.sh

65 lines
1.4 KiB
Bash
Raw Normal View History

#! /bin/bash
2023-06-09 00:38:49 +00:00
2023-07-17 03:47:16 +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
2023-07-17 03:47:16 +00:00
if [[ "$base_or_head" = "head" ]]; then
2023-06-09 00:38:49 +00:00
port=5433
2023-07-17 03:47:16 +00:00
else
port=5432
2023-06-09 00:38:49 +00:00
fi
2023-07-19 03:17:11 +00:00
docker compose stop "$base_or_head" 1>/dev/null
2023-07-19 03:24:31 +00:00
docker compose rm -f "$base_or_head" 1>/dev/null
2023-07-17 19:44:40 +00:00
2023-07-19 23:53:13 +00:00
pg_host=${PG_HOST:-localhost}
url="postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@$pg_host:$port"
2023-07-17 19:44:40 +00:00
isready() {
2023-07-19 03:29:40 +00:00
local waited=0
until pg_isready -p "$port" 1>/dev/null; do
if [[ "$waited" = "10" ]]; then
2023-07-19 03:35:46 +00:00
pwd
cat docker-compose.yml
2023-07-19 03:29:40 +00:00
docker compose logs base
exit 1
fi
sleep 1
2023-07-19 03:30:39 +00:00
echo $(( waited++ ))
2023-07-19 03:29:40 +00:00
done;
2023-07-17 19:44:40 +00:00
}
2023-06-09 01:29:13 +00:00
2023-07-17 19:44:40 +00:00
initdb() {
dropdb=$(mktemp)
echo "drop database dnim with (force);" > "$dropdb"
2023-07-19 23:49:37 +00:00
psql --quiet "$url/postgres" -f "$dropdb" || true
psql --quiet "$url/postgres" -c "create database dnim"
2023-07-17 19:44:40 +00:00
}
if [[ "$rev" = "empty" ]]; then
2023-07-19 03:13:08 +00:00
docker compose up -d "$base_or_head" 1>/dev/null
2023-07-17 19:44:40 +00:00
isready
initdb
exit 0
fi
2023-07-19 03:06:57 +00:00
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"
cd "./$base_or_head"
2023-07-19 03:13:08 +00:00
docker compose up -d "$base_or_head" 1>/dev/null
2023-07-17 03:47:16 +00:00
2023-07-17 19:44:40 +00:00
isready
initdb
2023-07-19 23:48:31 +00:00
ls ./schema/ | xargs -I{} bash -c "set -e; $(declare -f query_file); query_file \"$url/dnim\" ./schema/{}"