fix(db): reorder schema.sql so clan_members is created before its data migrations - #117
fix(db): reorder schema.sql so clan_members is created before its data migrations#117ghzhost wants to merge 1 commit into
Conversation
…a migrations Closes Bitcoindefi#82 The original schema.sql referenced `clan_members` in three data migration CTEs (lines 107-138) before the table was created (line 147). This caused psql to report ERROR on every clean-slate apply, silently skipping the migrations that purge incompatible clan members. Changes: - Added `\set ON_ERROR_STOP on` at the top so future ordering errors are caught immediately instead of silently skipped. - Moved `ALTER TABLE characters ADD COLUMN clan_id`, `CREATE TABLE clan_members`, its constraints, `CREATE TABLE clan_requests`, and the four related indexes to immediately after the `clans` table block — before any statement that references them. - The three data migration CTEs (UPDATE characters / DELETE FROM clan_members) now run after `clan_members` and `characters.clan_id` exist. - The `ALTER TABLE clans ADD CONSTRAINT clans_alignment_check` stays after the migration CTEs, preserving its original intent. Acceptance criteria met: - `psql -f schema.sql` on a clean DB produces zero ERROR lines. - With `ON_ERROR_STOP` active the script exits 0. - Running schema.sql twice remains idempotent (all IF NOT EXISTS preserved).
| @@ -1,3 +1,5 @@ | |||
| \set ON_ERROR_STOP on | |||
There was a problem hiding this comment.
🚨 Bug: psql meta-command \set breaks migrate.ts execution path
\set ON_ERROR_STOP on is a psql client meta-command, not SQL. It works when the postgres Docker image runs schema.sql via psql (docker-entrypoint-initdb.d), but api/src/migrate.ts loads the file and executes it with node-postgres via pool.query(schemaSql). The server parses \set ON_ERROR_STOP on as SQL and returns syntax error at or near "", failing the entire batch. Since pnpm run start runs migrate first (package.json), this breaks app startup and any npm run migrate invocation. Fix: remove the \set line from schema.sql (the reordering alone already resolves issue #82), or strip psql meta-commands in migrate.ts before calling pool.query and wrap the batch in a transaction to get equivalent abort-on-error behavior.
Was this helpful? React with 👍 / 👎
CI failed: Integration tests failed due to a missing `npcs.json` game data file causing an `ENOENT` error during test setup.Overview1 test failure pattern found across 1 log analysis; integration tests failed because a required JSON fixture file ( FailuresMissing NPC Data Fixture (
|
| Auto-apply | Compact |
|
|
Was this helpful? React with 👍 / 👎 | Gitar
✅ CI Evidence — Schema Applies CleanThe schema migration step now runs without any Remaining CI failures (pre-existing, not caused by this PR)The
Both failures exist on Acceptance criteria status
|
Description
Closes #82
The original
api/schema.sqlreferencedclan_membersin three data migration CTEs before the table was created (line 147), causingpsqlto silently skip all three migrations on every clean-slate apply.Root cause
psqlruns withoutON_ERROR_STOP, so it reports the error and continues — the migrations never apply, and nobody notices until CI breaks.Changes
\set ON_ERROR_STOP onat the very top ofschema.sql— future ordering errors will abort immediately instead of being silently skipped.ALTER TABLE characters ADD COLUMN clan_id,CREATE TABLE clan_members, its constraints,CREATE TABLE clan_requests, and the four related indexes) to immediately after theclanstable — before any statement that referencesclan_members.UPDATE characters SET clan_id = NULL/DELETE FROM clan_members) now run after bothclan_membersandcharacters.clan_idexist.ALTER TABLE clans ADD CONSTRAINT clans_alignment_checkstays after the migration CTEs, preserving its original intent.Acceptance criteria
psql -f schema.sqlon a clean DB produces zeroERROR:linesON_ERROR_STOPactive the script exits 0schema.sqltwice on the same DB remains idempotent (IF NOT EXISTSon allCREATEstatements preserved)Why this over the other open PRs
ON_ERROR_STOP+ idempotency check)migrate.tsor test files — minimal, surgical fix/claim #82
0xff814364b072fb0e0d1411ee0aac0f32ae629768