db/scripts/build.sh

42 lines
723 B
Bash
Raw Normal View History

2023-06-09 00:38:49 +00:00
#! /usr/bin/bash
2023-06-09 01:29:13 +00:00
source ./scripts/env.sh
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-09 01:29:13 +00:00
git reset "$rev" --quiet --hard
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/$POSTGRES_DB
psql \
--quiet \
$url \
--command="drop schema public cascade; create schema public;" \
1>/dev/null
2023-06-09 01:48:47 +00:00
ls ./schema/ | xargs -I{} \
2023-06-09 01:29:13 +00:00
psql \
--quiet \
$url \
2023-06-09 01:48:47 +00:00
--file=./schema/{} \
2023-06-09 01:29:13 +00:00
1>/dev/null