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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix(catalog): rebuild identity read views when their base table gains a column (#12766)
[._.]/523d7021fdbe
diff --git a/server/lib/db/schema/catalogIdentityViews.js b/server/lib/db/schema/catalogIdentityViews.jsindex 2b63da6525..9e7054673a 100644--- a/server/lib/db/schema/catalogIdentityViews.js+++ b/server/lib/db/schema/catalogIdentityViews.js@@ -9,6 +9,25 @@ const ASSOCIATIONS = {revisions: ['catalog_ingredient_revisions', ['id']],};+// The views project `(r).*` of their base table, so a column added to that+// table (e.g. catalog_ingredient_media.library, #12762) shifts the view's+// column list; CREATE OR REPLACE VIEW can only APPEND columns, and boot failed+// with "cannot change name of view column". Drop a view whose base table has a+// column the view lacks so the CREATE below rebuilds it. Nothing depends on these+// views (read paths query them directly), and a current view is left untouched,+// so this is idempotent on every boot and a no-op on a fresh database.+const viewDriftDropSql = (kind, table) => `DO $$ BEGIN+ IF to_regclass('catalog_identity_read_${kind}') IS NOT NULL AND EXISTS (+ SELECT column_name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = '${table}'+ EXCEPT+ SELECT column_name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = 'catalog_identity_read_${kind}'+ ) THEN+ DROP VIEW catalog_identity_read_${kind};+ END IF;+ END $$`;++export const buildCatalogIdentityViewsPreludeDdl = () => Object.entries(ASSOCIATIONS).map(([kind, [table]]) => viewDriftDropSql(kind, table));+export const buildCatalogIdentityViewsDdl = () => Object.entries(ASSOCIATIONS).map(([kind, [table, keys]]) => {const sourceMatch = kind === 'relations'? '(live.from_id = o.source_id OR live.to_id = o.source_id)'diff --git a/server/lib/db/schema/catalogIdentityViews.test.js b/server/lib/db/schema/catalogIdentityViews.test.jsnew file mode 100644index 0000000000..44058dc04c--- /dev/null+++ b/server/lib/db/schema/catalogIdentityViews.test.js@@ -0,0 +1,28 @@+import { describe, it, expect } from 'vitest';+import { buildCatalogIdentityViewsDdl, buildCatalogIdentityViewsPreludeDdl } from './catalogIdentityViews.js';+import { buildCatalogDdl } from './index.js';++// Regression for the #12762 boot crash: adding catalog_ingredient_media.library+// shifted the `(r).*` column list of catalog_identity_read_media and CREATE OR+// REPLACE VIEW refused ("cannot change name of view column").+describe('catalog identity view drift prelude', () => {+ it('guards every identity read view with a conditional drop', () => {+ const views = buildCatalogIdentityViewsDdl().map((sql) => /^CREATE OR REPLACE VIEW ([a-z_]+)/.exec(sql)[1]);+ const prelude = buildCatalogIdentityViewsPreludeDdl();+ expect(prelude).toHaveLength(views.length);+ views.forEach((view, i) => {+ expect(prelude[i]).toContain(`to_regclass('${view}')`);+ expect(prelude[i]).toContain(`DROP VIEW ${view};`);+ expect(prelude[i]).toContain('EXCEPT');+ });+ expect(prelude.find((sql) => sql.includes('catalog_identity_read_media'))).toContain("table_name = 'catalog_ingredient_media'");+ });++ it('runs the prelude before the views in ensureSchema', () => {+ const ddl = buildCatalogDdl();+ const dropAt = ddl.findIndex((sql) => sql.includes('DROP VIEW catalog_identity_read_media;'));+ const createAt = ddl.findIndex((sql) => sql.startsWith('CREATE OR REPLACE VIEW catalog_identity_read_media'));+ expect(dropAt).toBeGreaterThanOrEqual(0);+ expect(dropAt).toBeLessThan(createAt);+ });+});diff --git a/server/lib/db/schema/index.js b/server/lib/db/schema/index.jsindex e32ad2368b..4e5a88d6cb 100644--- a/server/lib/db/schema/index.js+++ b/server/lib/db/schema/index.js@@ -27,7 +27,7 @@ import { aiGraphDdl } from './aiGraph.js';import { peerExecutionDdl } from './peerExecution.js';import { catalogDdl, catalogUserTypesDdl, catalogPendingAppliesDdl } from './catalog.js';import { catalogIdentityDdl } from './catalogIdentity.js';-import { buildCatalogIdentityViewsDdl } from './catalogIdentityViews.js';+import { buildCatalogIdentityViewsDdl, buildCatalogIdentityViewsPreludeDdl } from './catalogIdentityViews.js';import { mediaDdl } from './media.js';import { universesDdl } from './universes.js';import { libraryDdl } from './library.js';@@ -59,6 +59,7 @@ export {catalogDdl,catalogIdentityDdl,buildCatalogIdentityViewsDdl,+ buildCatalogIdentityViewsPreludeDdl,catalogUserTypesDdl,catalogPendingAppliesDdl,mediaDdl,@@ -112,6 +113,7 @@ export function buildCatalogDdl() {return [...catalogDdl,...catalogIdentityDdl,+ ...buildCatalogIdentityViewsPreludeDdl(),...buildCatalogIdentityViewsDdl(),...mediaDdl,...catalogUserTypesDdl,diff --git a/server/lib/db/schema/schema.test.js b/server/lib/db/schema/schema.test.jsindex 06078a4777..c71496457c 100644--- a/server/lib/db/schema/schema.test.js+++ b/server/lib/db/schema/schema.test.js@@ -62,6 +62,7 @@ describe('db/schema barrel + composer (#2832)', () => {const expected = [...schema.catalogDdl,...schema.catalogIdentityDdl,+ ...schema.buildCatalogIdentityViewsPreludeDdl(),...schema.buildCatalogIdentityViewsDdl(),...schema.mediaDdl,...schema.catalogUserTypesDdl,diff --git a/server/scripts/init-db.sql b/server/scripts/init-db.sqlindex 7d11824b80..e1227cef81 100644--- a/server/scripts/init-db.sql+++ b/server/scripts/init-db.sql@@ -2632,6 +2632,53 @@ CREATE TRIGGER trg_catalog_identity_member BEFORE INSERT OR UPDATE OR DELETE ONDROP TRIGGER IF EXISTS trg_catalog_identity_member ON catalog_ingredient_revisions;CREATE TRIGGER trg_catalog_identity_member BEFORE INSERT OR UPDATE OR DELETE ON catalog_ingredient_revisionsFOR EACH ROW EXECUTE FUNCTION guard_catalog_identity_member();+-- Drop identity read views whose base table gained a column (CREATE OR REPLACE+-- VIEW can only append columns; see catalogIdentityViews.js).+DO $$ BEGIN+ IF to_regclass('catalog_identity_read_sources') IS NOT NULL AND EXISTS (+ SELECT column_name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = 'catalog_ingredient_sources'+ EXCEPT+ SELECT column_name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = 'catalog_identity_read_sources'+ ) THEN+ DROP VIEW catalog_identity_read_sources;+ END IF;+ END $$;+DO $$ BEGIN+ IF to_regclass('catalog_identity_read_refs') IS NOT NULL AND EXISTS (+ SELECT column_name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = 'catalog_ingredient_refs'+ EXCEPT+ SELECT column_name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = 'catalog_identity_read_refs'+ ) THEN+ DROP VIEW catalog_identity_read_refs;+ END IF;+ END $$;+DO $$ BEGIN+ IF to_regclass('catalog_identity_read_media') IS NOT NULL AND EXISTS (+ SELECT column_name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = 'catalog_ingredient_media'+ EXCEPT+ SELECT column_name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = 'catalog_identity_read_media'+ ) THEN+ DROP VIEW catalog_identity_read_media;+ END IF;+ END $$;+DO $$ BEGIN+ IF to_regclass('catalog_identity_read_relations') IS NOT NULL AND EXISTS (+ SELECT column_name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = 'catalog_ingredient_relations'+ EXCEPT+ SELECT column_name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = 'catalog_identity_read_relations'+ ) THEN+ DROP VIEW catalog_identity_read_relations;+ END IF;+ END $$;+DO $$ BEGIN+ IF to_regclass('catalog_identity_read_revisions') IS NOT NULL AND EXISTS (+ SELECT column_name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = 'catalog_ingredient_revisions'+ EXCEPT+ SELECT column_name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = 'catalog_identity_read_revisions'+ ) THEN+ DROP VIEW catalog_identity_read_revisions;+ END IF;+ END $$;CREATE OR REPLACE VIEW catalog_identity_read_sources ASWITH active AS (SELECT * FROM catalog_identity_operations WHERE state IN ('committed', 'recovery_prepared')),candidates AS (