forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

prepare_db.sh44 lines, 1.7 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# Refresh the sqlx "prepare" database: the ONE database whose schema is the
3# union of migrations/control/*.sql + migrations/school/*.sql, against which
4# sqlx's compile-time query macros check every query (`cargo sqlx prepare`,
5# and plain `cargo check` with DATABASE_URL set — see .env.example).
6#
7# DISJOINTNESS INVARIANT: control tables (school, builder, builder_session,
8# rate_limit) and school tables must never collide on a name — the union is
9# applied to this single database. The loop below fails loudly (psql
10# ON_ERROR_STOP=1) if a name ever collides.
11#
12# Raw apply, on purpose: no `sqlx migrate run`, so no `_sqlx_migrations`
13# tracking rows exist and the control and school file sets never share a
14# tracking table. The database is dropped and recreated on every run, so the
15# result is deterministic.
16#
17# Requires the compose Postgres: `podman compose up -d postgres`.
18set -euo pipefail
19
20CONTAINER="${HEZARFEN_PG_CONTAINER:-hezarfen_backend_postgres}"
21PREPARE_DB="${HEZARFEN_PREPARE_DB:-hezarfen_sqlx_prepare}"
22HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
23
24psql() {
25 podman exec -i "$CONTAINER" psql -U hezarfen -v ON_ERROR_STOP=1 "$@"
26}
27
28echo "== refreshing $PREPARE_DB on $CONTAINER"
29psql -d postgres -q <<SQL
30DROP DATABASE IF EXISTS $PREPARE_DB;
31CREATE DATABASE $PREPARE_DB;
32SQL
33
34applied=0
35for dir in control school; do
36 for file in "$HERE"/migrations/"$dir"/*.sql; do
37 echo "== applying migrations/$dir/$(basename "$file")"
38 psql -d "$PREPARE_DB" -q -f - < "$file"
39 applied=$((applied + 1))
40 done
41done
42
43echo "== applied $applied migration files; tables now in $PREPARE_DB:"
44psql -d "$PREPARE_DB" -c '\dt'