Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

96 wiersze
5.8KB

  1. <%
  2. '=======================================================================================================================
  3. ' MIGRATION: create_graph_tables
  4. '=======================================================================================================================
  5. ' Phase 4 graph relationships (spec FR-023, section 8 "Entity", "Relationship", section 14, ADR-014):
  6. ' entities named concepts of a namespace (type + canonical key unique per namespace), with aliases
  7. ' relationships typed, directed edges between entities, memories and documents; proposed -> accepted|rejected
  8. ' by review; evidence is required and stored in evidence_links with owner_kind 'relationship'
  9. ' evidence_links rebuilt so its owner_kind CHECK also allows 'relationship' (SQLite cannot alter a CHECK)
  10. '-----------------------------------------------------------------------------------------------------------------------
  11. Sub Migration_Up(migration)
  12. migration.ExecuteSQL "CREATE TABLE entities (" & _
  13. "id TEXT PRIMARY KEY, " & _
  14. "tenant_id TEXT NOT NULL REFERENCES tenants(id), " & _
  15. "namespace_id TEXT NOT NULL REFERENCES namespaces(id), " & _
  16. "type TEXT NOT NULL, " & _
  17. "canonical_key TEXT NOT NULL, " & _
  18. "name TEXT NOT NULL, " & _
  19. "aliases TEXT, " & _
  20. "description TEXT, " & _
  21. "properties TEXT, " & _
  22. "created_by TEXT NOT NULL REFERENCES principals(id), " & _
  23. "created_at TEXT NOT NULL, " & _
  24. "updated_at TEXT NOT NULL, " & _
  25. "UNIQUE (namespace_id, type, canonical_key))"
  26. migration.CreateIndex "idx_entities_tenant", "entities", "tenant_id, namespace_id"
  27. migration.ExecuteSQL "CREATE TABLE relationships (" & _
  28. "id TEXT PRIMARY KEY, " & _
  29. "tenant_id TEXT NOT NULL REFERENCES tenants(id), " & _
  30. "namespace_id TEXT NOT NULL REFERENCES namespaces(id), " & _
  31. "source_kind TEXT NOT NULL CHECK (source_kind IN ('entity','memory','document')), " & _
  32. "source_id TEXT NOT NULL, " & _
  33. "target_kind TEXT NOT NULL CHECK (target_kind IN ('entity','memory','document')), " & _
  34. "target_id TEXT NOT NULL, " & _
  35. "type TEXT NOT NULL, " & _
  36. "description TEXT, " & _
  37. "confidence REAL, " & _
  38. "valid_from TEXT, " & _
  39. "valid_until TEXT, " & _
  40. "inferred INTEGER NOT NULL DEFAULT 0 CHECK (inferred IN (0,1)), " & _
  41. "state TEXT NOT NULL DEFAULT 'proposed' CHECK (state IN ('proposed','accepted','rejected')), " & _
  42. "author_principal_id TEXT NOT NULL REFERENCES principals(id), " & _
  43. "reviewer_principal_id TEXT REFERENCES principals(id), " & _
  44. "review_notes TEXT, " & _
  45. "created_at TEXT NOT NULL, " & _
  46. "reviewed_at TEXT)"
  47. migration.CreateIndex "idx_relationships_source", "relationships", "tenant_id, source_kind, source_id"
  48. migration.CreateIndex "idx_relationships_target", "relationships", "tenant_id, target_kind, target_id"
  49. migration.CreateIndex "idx_relationships_namespace", "relationships", "tenant_id, namespace_id, state"
  50. ' At most one live (proposed or accepted) edge per namespace, endpoints and type
  51. migration.ExecuteSQL "CREATE UNIQUE INDEX idx_relationships_live ON relationships (namespace_id, source_kind, source_id, target_kind, target_id, type) " & _
  52. "WHERE state IN ('proposed','accepted')"
  53. RebuildEvidenceLinks migration, "'memory','candidate','relationship'"
  54. End Sub
  55. '-----------------------------------------------------------------------------------------------------------------------
  56. ' DOWN - Rollback the migration
  57. '-----------------------------------------------------------------------------------------------------------------------
  58. Sub Migration_Down(migration)
  59. migration.ExecuteSQL "DELETE FROM evidence_links WHERE owner_kind = 'relationship'"
  60. RebuildEvidenceLinks migration, "'memory','candidate'"
  61. migration.ExecuteSQL "DROP INDEX idx_relationships_live"
  62. migration.DropIndex "idx_relationships_namespace", "relationships"
  63. migration.DropIndex "idx_relationships_target", "relationships"
  64. migration.DropIndex "idx_relationships_source", "relationships"
  65. migration.DropTable "relationships"
  66. migration.DropIndex "idx_entities_tenant", "entities"
  67. migration.DropTable "entities"
  68. End Sub
  69. ' Recreates evidence_links with the given owner kinds, keeping every row and both indexes.
  70. Sub RebuildEvidenceLinks(migration, ownerKinds)
  71. migration.ExecuteSQL "CREATE TABLE evidence_links_new (" & _
  72. "id TEXT PRIMARY KEY, " & _
  73. "tenant_id TEXT NOT NULL REFERENCES tenants(id), " & _
  74. "owner_kind TEXT NOT NULL CHECK (owner_kind IN (" & ownerKinds & ")), " & _
  75. "owner_id TEXT NOT NULL, " & _
  76. "owner_revision INTEGER, " & _
  77. "target_kind TEXT NOT NULL CHECK (target_kind IN ('experience','chunk','memory','document')), " & _
  78. "target_id TEXT NOT NULL, " & _
  79. "target_revision INTEGER, " & _
  80. "role TEXT NOT NULL DEFAULT 'supports' CHECK (role IN ('supports','contradicts','context')), " & _
  81. "excerpt TEXT, " & _
  82. "created_by TEXT NOT NULL REFERENCES principals(id), " & _
  83. "created_at TEXT NOT NULL)"
  84. 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) " & _
  85. "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"
  86. migration.ExecuteSQL "DROP TABLE evidence_links"
  87. migration.ExecuteSQL "ALTER TABLE evidence_links_new RENAME TO evidence_links"
  88. migration.CreateIndex "idx_evidence_owner", "evidence_links", "owner_kind, owner_id, owner_revision"
  89. migration.CreateIndex "idx_evidence_tenant", "evidence_links", "tenant_id"
  90. End Sub
  91. %>

Powered by TurnKey Linux.