Files
OMT-SM/server/db/init/01_run_migrations.sh

21 lines
874 B
Bash
Executable File

#!/usr/bin/env bash
# Run all SQL migrations from /sql/migrations/ in lexical order.
# On plain Postgres (no pg_cron extension), wrap any bare
# `create extension if not exists pg_cron;` line so it doesn't abort.
set -euo pipefail
TMPDIR_M=/tmp/migrations
mkdir -p "$TMPDIR_M"
echo ">> applying app migrations from /sql/migrations"
for f in /sql/migrations/*.sql; do
base="$(basename "$f")"
# Replace the bare pg_cron extension creation with a soft variant.
sed -E "s|^create extension if not exists pg_cron;|do \$\$ begin create extension if not exists pg_cron; exception when others then raise notice 'pg_cron unavailable, skipping schedules'; end \$\$;|i" "$f" > "$TMPDIR_M/$base"
echo ">> $base"
psql -v ON_ERROR_STOP=1 \
--username "$POSTGRES_USER" \
--dbname "$POSTGRES_DB" \
-f "$TMPDIR_M/$base"
done
echo ">> migrations complete"