diff --git a/.github/workflows/deploy-to-vm.yaml b/.github/workflows/deploy-to-vm.yaml index e1f5e80..47c1111 100644 --- a/.github/workflows/deploy-to-vm.yaml +++ b/.github/workflows/deploy-to-vm.yaml @@ -7,7 +7,10 @@ # sudo npm install -g pnpm@10 # 2. Sudoers entry so the runner can manage the services (sudo visudo): # ALL=(root) NOPASSWD: /usr/bin/install, /usr/bin/tee, /usr/bin/systemctl -# 3. PostgreSQL on localhost (pnpm db:setup, see README). +# 3. PostgreSQL on localhost (pnpm db:setup, see README). Deploys apply the +# Prisma migrations in packages/db/prisma/migrations. A database that was +# created with `prisma db push` must be baselined once with +# `pnpm db:baseline` before the first migration-based deploy (see README). # 4. Apache reverse proxy so the frontend is reachable at [baseurl]/project2 # (the Nitro server already serves the app under /project2): # sudo a2enmod proxy_http @@ -92,8 +95,8 @@ jobs: - name: Install dependencies in deploy root run: pnpm -C "$DEPLOY_ROOT" install --frozen-lockfile - - name: Apply database schema - run: pnpm -C "$DEPLOY_ROOT" --filter @t09-project-2/db run db:push + - name: Apply database migrations + run: pnpm -C "$DEPLOY_ROOT" --filter @t09-project-2/db run db:deploy - name: Install and restart services run: | diff --git a/README.md b/README.md index 5cec5bb..a690cdb 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ cp apps/backend/.env.example apps/backend/.env # create the database schema and optionally seed it pnpm db:generate -pnpm db:push +pnpm db:deploy pnpm db:seed # run the frontend and backend together @@ -74,20 +74,22 @@ Frontend: http://localhost:3000 ยท GraphQL API: http://localhost:3001 ### Commands -| Command | What it does | -| ---------------------------------------------- | ------------------------------------------------------ | -| `pnpm install` / `pnpm install:all` / `pnpm i` | Install dependencies in all workspaces | -| `pnpm dev` | Run frontend and backend concurrently with live reload | -| `pnpm build` | Build all workspaces that define a build | -| `pnpm typecheck` | Typecheck all workspaces | -| `pnpm lint` / `pnpm format` / `pnpm check` | Lint, auto-fix format, and check formatting | -| `pnpm db:setup` | Provision PostgreSQL natively on Ubuntu (see below) | -| `pnpm db:generate` | Regenerate the Prisma client and Pothos types | -| `pnpm db:push` | Push the Prisma schema to the database | -| `pnpm db:migrate` | Create/apply a development migration | -| `pnpm db:seed` | Seed the database | -| `pnpm db:studio` | Open Prisma Studio | -| `pnpm generate-routes` | Regenerate the TanStack Router route tree | +| Command | What it does | +| ---------------------------------------------- | ------------------------------------------------------- | +| `pnpm install` / `pnpm install:all` / `pnpm i` | Install dependencies in all workspaces | +| `pnpm dev` | Run frontend and backend concurrently with live reload | +| `pnpm build` | Build all workspaces that define a build | +| `pnpm typecheck` | Typecheck all workspaces | +| `pnpm lint` / `pnpm format` / `pnpm check` | Lint, auto-fix format, and check formatting | +| `pnpm db:setup` | Provision PostgreSQL natively on Ubuntu (see below) | +| `pnpm db:generate` | Regenerate the Prisma client and Pothos types | +| `pnpm db:push` | Push the Prisma schema to the database (prototyping) | +| `pnpm db:migrate` | Create/apply a development migration | +| `pnpm db:deploy` | Apply pending migrations (used by the deploy workflow) | +| `pnpm db:baseline` | One-time bootstrap for databases created with `db push` | +| `pnpm db:seed` | Seed the database | +| `pnpm db:studio` | Open Prisma Studio | +| `pnpm generate-routes` | Regenerate the TanStack Router route tree | ### Styling @@ -111,6 +113,12 @@ pnpm check # Runs the prettier formatter in check mode, so will not write to any The deployment of both the backend and frontend is automated using the github action found in `./.github/workflows/deploy-to-vm.yaml` +Deploys apply the Prisma migrations in `./packages/db/prisma/migrations` with `prisma migrate deploy` (via `pnpm db:deploy`). Migrations are the single source of truth for the database, so never change the deployed schema with `pnpm db:push`: it does not run the migration SQL (functions, triggers, ...) and skips the migration history, which leaves the deploy unable to apply those migrations later. + +A database that was created with `pnpm db:push` before migrations were adopted has no `_prisma_migrations` table. Run `pnpm db:baseline` once against it, then `pnpm db:deploy`; after that the deploy workflow keeps it up to date. + +If a migration fails against an existing database, `prisma migrate deploy` refuses to continue until the failed migration is resolved (`prisma migrate resolve`). Inspect `_prisma_migrations` and fix the cause before re-running the deploy. + ## Timeline Below is the timeline for our hand-ins, with the date and commit hash of the most recent commit at the time of hand-in, in case we update the repository between the time of handing in and someone reviewing our code diff --git a/package.json b/package.json index b076ce9..1ba08d8 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,8 @@ "db:generate": "pnpm --filter @t09-project-2/db run db:generate", "db:push": "pnpm --filter @t09-project-2/db run db:push", "db:migrate": "pnpm --filter @t09-project-2/db run db:migrate", + "db:deploy": "pnpm --filter @t09-project-2/db run db:deploy", + "db:baseline": "bash scripts/baseline-db.sh", "db:seed": "pnpm --filter @t09-project-2/db run db:seed", "db:studio": "pnpm --filter @t09-project-2/db run db:studio" }, diff --git a/packages/db/package.json b/packages/db/package.json index fbb0593..3213518 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -9,6 +9,7 @@ "db:generate": "dotenv -e .env -- prisma generate", "db:push": "dotenv -e .env -- prisma db push", "db:migrate": "dotenv -e .env -- prisma migrate dev", + "db:deploy": "dotenv -e .env -- prisma migrate deploy", "db:seed": "dotenv -e .env -- prisma db seed", "db:studio": "dotenv -e .env -- prisma studio", "db:reset": "dotenv -e .env -- prisma migrate reset", diff --git a/scripts/baseline-db.sh b/scripts/baseline-db.sh new file mode 100755 index 0000000..ff3fdce --- /dev/null +++ b/scripts/baseline-db.sh @@ -0,0 +1,77 @@ +#!/usr/bin/env bash +# +# One-time bootstrap for databases that were created with `prisma db push`. +# +# `db push` syncs the schema but never runs migration SQL, so SQL-only +# migrations (functions, triggers, ...) are missing and there is no +# `_prisma_migrations` table. This script marks the already-pushed schema +# migrations as applied, leaving SQL-only migrations pending so that +# `prisma migrate deploy` actually runs them. +# +# It is safe to run more than once: it does nothing once migration history +# exists, and nothing on an empty database (`migrate deploy` handles that). +# +# Usage: pnpm db:baseline + +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$repo_root" + +if [[ -z ${DATABASE_URL:-} ]]; then + DATABASE_URL="$( + sed -n 's/^DATABASE_URL=//p' packages/db/.env | tr -d '"' | head -n1 + )" +fi + +if [[ -z ${DATABASE_URL:-} ]]; then + echo "DATABASE_URL is not set and could not be read from packages/db/.env" >&2 + exit 1 +fi + +export DATABASE_URL + +query() { + psql "$DATABASE_URL" -tAc "$1" +} + +if [[ -n "$(query "select to_regclass('public._prisma_migrations')")" ]]; then + echo "Migration history already exists; nothing to baseline." + exit 0 +fi + +if [[ -z "$(query "select to_regclass('public.venue')")" ]]; then + echo "Empty database detected; prisma migrate deploy will create everything." + exit 0 +fi + +echo "Found a db push-managed schema without migration history; baselining..." + +for migration_dir in packages/db/prisma/migrations/*/; do + migration="$(basename "$migration_dir")" + + # Migrations that only contain regular DDL are already reflected in the + # database, because `db push` synced the whole schema. SQL-only migrations + # are not, so those have to stay pending for `migrate deploy`. + if grep -qiE 'create (or replace )?(function|procedure|trigger)' \ + "$migration_dir/migration.sql"; then + case "$migration" in + *add_haversine_distance_function) + # Leave it pending unless it was already created by hand. + if [[ -n "$(query "select to_regprocedure('haversine_distance(double precision,double precision,double precision,double precision)')")" ]]; then + pnpm --filter @t09-project-2/db exec prisma migrate resolve --applied "$migration" + else + echo "Leaving $migration pending (SQL-only migration)." + fi + ;; + *) + echo "Leaving $migration pending (SQL-only migration)." + ;; + esac + continue + fi + + pnpm --filter @t09-project-2/db exec prisma migrate resolve --applied "$migration" +done + +echo "Baseline complete. Run 'pnpm db:deploy' to apply the SQL-only migrations."