From 0e2fbc0e83bae9f0df3acf9626ce4008e613d31a Mon Sep 17 00:00:00 2001 From: Mustafa Gezen Date: Wed, 30 Aug 2023 01:49:00 +0200 Subject: [PATCH] Add script to recreate database and apply migrations --- scripts/recdb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 scripts/recdb diff --git a/scripts/recdb b/scripts/recdb new file mode 100755 index 00000000..1362422b --- /dev/null +++ b/scripts/recdb @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +export PGPASSWORD=postgres +export PGSSLMODE=disable + +DB="$1" +P="$2" +POSTGRES_PORT=${POSTGRES_PORT:-5432} +POSTGRES_HOST=${POSTGRES_HOST:-localhost} +MIG_ONLY=${MIG_ONLY:-0} + +PSQL="psql -U postgres -h $POSTGRES_HOST -p $POSTGRES_PORT" + +if [[ -z "$DB" || -z "$P" ]]; then + echo "usage: recdb "; + exit 1 +fi + +DB_EXISTS="$($PSQL -lqt | cut -d \| -f 1 | grep -qw $DB)" + +if [[ -z ${DB_EXISTS} ]]; then + $PSQL -c "create database $DB;"; +fi + +DATABASE_URL="postgres://postgres:postgres@${POSTGRES_HOST}:${POSTGRES_PORT}/$DB?sslmode=disable" +m="migrate -source file://$P -database ${DATABASE_URL}" + +if [[ $MIG_ONLY -eq 0 ]]; then + $m down -all + $m drop -f +fi +$m up || exit 1 + +#$PSQL -d $DB -a -f seed.sql +#$PSQL -d $DB -a -f *_mock.sql || exit 0