db/scripts/build.sh

42 lines
777 B
Bash
Raw Normal View History

#! /bin/bash
2023-06-09 00:38:49 +00:00
2023-06-27 23:43:04 +00:00
set -ex
source ./scripts/env.sh ./.env.schema
2023-06-09 01:29:13 +00:00
2023-06-09 00:38:49 +00:00
rev=${1:-HEAD}
if [[ -n $(git status --porcelain) ]]; then
2023-06-09 01:29:13 +00:00
echo "git working tree dirty" 1>&2;
2023-06-09 00:38:49 +00:00
exit 1;
fi
if [[ "$rev" = "HEAD" ]]; then
head=""
port=5432
else
head=$(git show --format=format:%h -q)
2023-06-27 23:43:04 +00:00
git reset --quiet --hard "$rev"
2023-06-09 00:38:49 +00:00
port=5433
fi
2023-06-09 01:29:13 +00:00
switch_back_to_head() {
if [[ -n "$head" ]]; then
git reset --quiet --hard "$head"
fi
}
trap 'switch_back_to_head' EXIT
url=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:$port
2023-06-09 01:29:13 +00:00
dropdb=$(mktemp)
echo "drop database dnim with (force); create database dnim;" > "$dropdb"
psql --quiet "$url/postgres" --file="$dropdb" 1>/dev/null
2023-06-09 01:29:13 +00:00
2023-06-09 01:48:47 +00:00
ls ./schema/ | xargs -I{} \
2023-06-09 01:29:13 +00:00
psql \
--quiet \
2023-06-27 23:43:04 +00:00
"$url/dnim" \
2023-06-09 01:48:47 +00:00
--file=./schema/{} \
2023-06-09 01:29:13 +00:00
1>/dev/null