Skip to content

refactor: split ensureSchemaImpl DDL into per-domain db/schema modules#2858

Merged
atomantic merged 3 commits into
mainfrom
next/issue-2832
Jul 22, 2026
Merged

refactor: split ensureSchemaImpl DDL into per-domain db/schema modules#2858
atomantic merged 3 commits into
mainfrom
next/issue-2832

Conversation

@atomantic

Copy link
Copy Markdown
Owner

Summary

Splits the ~1265-line inline DDL in ensureSchemaImpl() (server/lib/db.js) into per-domain modules under server/lib/db/schema/, mirroring the arcPlanner.jsarcPlanner/* split from #1152. ensureSchemaImpl becomes a thin composer:

const upgrades = buildUpgradeDdl();   // core, tribe, humanActivity, commissions
const catalogDDL = buildCatalogDdl(); // catalog, media, universes, library, pipeline, writersRoom, lora, privacy, audit + triggers

Each domain module (core, tribe, humanActivity, commissions, catalog, media, universes, library, pipeline, writersRoom, lora, privacy, audit) exports a plain statement array; db/schema/index.js re-exports them and composes the two ordered lists. db.js drops from 1632 to 414 lines.

No behavior change — byte-identical. The modules were extracted verbatim (statement text and order unchanged), so the composed schema is identical to the pre-split version. This was verified programmatically against the arrays from the parent commit: 28 upgrade + 181 catalog statements match exactly. Order is load-bearing (FK references, the record_audit_log() function preceding its triggers, and catalog_user_types' original post-media position) and is preserved by the composer.

catalog_user_types lives in catalog.js as a separate export (catalogUserTypesDdl) because in the original array it sits after the media block, not adjacent to the other catalog tables — the composer threads it back into that exact position.

The db.catalogDdlParity.test.js and catalogTypes.test.js text-scan tests now read the schema-module sources (the DDL literals moved out of db.js); their set/membership assertions are unchanged.

New db/schema/README.md (module catalog) and db/schema/schema.test.js (composer contract: array shapes, compose order, FK-target-precedes-reference, function-before-triggers, audit-trigger pairs).

Test plan

  • server/lib/db/schema/schema.test.js, db.catalogDdlParity.test.js, catalogTypes.test.js, db.guards.test.js, lib/index.test.js — all pass.
  • Full server suite: 1009 files passed, 20832 tests passed, 24 db-backed suites self-skipped on the non-test portos DB (guard held). Zero failures.
  • Byte-identical composed DDL verified against the pre-split arrays (28 + 181 statements match).
  • Reviewed with claude, codex (ran the full suite itself, confirmed statement + order match), and ollama (local coding model) — all clean; ollama's findings were pre-existing verbatim SQL or false-positive ordering concerns, dismissed.

Closes #2832

…/schema modules

Extract the ~1265-line inline DDL in ensureSchemaImpl() (server/lib/db.js)
into per-domain modules under server/lib/db/schema/, each exporting a plain
statement array; ensureSchemaImpl becomes a thin composer that runs
buildUpgradeDdl() then buildCatalogDdl(). Statement text and order are
byte-identical to the pre-split arrays (verified: 28 upgrade + 181 catalog
statements match exactly), so the composed schema is unchanged.

The catalog-DDL parity and catalogTypes text-scan tests now read the schema
module sources (the DDL literals moved out of db.js). Mirrors the
arcPlanner.js -> arcPlanner/* split (#1152).

Closes #2832
@atomantic
atomantic merged commit 32d5c2d into main Jul 22, 2026
3 of 4 checks passed
@atomantic
atomantic deleted the next/issue-2832 branch July 22, 2026 00:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Split ensureSchemaImpl() in server/lib/db.js into per-domain DDL modules

1 participant