2023-06-18 15:52:44 +00:00
|
|
|
#! /bin/bash
|
2023-06-10 04:51:28 +00:00
|
|
|
|
2023-07-03 00:40:19 +00:00
|
|
|
set -e
|
2023-06-10 04:51:28 +00:00
|
|
|
|
2023-07-17 19:44:40 +00:00
|
|
|
source ./scripts/common.sh
|
2023-06-11 21:43:53 +00:00
|
|
|
source ./scripts/env.sh ./.env
|
|
|
|
|
|
|
|
if [[ -n $(git status --porcelain) ]]; then
|
|
|
|
echo "git working tree dirty" 1>&2;
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
|
2023-07-15 22:22:40 +00:00
|
|
|
to_tag="$1"
|
2023-06-11 21:43:53 +00:00
|
|
|
|
|
|
|
get_dnim_database_count="copy (select count(*) from pg_database where datname = 'dnim') to stdout with null as '';"
|
2023-07-17 19:44:40 +00:00
|
|
|
dnim_database_count=$(query "$POSTGRES_URI/postgres" "$get_dnim_database_count")
|
2023-06-11 21:43:53 +00:00
|
|
|
|
|
|
|
if [[ "$dnim_database_count" = "0" ]]; then
|
|
|
|
echo "fresh database"
|
2023-07-17 19:44:40 +00:00
|
|
|
query "$POSTGRES_URI/postgres" "create database dnim;"
|
|
|
|
ls ./schema/ | xargs -I{} bash -c "$(declare -f query_file); query_file \"$POSTGRES_URI/dnim\" ./schema/{}"
|
2023-07-17 03:47:16 +00:00
|
|
|
rev_last='empty'
|
2023-06-11 21:43:53 +00:00
|
|
|
script=''
|
|
|
|
else
|
2023-07-17 03:47:16 +00:00
|
|
|
get_rev_last="copy (select to_revision from migration order by performed_on desc limit 1) to stdout with null as '';"
|
2023-07-17 19:44:40 +00:00
|
|
|
rev_last=$(query "$POSTGRES_URI/dnim" "$get_rev_last")
|
2023-07-17 03:47:16 +00:00
|
|
|
migration=$(./scripts/diff.sh "$rev_last" "$to_tag")
|
2023-06-11 21:43:53 +00:00
|
|
|
|
2023-07-15 22:22:40 +00:00
|
|
|
if [[ "$2" = "--greenlight" ]]; then
|
2023-07-17 19:44:40 +00:00
|
|
|
query_file "$POSTGRES_URI/dnim" "$migration"
|
2023-06-11 21:43:53 +00:00
|
|
|
else
|
2023-07-17 03:47:16 +00:00
|
|
|
echo "migration available at $migration"
|
2023-07-03 00:40:19 +00:00
|
|
|
echo "review and rerun with --greenlight to apply"
|
2023-06-11 21:43:53 +00:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-07-17 03:47:16 +00:00
|
|
|
insert_migration="insert into migration (from_revision, to_revision) values ('$rev_last', '$to_tag');"
|
2023-07-17 19:44:40 +00:00
|
|
|
query "$POSTGRES_URI/dnim" "$insert_migration"
|
2023-06-11 21:43:53 +00:00
|
|
|
echo "inserted migration"
|