29 lines
832 B
Bash
Executable File
29 lines
832 B
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
|
|
: "${PGHOST:?required}"
|
|
: "${PGUSER:?required}"
|
|
: "${PGDATABASE:?required}"
|
|
: "${PGPASSWORD:?required}"
|
|
|
|
psql -v ON_ERROR_STOP=1 -c '
|
|
CREATE TABLE IF NOT EXISTS schema_migrations (
|
|
version text PRIMARY KEY,
|
|
applied_at timestamptz NOT NULL DEFAULT now()
|
|
)
|
|
'
|
|
|
|
for migration in /migrations/*.up.sql; do
|
|
version=$(basename "$migration" .up.sql)
|
|
case "$version" in
|
|
*[!0-9A-Za-z_-]*) echo "invalid migration name: $version" >&2; exit 1 ;;
|
|
esac
|
|
applied=$(psql -v ON_ERROR_STOP=1 -Atc "SELECT count(*) FROM schema_migrations WHERE version='$version'")
|
|
[ "$applied" = 1 ] && continue
|
|
{
|
|
printf 'BEGIN;\nSELECT pg_advisory_xact_lock(731006);\n'
|
|
cat "$migration"
|
|
printf "\nINSERT INTO schema_migrations(version) VALUES ('%s');\nCOMMIT;\n" "$version"
|
|
} | psql -v ON_ERROR_STOP=1
|
|
done
|