<% '======================================================================================================================= ' MIGRATION: create_graph_tables '======================================================================================================================= ' Phase 4 graph relationships (spec FR-023, section 8 "Entity", "Relationship", section 14, ADR-014): ' entities named concepts of a namespace (type + canonical key unique per namespace), with aliases ' relationships typed, directed edges between entities, memories and documents; proposed -> accepted|rejected ' by review; evidence is required and stored in evidence_links with owner_kind 'relationship' ' evidence_links rebuilt so its owner_kind CHECK also allows 'relationship' (SQLite cannot alter a CHECK) '----------------------------------------------------------------------------------------------------------------------- Sub Migration_Up(migration) migration.ExecuteSQL "CREATE TABLE entities (" & _ "id TEXT PRIMARY KEY, " & _ "tenant_id TEXT NOT NULL REFERENCES tenants(id), " & _ "namespace_id TEXT NOT NULL REFERENCES namespaces(id), " & _ "type TEXT NOT NULL, " & _ "canonical_key TEXT NOT NULL, " & _ "name TEXT NOT NULL, " & _ "aliases TEXT, " & _ "description TEXT, " & _ "properties TEXT, " & _ "created_by TEXT NOT NULL REFERENCES principals(id), " & _ "created_at TEXT NOT NULL, " & _ "updated_at TEXT NOT NULL, " & _ "UNIQUE (namespace_id, type, canonical_key))" migration.CreateIndex "idx_entities_tenant", "entities", "tenant_id, namespace_id" migration.ExecuteSQL "CREATE TABLE relationships (" & _ "id TEXT PRIMARY KEY, " & _ "tenant_id TEXT NOT NULL REFERENCES tenants(id), " & _ "namespace_id TEXT NOT NULL REFERENCES namespaces(id), " & _ "source_kind TEXT NOT NULL CHECK (source_kind IN ('entity','memory','document')), " & _ "source_id TEXT NOT NULL, " & _ "target_kind TEXT NOT NULL CHECK (target_kind IN ('entity','memory','document')), " & _ "target_id TEXT NOT NULL, " & _ "type TEXT NOT NULL, " & _ "description TEXT, " & _ "confidence REAL, " & _ "valid_from TEXT, " & _ "valid_until TEXT, " & _ "inferred INTEGER NOT NULL DEFAULT 0 CHECK (inferred IN (0,1)), " & _ "state TEXT NOT NULL DEFAULT 'proposed' CHECK (state IN ('proposed','accepted','rejected')), " & _ "author_principal_id TEXT NOT NULL REFERENCES principals(id), " & _ "reviewer_principal_id TEXT REFERENCES principals(id), " & _ "review_notes TEXT, " & _ "created_at TEXT NOT NULL, " & _ "reviewed_at TEXT)" migration.CreateIndex "idx_relationships_source", "relationships", "tenant_id, source_kind, source_id" migration.CreateIndex "idx_relationships_target", "relationships", "tenant_id, target_kind, target_id" migration.CreateIndex "idx_relationships_namespace", "relationships", "tenant_id, namespace_id, state" ' At most one live (proposed or accepted) edge per namespace, endpoints and type migration.ExecuteSQL "CREATE UNIQUE INDEX idx_relationships_live ON relationships (namespace_id, source_kind, source_id, target_kind, target_id, type) " & _ "WHERE state IN ('proposed','accepted')" RebuildEvidenceLinks migration, "'memory','candidate','relationship'" End Sub '----------------------------------------------------------------------------------------------------------------------- ' DOWN - Rollback the migration '----------------------------------------------------------------------------------------------------------------------- Sub Migration_Down(migration) migration.ExecuteSQL "DELETE FROM evidence_links WHERE owner_kind = 'relationship'" RebuildEvidenceLinks migration, "'memory','candidate'" migration.ExecuteSQL "DROP INDEX idx_relationships_live" migration.DropIndex "idx_relationships_namespace", "relationships" migration.DropIndex "idx_relationships_target", "relationships" migration.DropIndex "idx_relationships_source", "relationships" migration.DropTable "relationships" migration.DropIndex "idx_entities_tenant", "entities" migration.DropTable "entities" End Sub ' Recreates evidence_links with the given owner kinds, keeping every row and both indexes. Sub RebuildEvidenceLinks(migration, ownerKinds) migration.ExecuteSQL "CREATE TABLE evidence_links_new (" & _ "id TEXT PRIMARY KEY, " & _ "tenant_id TEXT NOT NULL REFERENCES tenants(id), " & _ "owner_kind TEXT NOT NULL CHECK (owner_kind IN (" & ownerKinds & ")), " & _ "owner_id TEXT NOT NULL, " & _ "owner_revision INTEGER, " & _ "target_kind TEXT NOT NULL CHECK (target_kind IN ('experience','chunk','memory','document')), " & _ "target_id TEXT NOT NULL, " & _ "target_revision INTEGER, " & _ "role TEXT NOT NULL DEFAULT 'supports' CHECK (role IN ('supports','contradicts','context')), " & _ "excerpt TEXT, " & _ "created_by TEXT NOT NULL REFERENCES principals(id), " & _ "created_at TEXT NOT NULL)" migration.ExecuteSQL "INSERT INTO evidence_links_new (id, tenant_id, owner_kind, owner_id, owner_revision, target_kind, target_id, target_revision, role, excerpt, created_by, created_at) " & _ "SELECT id, tenant_id, owner_kind, owner_id, owner_revision, target_kind, target_id, target_revision, role, excerpt, created_by, created_at FROM evidence_links" migration.ExecuteSQL "DROP TABLE evidence_links" migration.ExecuteSQL "ALTER TABLE evidence_links_new RENAME TO evidence_links" migration.CreateIndex "idx_evidence_owner", "evidence_links", "owner_kind, owner_id, owner_revision" migration.CreateIndex "idx_evidence_tenant", "evidence_links", "tenant_id" End Sub %>