Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

feat(catalog): relax type CHECK to app-layer validation (decision: option b)

[._.]/ Adam Eivy727ad5bf68f9
diff --git a/server/lib/db.catalogDdlParity.test.js b/server/lib/db.catalogDdlParity.test.js
index 50b789d141..953fca5869 100644
--- a/server/lib/db.catalogDdlParity.test.js
+++ b/server/lib/db.catalogDdlParity.test.js
@@ -150,15 +150,17 @@ describe('catalog DDL parity (init-db.sql ↔ db.js ensureSchema)', () => {
expect(sqlTrgs.size).toBeGreaterThan(0);
});
- it('catalog_ingredients type CHECK list matches', () => {
- // The init-db.sql CREATE TABLE block carries the CHECK; the db.js
- // version embeds it inline in the same CREATE. The parity guard catches
- // either side adding a 7th type without updating the other.
- const sqlSet = extractTypeCheckSet(INIT_SQL);
- const jsSet = extractTypeCheckSet(DB_JS);
- expect(sqlSet, 'init-db.sql is missing the type CHECK').toBeTruthy();
- expect(jsSet, 'db.js is missing the type CHECK').toBeTruthy();
- expect([...sqlSet].sort()).toEqual([...jsSet].sort());
+ it('catalog_ingredients type is app-layer-gated (no hardcoded CHECK in either file)', () => {
+ // The legacy `CHECK (type IN (...))` was dropped — valid types are gated at
+ // the app layer via the INGREDIENT_TYPES registry + Zod enum, so a new type
+ // is a registry entry, not a two-file constraint migration. Assert NEITHER
+ // file reintroduces a hardcoded `type IN (...)` CHECK (a one-sided re-add
+ // would drift the fresh-install and upgrade paths apart again), and that
+ // both declare the widened VARCHAR(32) column.
+ expect(extractTypeCheckSet(INIT_SQL), 'init-db.sql reintroduced a hardcoded type CHECK').toBeNull();
+ expect(extractTypeCheckSet(DB_JS), 'db.js reintroduced a hardcoded type CHECK').toBeNull();
+ expect(/type VARCHAR\(32\)/i.test(extractCreateTable(INIT_SQL, 'catalog_ingredients'))).toBe(true);
+ expect(/type VARCHAR\(32\)/i.test(extractCreateTable(DB_JS, 'catalog_ingredients'))).toBe(true);
});
it('search_tsv payload field set matches', () => {
diff --git a/server/lib/db.js b/server/lib/db.js
index 489a9892c0..e1ef2103b8 100644
--- a/server/lib/db.js
+++ b/server/lib/db.js
@@ -141,8 +141,12 @@ export async function ensureSchema() {
`CREATE TABLE IF NOT EXISTS catalog_ingredients (
id TEXT PRIMARY KEY,
- type VARCHAR(20) NOT NULL
- CHECK (type IN ('character', 'place', 'object', 'idea', 'scene', 'concept')),
+ -- No DB CHECK on \`type\`: valid types are gated at the app layer via the
+ -- INGREDIENT_TYPES registry (catalogTypes.js / catalogValidation.js Zod
+ -- enum), so a new system or user-defined type needs no constraint migration.
+ -- VARCHAR(32) leaves headroom for longer type ids. The DROP CONSTRAINT +
+ -- widen for existing installs runs in the idempotent ALTER block below.
+ type VARCHAR(32) NOT NULL,
name TEXT NOT NULL,
payload JSONB NOT NULL DEFAULT '{}'::jsonb,
tags TEXT[] DEFAULT '{}',
@@ -155,6 +159,14 @@ export async function ensureSchema() {
deleted_at TIMESTAMPTZ,
sync_sequence BIGSERIAL
)`,
+ // Relax the legacy `type` CHECK on existing installs: types are now gated at
+ // the app layer (INGREDIENT_TYPES registry + Zod enum), so a new system or
+ // user-defined type doesn't need a DROP/RE-ADD constraint migration. Postgres
+ // auto-named the inline CHECK `catalog_ingredients_type_check`. Both statements
+ // are idempotent — DROP IF EXISTS no-ops once gone; the column-type widen
+ // no-ops when already VARCHAR(32).
+ `ALTER TABLE catalog_ingredients DROP CONSTRAINT IF EXISTS catalog_ingredients_type_check`,
+ `ALTER TABLE catalog_ingredients ALTER COLUMN type TYPE VARCHAR(32)`,
// Postgres can't ALTER the expression of a STORED generated column, so when
// the v2 expansion needs to land we DROP and re-ADD `search_tsv`. The
// conditional below (executed after the table CREATE, before the
diff --git a/server/scripts/init-db.sql b/server/scripts/init-db.sql
index 111e0f4fac..96704ca3dc 100644
--- a/server/scripts/init-db.sql
+++ b/server/scripts/init-db.sql
@@ -154,8 +154,12 @@ CREATE INDEX IF NOT EXISTS idx_catalog_scraps_origin_instance ON catalog_scraps
-- produce identical records. Idea/scene/concept payloads are lighter shapes.
CREATE TABLE IF NOT EXISTS catalog_ingredients (
id TEXT PRIMARY KEY, -- 'cat-chr-<uuid>', 'cat-plc-<uuid>', etc.
- type VARCHAR(20) NOT NULL
- CHECK (type IN ('character', 'place', 'object', 'idea', 'scene', 'concept')),
+ -- No DB CHECK on `type`: valid types are gated at the app layer via the
+ -- INGREDIENT_TYPES registry (server/lib/catalogTypes.js), enforced by the Zod
+ -- enum in catalogValidation.js. Adding a new system (or future user-defined)
+ -- type is then a registry entry, NOT a DROP/RE-ADD constraint migration in two
+ -- files. VARCHAR(32) leaves headroom for longer type ids.
+ type VARCHAR(32) NOT NULL,
name TEXT NOT NULL,
payload JSONB NOT NULL DEFAULT '{}'::jsonb,
tags TEXT[] DEFAULT '{}',