semantic-atlas 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/skills/semantic-atlas/SKILL.md +172 -0
- package/.agents/skills/semantic-atlas/references/graph-patch.md +109 -0
- package/.agents/skills/semantic-atlas/references/result-routing.md +91 -0
- package/LICENSE +21 -0
- package/README.md +118 -0
- package/dist/business-flow/business-flow-derivation-service.d.ts +11 -0
- package/dist/business-flow/business-flow-derivation-service.d.ts.map +1 -0
- package/dist/business-flow/business-flow-derivation-service.js +151 -0
- package/dist/business-flow/business-flow-draft.d.ts +16 -0
- package/dist/business-flow/business-flow-draft.d.ts.map +1 -0
- package/dist/business-flow/business-flow-draft.js +150 -0
- package/dist/business-flow/framework-business-strategy.d.ts +7 -0
- package/dist/business-flow/framework-business-strategy.d.ts.map +1 -0
- package/dist/business-flow/framework-business-strategy.js +1 -0
- package/dist/business-flow/strategies/bullmq-business-strategy.d.ts +10 -0
- package/dist/business-flow/strategies/bullmq-business-strategy.d.ts.map +1 -0
- package/dist/business-flow/strategies/bullmq-business-strategy.js +79 -0
- package/dist/business-flow/strategies/called-operation-derivation.d.ts +13 -0
- package/dist/business-flow/strategies/called-operation-derivation.d.ts.map +1 -0
- package/dist/business-flow/strategies/called-operation-derivation.js +32 -0
- package/dist/business-flow/strategies/graphql-business-strategy.d.ts +8 -0
- package/dist/business-flow/strategies/graphql-business-strategy.d.ts.map +1 -0
- package/dist/business-flow/strategies/graphql-business-strategy.js +65 -0
- package/dist/business-flow/strategies/nestjs-business-strategy.d.ts +8 -0
- package/dist/business-flow/strategies/nestjs-business-strategy.d.ts.map +1 -0
- package/dist/business-flow/strategies/nestjs-business-strategy.js +72 -0
- package/dist/business-flow/strategies/strategy-helpers.d.ts +32 -0
- package/dist/business-flow/strategies/strategy-helpers.d.ts.map +1 -0
- package/dist/business-flow/strategies/strategy-helpers.js +49 -0
- package/dist/business-flow/strategies/typeorm-business-strategy.d.ts +10 -0
- package/dist/business-flow/strategies/typeorm-business-strategy.d.ts.map +1 -0
- package/dist/business-flow/strategies/typeorm-business-strategy.js +136 -0
- package/dist/business-flow/strategies/verified-invariant-strategy.d.ts +5 -0
- package/dist/business-flow/strategies/verified-invariant-strategy.d.ts.map +1 -0
- package/dist/business-flow/strategies/verified-invariant-strategy.js +38 -0
- package/dist/business-flow/strategies/verified-test-strategy.d.ts +5 -0
- package/dist/business-flow/strategies/verified-test-strategy.d.ts.map +1 -0
- package/dist/business-flow/strategies/verified-test-strategy.js +26 -0
- package/dist/business-flow/structural-flow-catalog.d.ts +21 -0
- package/dist/business-flow/structural-flow-catalog.d.ts.map +1 -0
- package/dist/business-flow/structural-flow-catalog.js +125 -0
- package/dist/business-flow/types.d.ts +51 -0
- package/dist/business-flow/types.d.ts.map +1 -0
- package/dist/business-flow/types.js +1 -0
- package/dist/cli/argument-parser.d.ts +3 -0
- package/dist/cli/argument-parser.d.ts.map +1 -0
- package/dist/cli/argument-parser.js +174 -0
- package/dist/cli/bin.d.ts +3 -0
- package/dist/cli/bin.d.ts.map +1 -0
- package/dist/cli/bin.js +23 -0
- package/dist/cli/cli-application.d.ts +38 -0
- package/dist/cli/cli-application.d.ts.map +1 -0
- package/dist/cli/cli-application.js +515 -0
- package/dist/cli/cli-error.d.ts +12 -0
- package/dist/cli/cli-error.d.ts.map +1 -0
- package/dist/cli/cli-error.js +44 -0
- package/dist/cli/cli-presenter.d.ts +14 -0
- package/dist/cli/cli-presenter.d.ts.map +1 -0
- package/dist/cli/cli-presenter.js +36 -0
- package/dist/cli/main.d.ts +3 -0
- package/dist/cli/main.d.ts.map +1 -0
- package/dist/cli/main.js +45 -0
- package/dist/cli/types.d.ts +46 -0
- package/dist/cli/types.d.ts.map +1 -0
- package/dist/cli/types.js +1 -0
- package/dist/contracts/cli.d.ts +2139 -0
- package/dist/contracts/cli.d.ts.map +1 -0
- package/dist/contracts/cli.js +232 -0
- package/dist/contracts/graph.d.ts +262 -0
- package/dist/contracts/graph.d.ts.map +1 -0
- package/dist/contracts/graph.js +182 -0
- package/dist/contracts/identifiers.d.ts +4 -0
- package/dist/contracts/identifiers.d.ts.map +1 -0
- package/dist/contracts/identifiers.js +7 -0
- package/dist/contracts/public-contracts.d.ts +4 -0
- package/dist/contracts/public-contracts.d.ts.map +1 -0
- package/dist/contracts/public-contracts.js +3 -0
- package/dist/evaluation/comparison.d.ts +30 -0
- package/dist/evaluation/comparison.d.ts.map +1 -0
- package/dist/evaluation/comparison.js +108 -0
- package/dist/evaluation/contracts.d.ts +204 -0
- package/dist/evaluation/contracts.d.ts.map +1 -0
- package/dist/evaluation/contracts.js +269 -0
- package/dist/graph/graph-store.d.ts +47 -0
- package/dist/graph/graph-store.d.ts.map +1 -0
- package/dist/graph/graph-store.js +719 -0
- package/dist/graph/types.d.ts +168 -0
- package/dist/graph/types.d.ts.map +1 -0
- package/dist/graph/types.js +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/knowledge/business-knowledge-service.d.ts +24 -0
- package/dist/knowledge/business-knowledge-service.d.ts.map +1 -0
- package/dist/knowledge/business-knowledge-service.js +200 -0
- package/dist/knowledge/graph-patch-conflict-error.d.ts +8 -0
- package/dist/knowledge/graph-patch-conflict-error.d.ts.map +1 -0
- package/dist/knowledge/graph-patch-conflict-error.js +12 -0
- package/dist/knowledge/structural-target-binding.d.ts +14 -0
- package/dist/knowledge/structural-target-binding.d.ts.map +1 -0
- package/dist/knowledge/structural-target-binding.js +11 -0
- package/dist/repository/git-command.d.ts +3 -0
- package/dist/repository/git-command.d.ts.map +1 -0
- package/dist/repository/git-command.js +25 -0
- package/dist/repository/repository-inspector.d.ts +7 -0
- package/dist/repository/repository-inspector.d.ts.map +1 -0
- package/dist/repository/repository-inspector.js +84 -0
- package/dist/repository/repository-language-support.d.ts +14 -0
- package/dist/repository/repository-language-support.d.ts.map +1 -0
- package/dist/repository/repository-language-support.js +64 -0
- package/dist/repository/types.d.ts +9 -0
- package/dist/repository/types.d.ts.map +1 -0
- package/dist/repository/types.js +1 -0
- package/dist/snapshots/repository-snapshot.d.ts +4 -0
- package/dist/snapshots/repository-snapshot.d.ts.map +1 -0
- package/dist/snapshots/repository-snapshot.js +119 -0
- package/dist/snapshots/types.d.ts +30 -0
- package/dist/snapshots/types.d.ts.map +1 -0
- package/dist/snapshots/types.js +1 -0
- package/dist/storage/atlas-database.d.ts +14 -0
- package/dist/storage/atlas-database.d.ts.map +1 -0
- package/dist/storage/atlas-database.js +552 -0
- package/dist/storage/snapshot-store.d.ts +19 -0
- package/dist/storage/snapshot-store.d.ts.map +1 -0
- package/dist/storage/snapshot-store.js +77 -0
- package/dist/structural-backend/codegraph-backend.d.ts +54 -0
- package/dist/structural-backend/codegraph-backend.d.ts.map +1 -0
- package/dist/structural-backend/codegraph-backend.js +1224 -0
- package/dist/structural-backend/codegraph-worker-client.d.ts +5 -0
- package/dist/structural-backend/codegraph-worker-client.d.ts.map +1 -0
- package/dist/structural-backend/codegraph-worker-client.js +118 -0
- package/dist/structural-backend/codegraph-worker-protocol.d.ts +35 -0
- package/dist/structural-backend/codegraph-worker-protocol.d.ts.map +1 -0
- package/dist/structural-backend/codegraph-worker-protocol.js +1 -0
- package/dist/structural-backend/codegraph-worker.d.ts +2 -0
- package/dist/structural-backend/codegraph-worker.d.ts.map +1 -0
- package/dist/structural-backend/codegraph-worker.js +131 -0
- package/dist/structural-backend/process-instance.d.ts +18 -0
- package/dist/structural-backend/process-instance.d.ts.map +1 -0
- package/dist/structural-backend/process-instance.js +207 -0
- package/dist/structural-backend/structural-fact-changes.d.ts +7 -0
- package/dist/structural-backend/structural-fact-changes.d.ts.map +1 -0
- package/dist/structural-backend/structural-fact-changes.js +78 -0
- package/dist/structural-backend/structural-publication.d.ts +12 -0
- package/dist/structural-backend/structural-publication.d.ts.map +1 -0
- package/dist/structural-backend/structural-publication.js +252 -0
- package/dist/structural-backend/structural-write-lock.d.ts +8 -0
- package/dist/structural-backend/structural-write-lock.d.ts.map +1 -0
- package/dist/structural-backend/structural-write-lock.js +415 -0
- package/dist/structural-backend/types.d.ts +144 -0
- package/dist/structural-backend/types.d.ts.map +1 -0
- package/dist/structural-backend/types.js +11 -0
- package/dist/world/types.d.ts +77 -0
- package/dist/world/types.d.ts.map +1 -0
- package/dist/world/types.js +1 -0
- package/dist/world/world-graph-query.d.ts +31 -0
- package/dist/world/world-graph-query.d.ts.map +1 -0
- package/dist/world/world-graph-query.js +453 -0
- package/dist/world/world-model-service.d.ts +21 -0
- package/dist/world/world-model-service.d.ts.map +1 -0
- package/dist/world/world-model-service.js +211 -0
- package/dist/world/world-snapshot-store.d.ts +40 -0
- package/dist/world/world-snapshot-store.d.ts.map +1 -0
- package/dist/world/world-snapshot-store.js +555 -0
- package/docs/architecture/codegraph-backend.md +193 -0
- package/docs/contracts/cli-v1.md +119 -0
- package/docs/contracts/graph-model.md +102 -0
- package/docs/contracts/graph-patch-v1.md +98 -0
- package/docs/evaluation.md +128 -0
- package/docs/product-contract.md +148 -0
- package/package.json +78 -0
- package/schemas/cli-envelope-v1.schema.json +1359 -0
- package/schemas/evaluation-plan-v1.schema.json +211 -0
- package/schemas/evaluation-run-v1.schema.json +365 -0
- package/schemas/graph-patch-v1.schema.json +392 -0
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
import { lstatSync, realpathSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
export const CURRENT_ATLAS_SCHEMA_VERSION = 5;
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
const migrations = [
|
|
7
|
+
{
|
|
8
|
+
version: 1,
|
|
9
|
+
sql: `
|
|
10
|
+
CREATE TABLE atlas_repositories (
|
|
11
|
+
repository_id TEXT PRIMARY KEY,
|
|
12
|
+
common_git_directory TEXT NOT NULL,
|
|
13
|
+
worktree_root TEXT NOT NULL,
|
|
14
|
+
latest_snapshot_id TEXT,
|
|
15
|
+
updated_at TEXT NOT NULL
|
|
16
|
+
) STRICT;
|
|
17
|
+
|
|
18
|
+
CREATE TABLE atlas_repository_snapshots (
|
|
19
|
+
repository_id TEXT NOT NULL,
|
|
20
|
+
snapshot_id TEXT NOT NULL,
|
|
21
|
+
payload TEXT NOT NULL,
|
|
22
|
+
created_at TEXT NOT NULL,
|
|
23
|
+
PRIMARY KEY (repository_id, snapshot_id),
|
|
24
|
+
FOREIGN KEY (repository_id)
|
|
25
|
+
REFERENCES atlas_repositories(repository_id) ON DELETE CASCADE
|
|
26
|
+
) STRICT;
|
|
27
|
+
|
|
28
|
+
CREATE TABLE atlas_business_nodes (
|
|
29
|
+
node_id INTEGER PRIMARY KEY,
|
|
30
|
+
repository_id TEXT NOT NULL,
|
|
31
|
+
node_key TEXT NOT NULL,
|
|
32
|
+
base_snapshot_id TEXT NOT NULL,
|
|
33
|
+
kind TEXT NOT NULL CHECK (kind IN (
|
|
34
|
+
'Capability', 'Scenario', 'Operation', 'Invariant', 'Interface', 'Data'
|
|
35
|
+
)),
|
|
36
|
+
label TEXT NOT NULL,
|
|
37
|
+
summary TEXT NOT NULL,
|
|
38
|
+
certainty TEXT NOT NULL CHECK (certainty IN ('exact', 'inferred', 'hypothesis')),
|
|
39
|
+
UNIQUE (repository_id, node_key),
|
|
40
|
+
FOREIGN KEY (repository_id, base_snapshot_id)
|
|
41
|
+
REFERENCES atlas_repository_snapshots(repository_id, snapshot_id)
|
|
42
|
+
) STRICT;
|
|
43
|
+
|
|
44
|
+
CREATE TABLE atlas_business_node_aliases (
|
|
45
|
+
node_id INTEGER NOT NULL,
|
|
46
|
+
position INTEGER NOT NULL,
|
|
47
|
+
alias TEXT NOT NULL,
|
|
48
|
+
PRIMARY KEY (node_id, position),
|
|
49
|
+
FOREIGN KEY (node_id) REFERENCES atlas_business_nodes(node_id) ON DELETE CASCADE
|
|
50
|
+
) STRICT;
|
|
51
|
+
|
|
52
|
+
CREATE TABLE atlas_business_node_evidence (
|
|
53
|
+
node_id INTEGER NOT NULL,
|
|
54
|
+
position INTEGER NOT NULL,
|
|
55
|
+
structural_reference TEXT NOT NULL,
|
|
56
|
+
file TEXT NOT NULL,
|
|
57
|
+
start_line INTEGER NOT NULL,
|
|
58
|
+
start_column INTEGER NOT NULL,
|
|
59
|
+
end_line INTEGER NOT NULL,
|
|
60
|
+
end_column INTEGER NOT NULL,
|
|
61
|
+
content_hash TEXT NOT NULL,
|
|
62
|
+
PRIMARY KEY (node_id, position),
|
|
63
|
+
FOREIGN KEY (node_id) REFERENCES atlas_business_nodes(node_id) ON DELETE CASCADE
|
|
64
|
+
) STRICT;
|
|
65
|
+
|
|
66
|
+
CREATE TABLE atlas_business_relations (
|
|
67
|
+
relation_id INTEGER PRIMARY KEY,
|
|
68
|
+
repository_id TEXT NOT NULL,
|
|
69
|
+
base_snapshot_id TEXT NOT NULL,
|
|
70
|
+
from_key TEXT NOT NULL,
|
|
71
|
+
relation_type TEXT NOT NULL CHECK (relation_type IN (
|
|
72
|
+
'part_of', 'realized_by', 'reads', 'writes', 'publishes', 'consumes',
|
|
73
|
+
'constrained_by', 'verified_by'
|
|
74
|
+
)),
|
|
75
|
+
to_domain TEXT NOT NULL CHECK (to_domain IN ('structural', 'business')),
|
|
76
|
+
to_key TEXT NOT NULL,
|
|
77
|
+
certainty TEXT NOT NULL CHECK (certainty IN ('exact', 'inferred', 'hypothesis')),
|
|
78
|
+
UNIQUE (repository_id, from_key, relation_type, to_domain, to_key),
|
|
79
|
+
FOREIGN KEY (repository_id, base_snapshot_id)
|
|
80
|
+
REFERENCES atlas_repository_snapshots(repository_id, snapshot_id),
|
|
81
|
+
FOREIGN KEY (repository_id, from_key)
|
|
82
|
+
REFERENCES atlas_business_nodes(repository_id, node_key)
|
|
83
|
+
) STRICT;
|
|
84
|
+
|
|
85
|
+
CREATE INDEX atlas_business_relations_from_index
|
|
86
|
+
ON atlas_business_relations (repository_id, from_key);
|
|
87
|
+
CREATE INDEX atlas_business_relations_to_index
|
|
88
|
+
ON atlas_business_relations (repository_id, to_domain, to_key);
|
|
89
|
+
|
|
90
|
+
CREATE TABLE atlas_business_relation_evidence (
|
|
91
|
+
relation_id INTEGER NOT NULL,
|
|
92
|
+
position INTEGER NOT NULL,
|
|
93
|
+
structural_reference TEXT NOT NULL,
|
|
94
|
+
file TEXT NOT NULL,
|
|
95
|
+
start_line INTEGER NOT NULL,
|
|
96
|
+
start_column INTEGER NOT NULL,
|
|
97
|
+
end_line INTEGER NOT NULL,
|
|
98
|
+
end_column INTEGER NOT NULL,
|
|
99
|
+
content_hash TEXT NOT NULL,
|
|
100
|
+
PRIMARY KEY (relation_id, position),
|
|
101
|
+
FOREIGN KEY (relation_id)
|
|
102
|
+
REFERENCES atlas_business_relations(relation_id) ON DELETE CASCADE
|
|
103
|
+
) STRICT;
|
|
104
|
+
|
|
105
|
+
CREATE TABLE atlas_business_node_validity (
|
|
106
|
+
node_id INTEGER NOT NULL,
|
|
107
|
+
repository_id TEXT NOT NULL,
|
|
108
|
+
snapshot_id TEXT NOT NULL,
|
|
109
|
+
validity TEXT NOT NULL CHECK (validity IN ('valid', 'stale')),
|
|
110
|
+
PRIMARY KEY (node_id, snapshot_id),
|
|
111
|
+
FOREIGN KEY (node_id) REFERENCES atlas_business_nodes(node_id) ON DELETE CASCADE,
|
|
112
|
+
FOREIGN KEY (repository_id, snapshot_id)
|
|
113
|
+
REFERENCES atlas_repository_snapshots(repository_id, snapshot_id) ON DELETE CASCADE
|
|
114
|
+
) STRICT;
|
|
115
|
+
|
|
116
|
+
CREATE TABLE atlas_business_relation_validity (
|
|
117
|
+
relation_id INTEGER NOT NULL,
|
|
118
|
+
repository_id TEXT NOT NULL,
|
|
119
|
+
snapshot_id TEXT NOT NULL,
|
|
120
|
+
validity TEXT NOT NULL CHECK (validity IN ('valid', 'stale')),
|
|
121
|
+
PRIMARY KEY (relation_id, snapshot_id),
|
|
122
|
+
FOREIGN KEY (relation_id)
|
|
123
|
+
REFERENCES atlas_business_relations(relation_id) ON DELETE CASCADE,
|
|
124
|
+
FOREIGN KEY (repository_id, snapshot_id)
|
|
125
|
+
REFERENCES atlas_repository_snapshots(repository_id, snapshot_id) ON DELETE CASCADE
|
|
126
|
+
) STRICT;
|
|
127
|
+
|
|
128
|
+
CREATE TABLE atlas_graph_search (
|
|
129
|
+
repository_id TEXT NOT NULL,
|
|
130
|
+
node_key TEXT NOT NULL,
|
|
131
|
+
label TEXT NOT NULL,
|
|
132
|
+
aliases TEXT NOT NULL,
|
|
133
|
+
summary TEXT NOT NULL,
|
|
134
|
+
symbols TEXT NOT NULL,
|
|
135
|
+
paths TEXT NOT NULL,
|
|
136
|
+
PRIMARY KEY (repository_id, node_key),
|
|
137
|
+
FOREIGN KEY (repository_id, node_key)
|
|
138
|
+
REFERENCES atlas_business_nodes(repository_id, node_key) ON DELETE CASCADE
|
|
139
|
+
) STRICT;
|
|
140
|
+
`,
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
version: 2,
|
|
144
|
+
sql: `
|
|
145
|
+
CREATE TABLE atlas_world_state (
|
|
146
|
+
repository_id TEXT PRIMARY KEY,
|
|
147
|
+
status TEXT NOT NULL CHECK (status IN ('missing', 'building', 'current', 'failed')),
|
|
148
|
+
current_snapshot_id TEXT,
|
|
149
|
+
target_snapshot_id TEXT,
|
|
150
|
+
backend_version TEXT,
|
|
151
|
+
extraction_version INTEGER,
|
|
152
|
+
failure_message TEXT,
|
|
153
|
+
started_at TEXT,
|
|
154
|
+
published_at TEXT,
|
|
155
|
+
updated_at TEXT NOT NULL,
|
|
156
|
+
FOREIGN KEY (repository_id) REFERENCES atlas_repositories(repository_id) ON DELETE CASCADE,
|
|
157
|
+
FOREIGN KEY (repository_id, current_snapshot_id)
|
|
158
|
+
REFERENCES atlas_repository_snapshots(repository_id, snapshot_id)
|
|
159
|
+
) STRICT;
|
|
160
|
+
|
|
161
|
+
CREATE TABLE atlas_semantic_changes (
|
|
162
|
+
repository_id TEXT NOT NULL,
|
|
163
|
+
from_snapshot_id TEXT,
|
|
164
|
+
to_snapshot_id TEXT NOT NULL,
|
|
165
|
+
added_paths TEXT NOT NULL,
|
|
166
|
+
modified_paths TEXT NOT NULL,
|
|
167
|
+
removed_paths TEXT NOT NULL,
|
|
168
|
+
stale_assertions TEXT NOT NULL,
|
|
169
|
+
created_at TEXT NOT NULL,
|
|
170
|
+
PRIMARY KEY (repository_id, to_snapshot_id),
|
|
171
|
+
FOREIGN KEY (repository_id, to_snapshot_id)
|
|
172
|
+
REFERENCES atlas_repository_snapshots(repository_id, snapshot_id) ON DELETE CASCADE
|
|
173
|
+
) STRICT;
|
|
174
|
+
|
|
175
|
+
ALTER TABLE atlas_business_node_evidence ADD COLUMN qualified_symbol TEXT;
|
|
176
|
+
ALTER TABLE atlas_business_node_evidence ADD COLUMN structural_kind TEXT;
|
|
177
|
+
ALTER TABLE atlas_business_node_evidence ADD COLUMN atlas_snapshot_id TEXT;
|
|
178
|
+
ALTER TABLE atlas_business_node_evidence ADD COLUMN backend_version TEXT;
|
|
179
|
+
ALTER TABLE atlas_business_node_evidence ADD COLUMN backend_locator TEXT;
|
|
180
|
+
ALTER TABLE atlas_business_node_evidence ADD COLUMN binding_status TEXT NOT NULL DEFAULT 'unresolved'
|
|
181
|
+
CHECK (binding_status IN ('bound', 'missing', 'ambiguous', 'unresolved'));
|
|
182
|
+
|
|
183
|
+
ALTER TABLE atlas_business_relation_evidence ADD COLUMN qualified_symbol TEXT;
|
|
184
|
+
ALTER TABLE atlas_business_relation_evidence ADD COLUMN structural_kind TEXT;
|
|
185
|
+
ALTER TABLE atlas_business_relation_evidence ADD COLUMN atlas_snapshot_id TEXT;
|
|
186
|
+
ALTER TABLE atlas_business_relation_evidence ADD COLUMN backend_version TEXT;
|
|
187
|
+
ALTER TABLE atlas_business_relation_evidence ADD COLUMN backend_locator TEXT;
|
|
188
|
+
ALTER TABLE atlas_business_relation_evidence ADD COLUMN binding_status TEXT NOT NULL DEFAULT 'unresolved'
|
|
189
|
+
CHECK (binding_status IN ('bound', 'missing', 'ambiguous', 'unresolved'));
|
|
190
|
+
|
|
191
|
+
INSERT INTO atlas_world_state (repository_id, status, updated_at)
|
|
192
|
+
SELECT repository_id, 'missing', updated_at
|
|
193
|
+
FROM atlas_repositories;
|
|
194
|
+
`,
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
version: 3,
|
|
198
|
+
sql: `
|
|
199
|
+
ALTER TABLE atlas_business_relations ADD COLUMN target_file TEXT;
|
|
200
|
+
ALTER TABLE atlas_business_relations ADD COLUMN target_qualified_symbol TEXT;
|
|
201
|
+
ALTER TABLE atlas_business_relations ADD COLUMN target_structural_kind TEXT;
|
|
202
|
+
ALTER TABLE atlas_business_relations ADD COLUMN target_start_line INTEGER;
|
|
203
|
+
ALTER TABLE atlas_business_relations ADD COLUMN target_start_column INTEGER;
|
|
204
|
+
ALTER TABLE atlas_business_relations ADD COLUMN target_end_line INTEGER;
|
|
205
|
+
ALTER TABLE atlas_business_relations ADD COLUMN target_end_column INTEGER;
|
|
206
|
+
ALTER TABLE atlas_business_relations ADD COLUMN target_atlas_snapshot_id TEXT;
|
|
207
|
+
ALTER TABLE atlas_business_relations ADD COLUMN target_backend_version TEXT;
|
|
208
|
+
ALTER TABLE atlas_business_relations ADD COLUMN target_backend_locator TEXT;
|
|
209
|
+
ALTER TABLE atlas_business_relations ADD COLUMN target_binding_status TEXT NOT NULL DEFAULT 'unresolved'
|
|
210
|
+
CHECK (target_binding_status IN ('bound', 'missing', 'ambiguous', 'unresolved'));
|
|
211
|
+
|
|
212
|
+
UPDATE atlas_business_relations
|
|
213
|
+
SET
|
|
214
|
+
target_file = evidence.file,
|
|
215
|
+
target_qualified_symbol = evidence.qualified_symbol,
|
|
216
|
+
target_structural_kind = evidence.structural_kind,
|
|
217
|
+
target_start_line = evidence.start_line,
|
|
218
|
+
target_start_column = evidence.start_column,
|
|
219
|
+
target_end_line = evidence.end_line,
|
|
220
|
+
target_end_column = evidence.end_column,
|
|
221
|
+
target_atlas_snapshot_id = evidence.atlas_snapshot_id,
|
|
222
|
+
target_backend_version = evidence.backend_version,
|
|
223
|
+
target_backend_locator = evidence.backend_locator,
|
|
224
|
+
target_binding_status = evidence.binding_status
|
|
225
|
+
FROM atlas_business_relation_evidence AS evidence
|
|
226
|
+
WHERE atlas_business_relations.to_domain = 'structural'
|
|
227
|
+
AND evidence.relation_id = atlas_business_relations.relation_id
|
|
228
|
+
AND evidence.structural_reference = atlas_business_relations.to_key
|
|
229
|
+
AND evidence.position = (
|
|
230
|
+
SELECT MIN(candidate.position)
|
|
231
|
+
FROM atlas_business_relation_evidence AS candidate
|
|
232
|
+
WHERE candidate.relation_id = atlas_business_relations.relation_id
|
|
233
|
+
AND candidate.structural_reference = atlas_business_relations.to_key
|
|
234
|
+
);
|
|
235
|
+
`,
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
version: 4,
|
|
239
|
+
sql: `
|
|
240
|
+
CREATE TABLE atlas_world_publications (
|
|
241
|
+
publication_id INTEGER PRIMARY KEY,
|
|
242
|
+
repository_id TEXT NOT NULL,
|
|
243
|
+
previous_publication_id INTEGER CHECK (
|
|
244
|
+
previous_publication_id IS NULL OR previous_publication_id < publication_id
|
|
245
|
+
),
|
|
246
|
+
snapshot_id TEXT NOT NULL,
|
|
247
|
+
added_paths TEXT NOT NULL,
|
|
248
|
+
modified_paths TEXT NOT NULL,
|
|
249
|
+
removed_paths TEXT NOT NULL,
|
|
250
|
+
stale_assertions TEXT NOT NULL,
|
|
251
|
+
published_at TEXT NOT NULL,
|
|
252
|
+
UNIQUE (repository_id, publication_id),
|
|
253
|
+
FOREIGN KEY (repository_id)
|
|
254
|
+
REFERENCES atlas_repositories(repository_id) ON DELETE CASCADE,
|
|
255
|
+
FOREIGN KEY (repository_id, snapshot_id)
|
|
256
|
+
REFERENCES atlas_repository_snapshots(repository_id, snapshot_id) ON DELETE CASCADE,
|
|
257
|
+
FOREIGN KEY (repository_id, previous_publication_id)
|
|
258
|
+
REFERENCES atlas_world_publications(repository_id, publication_id)
|
|
259
|
+
) STRICT;
|
|
260
|
+
|
|
261
|
+
INSERT INTO atlas_world_publications (
|
|
262
|
+
publication_id,
|
|
263
|
+
repository_id,
|
|
264
|
+
previous_publication_id,
|
|
265
|
+
snapshot_id,
|
|
266
|
+
added_paths,
|
|
267
|
+
modified_paths,
|
|
268
|
+
removed_paths,
|
|
269
|
+
stale_assertions,
|
|
270
|
+
published_at
|
|
271
|
+
)
|
|
272
|
+
SELECT
|
|
273
|
+
rowid,
|
|
274
|
+
repository_id,
|
|
275
|
+
NULL,
|
|
276
|
+
to_snapshot_id,
|
|
277
|
+
added_paths,
|
|
278
|
+
modified_paths,
|
|
279
|
+
removed_paths,
|
|
280
|
+
stale_assertions,
|
|
281
|
+
created_at
|
|
282
|
+
FROM atlas_semantic_changes
|
|
283
|
+
ORDER BY rowid;
|
|
284
|
+
|
|
285
|
+
UPDATE atlas_world_publications AS publication
|
|
286
|
+
SET previous_publication_id = (
|
|
287
|
+
SELECT previous.publication_id
|
|
288
|
+
FROM atlas_semantic_changes AS legacy
|
|
289
|
+
JOIN atlas_world_publications AS previous
|
|
290
|
+
ON previous.repository_id = legacy.repository_id
|
|
291
|
+
AND previous.snapshot_id = legacy.from_snapshot_id
|
|
292
|
+
AND previous.publication_id < publication.publication_id
|
|
293
|
+
WHERE legacy.rowid = publication.publication_id
|
|
294
|
+
ORDER BY previous.publication_id DESC
|
|
295
|
+
LIMIT 1
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
CREATE INDEX atlas_world_publications_repository_order_index
|
|
299
|
+
ON atlas_world_publications (repository_id, publication_id DESC);
|
|
300
|
+
CREATE INDEX atlas_world_publications_snapshot_index
|
|
301
|
+
ON atlas_world_publications (repository_id, snapshot_id, publication_id DESC);
|
|
302
|
+
|
|
303
|
+
ALTER TABLE atlas_world_state
|
|
304
|
+
ADD COLUMN current_publication_id INTEGER REFERENCES atlas_world_publications(publication_id);
|
|
305
|
+
UPDATE atlas_world_state
|
|
306
|
+
SET current_publication_id = (
|
|
307
|
+
SELECT publication.publication_id
|
|
308
|
+
FROM atlas_world_publications AS publication
|
|
309
|
+
WHERE publication.repository_id = atlas_world_state.repository_id
|
|
310
|
+
AND publication.snapshot_id = atlas_world_state.current_snapshot_id
|
|
311
|
+
ORDER BY publication.publication_id DESC
|
|
312
|
+
LIMIT 1
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
DROP TABLE atlas_semantic_changes;
|
|
316
|
+
`,
|
|
317
|
+
},
|
|
318
|
+
{
|
|
319
|
+
version: 5,
|
|
320
|
+
sql: `
|
|
321
|
+
CREATE TABLE atlas_business_relations_v5 (
|
|
322
|
+
relation_id INTEGER PRIMARY KEY,
|
|
323
|
+
repository_id TEXT NOT NULL,
|
|
324
|
+
base_snapshot_id TEXT NOT NULL,
|
|
325
|
+
from_key TEXT NOT NULL,
|
|
326
|
+
relation_type TEXT NOT NULL CHECK (relation_type IN (
|
|
327
|
+
'part_of', 'invokes', 'realized_by', 'reads', 'writes', 'publishes',
|
|
328
|
+
'consumes', 'constrained_by', 'verified_by'
|
|
329
|
+
)),
|
|
330
|
+
to_domain TEXT NOT NULL CHECK (to_domain IN ('structural', 'business')),
|
|
331
|
+
to_key TEXT NOT NULL,
|
|
332
|
+
certainty TEXT NOT NULL CHECK (certainty IN ('exact', 'inferred', 'hypothesis')),
|
|
333
|
+
target_file TEXT,
|
|
334
|
+
target_qualified_symbol TEXT,
|
|
335
|
+
target_structural_kind TEXT,
|
|
336
|
+
target_start_line INTEGER,
|
|
337
|
+
target_start_column INTEGER,
|
|
338
|
+
target_end_line INTEGER,
|
|
339
|
+
target_end_column INTEGER,
|
|
340
|
+
target_atlas_snapshot_id TEXT,
|
|
341
|
+
target_backend_version TEXT,
|
|
342
|
+
target_backend_locator TEXT,
|
|
343
|
+
target_binding_status TEXT NOT NULL DEFAULT 'unresolved'
|
|
344
|
+
CHECK (target_binding_status IN ('bound', 'missing', 'ambiguous', 'unresolved')),
|
|
345
|
+
UNIQUE (repository_id, from_key, relation_type, to_domain, to_key),
|
|
346
|
+
FOREIGN KEY (repository_id, base_snapshot_id)
|
|
347
|
+
REFERENCES atlas_repository_snapshots(repository_id, snapshot_id),
|
|
348
|
+
FOREIGN KEY (repository_id, from_key)
|
|
349
|
+
REFERENCES atlas_business_nodes(repository_id, node_key)
|
|
350
|
+
) STRICT;
|
|
351
|
+
|
|
352
|
+
INSERT INTO atlas_business_relations_v5 (
|
|
353
|
+
relation_id,
|
|
354
|
+
repository_id,
|
|
355
|
+
base_snapshot_id,
|
|
356
|
+
from_key,
|
|
357
|
+
relation_type,
|
|
358
|
+
to_domain,
|
|
359
|
+
to_key,
|
|
360
|
+
certainty,
|
|
361
|
+
target_file,
|
|
362
|
+
target_qualified_symbol,
|
|
363
|
+
target_structural_kind,
|
|
364
|
+
target_start_line,
|
|
365
|
+
target_start_column,
|
|
366
|
+
target_end_line,
|
|
367
|
+
target_end_column,
|
|
368
|
+
target_atlas_snapshot_id,
|
|
369
|
+
target_backend_version,
|
|
370
|
+
target_backend_locator,
|
|
371
|
+
target_binding_status
|
|
372
|
+
)
|
|
373
|
+
SELECT
|
|
374
|
+
relation_id,
|
|
375
|
+
repository_id,
|
|
376
|
+
base_snapshot_id,
|
|
377
|
+
from_key,
|
|
378
|
+
relation_type,
|
|
379
|
+
to_domain,
|
|
380
|
+
to_key,
|
|
381
|
+
certainty,
|
|
382
|
+
target_file,
|
|
383
|
+
target_qualified_symbol,
|
|
384
|
+
target_structural_kind,
|
|
385
|
+
target_start_line,
|
|
386
|
+
target_start_column,
|
|
387
|
+
target_end_line,
|
|
388
|
+
target_end_column,
|
|
389
|
+
target_atlas_snapshot_id,
|
|
390
|
+
target_backend_version,
|
|
391
|
+
target_backend_locator,
|
|
392
|
+
target_binding_status
|
|
393
|
+
FROM atlas_business_relations;
|
|
394
|
+
|
|
395
|
+
CREATE TABLE atlas_business_relation_evidence_v5 (
|
|
396
|
+
relation_id INTEGER NOT NULL,
|
|
397
|
+
position INTEGER NOT NULL,
|
|
398
|
+
structural_reference TEXT NOT NULL,
|
|
399
|
+
file TEXT NOT NULL,
|
|
400
|
+
start_line INTEGER NOT NULL,
|
|
401
|
+
start_column INTEGER NOT NULL,
|
|
402
|
+
end_line INTEGER NOT NULL,
|
|
403
|
+
end_column INTEGER NOT NULL,
|
|
404
|
+
content_hash TEXT NOT NULL,
|
|
405
|
+
qualified_symbol TEXT,
|
|
406
|
+
structural_kind TEXT,
|
|
407
|
+
atlas_snapshot_id TEXT,
|
|
408
|
+
backend_version TEXT,
|
|
409
|
+
backend_locator TEXT,
|
|
410
|
+
binding_status TEXT NOT NULL DEFAULT 'unresolved'
|
|
411
|
+
CHECK (binding_status IN ('bound', 'missing', 'ambiguous', 'unresolved')),
|
|
412
|
+
PRIMARY KEY (relation_id, position),
|
|
413
|
+
FOREIGN KEY (relation_id)
|
|
414
|
+
REFERENCES atlas_business_relations_v5(relation_id) ON DELETE CASCADE
|
|
415
|
+
) STRICT;
|
|
416
|
+
|
|
417
|
+
INSERT INTO atlas_business_relation_evidence_v5
|
|
418
|
+
SELECT * FROM atlas_business_relation_evidence;
|
|
419
|
+
|
|
420
|
+
CREATE TABLE atlas_business_relation_validity_v5 (
|
|
421
|
+
relation_id INTEGER NOT NULL,
|
|
422
|
+
repository_id TEXT NOT NULL,
|
|
423
|
+
snapshot_id TEXT NOT NULL,
|
|
424
|
+
validity TEXT NOT NULL CHECK (validity IN ('valid', 'stale')),
|
|
425
|
+
PRIMARY KEY (relation_id, snapshot_id),
|
|
426
|
+
FOREIGN KEY (relation_id)
|
|
427
|
+
REFERENCES atlas_business_relations_v5(relation_id) ON DELETE CASCADE,
|
|
428
|
+
FOREIGN KEY (repository_id, snapshot_id)
|
|
429
|
+
REFERENCES atlas_repository_snapshots(repository_id, snapshot_id) ON DELETE CASCADE
|
|
430
|
+
) STRICT;
|
|
431
|
+
|
|
432
|
+
INSERT INTO atlas_business_relation_validity_v5
|
|
433
|
+
SELECT * FROM atlas_business_relation_validity;
|
|
434
|
+
|
|
435
|
+
DROP TABLE atlas_business_relation_evidence;
|
|
436
|
+
DROP TABLE atlas_business_relation_validity;
|
|
437
|
+
DROP TABLE atlas_business_relations;
|
|
438
|
+
ALTER TABLE atlas_business_relations_v5 RENAME TO atlas_business_relations;
|
|
439
|
+
ALTER TABLE atlas_business_relation_evidence_v5
|
|
440
|
+
RENAME TO atlas_business_relation_evidence;
|
|
441
|
+
ALTER TABLE atlas_business_relation_validity_v5
|
|
442
|
+
RENAME TO atlas_business_relation_validity;
|
|
443
|
+
|
|
444
|
+
CREATE INDEX atlas_business_relations_from_index
|
|
445
|
+
ON atlas_business_relations (repository_id, from_key);
|
|
446
|
+
CREATE INDEX atlas_business_relations_to_index
|
|
447
|
+
ON atlas_business_relations (repository_id, to_domain, to_key);
|
|
448
|
+
`,
|
|
449
|
+
},
|
|
450
|
+
];
|
|
451
|
+
export class AtlasDatabase {
|
|
452
|
+
databasePath;
|
|
453
|
+
connection;
|
|
454
|
+
#closed = false;
|
|
455
|
+
constructor(repository) {
|
|
456
|
+
const atlasDirectory = join(repository.worktreeRoot, ".atlas");
|
|
457
|
+
const databasePath = join(atlasDirectory, "codegraph.db");
|
|
458
|
+
requireExistingSharedDatabase(atlasDirectory, databasePath);
|
|
459
|
+
this.databasePath = databasePath;
|
|
460
|
+
const { DatabaseSync } = require("node:sqlite");
|
|
461
|
+
this.connection = new DatabaseSync(databasePath);
|
|
462
|
+
try {
|
|
463
|
+
this.connection.exec("PRAGMA foreign_keys = ON; PRAGMA journal_mode = WAL;");
|
|
464
|
+
this.applyMigrations();
|
|
465
|
+
this.connection.prepare(`
|
|
466
|
+
INSERT INTO atlas_repositories (
|
|
467
|
+
repository_id,
|
|
468
|
+
common_git_directory,
|
|
469
|
+
worktree_root,
|
|
470
|
+
updated_at
|
|
471
|
+
) VALUES (?, ?, ?, ?)
|
|
472
|
+
ON CONFLICT (repository_id) DO UPDATE SET
|
|
473
|
+
common_git_directory = excluded.common_git_directory,
|
|
474
|
+
worktree_root = excluded.worktree_root,
|
|
475
|
+
updated_at = excluded.updated_at
|
|
476
|
+
`).run(repository.repositoryId, repository.commonGitDirectory, repository.worktreeRoot, new Date().toISOString());
|
|
477
|
+
this.connection.prepare(`
|
|
478
|
+
INSERT INTO atlas_world_state (repository_id, status, updated_at)
|
|
479
|
+
VALUES (?, 'missing', ?)
|
|
480
|
+
ON CONFLICT (repository_id) DO NOTHING
|
|
481
|
+
`).run(repository.repositoryId, new Date().toISOString());
|
|
482
|
+
}
|
|
483
|
+
catch (error) {
|
|
484
|
+
this.connection.close();
|
|
485
|
+
throw error;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
get schemaVersion() {
|
|
489
|
+
const row = this.connection.prepare(`
|
|
490
|
+
SELECT COALESCE(MAX(version), 0) AS version
|
|
491
|
+
FROM atlas_schema_migrations
|
|
492
|
+
`).get();
|
|
493
|
+
return row.version;
|
|
494
|
+
}
|
|
495
|
+
close() {
|
|
496
|
+
if (this.#closed) {
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
this.connection.close();
|
|
500
|
+
this.#closed = true;
|
|
501
|
+
}
|
|
502
|
+
[Symbol.dispose]() {
|
|
503
|
+
this.close();
|
|
504
|
+
}
|
|
505
|
+
applyMigrations() {
|
|
506
|
+
this.connection.exec(`
|
|
507
|
+
CREATE TABLE IF NOT EXISTS atlas_schema_migrations (
|
|
508
|
+
version INTEGER PRIMARY KEY,
|
|
509
|
+
applied_at TEXT NOT NULL
|
|
510
|
+
) STRICT;
|
|
511
|
+
`);
|
|
512
|
+
const currentVersion = this.schemaVersion;
|
|
513
|
+
if (currentVersion > CURRENT_ATLAS_SCHEMA_VERSION) {
|
|
514
|
+
throw new Error(`Atlas database schema ${currentVersion} is newer than supported schema ${CURRENT_ATLAS_SCHEMA_VERSION}`);
|
|
515
|
+
}
|
|
516
|
+
for (const migration of migrations) {
|
|
517
|
+
this.connection.exec("BEGIN IMMEDIATE");
|
|
518
|
+
try {
|
|
519
|
+
const applied = this.connection.prepare(`
|
|
520
|
+
SELECT 1 AS applied
|
|
521
|
+
FROM atlas_schema_migrations
|
|
522
|
+
WHERE version = ?
|
|
523
|
+
`).get(migration.version);
|
|
524
|
+
if (applied === undefined) {
|
|
525
|
+
this.connection.exec(migration.sql);
|
|
526
|
+
this.connection.prepare(`
|
|
527
|
+
INSERT INTO atlas_schema_migrations (version, applied_at)
|
|
528
|
+
VALUES (?, ?)
|
|
529
|
+
`).run(migration.version, new Date().toISOString());
|
|
530
|
+
}
|
|
531
|
+
this.connection.exec("COMMIT");
|
|
532
|
+
}
|
|
533
|
+
catch (error) {
|
|
534
|
+
this.connection.exec("ROLLBACK");
|
|
535
|
+
throw error;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
function requireExistingSharedDatabase(atlasDirectory, databasePath) {
|
|
541
|
+
const directory = lstatSync(atlasDirectory);
|
|
542
|
+
if (!directory.isDirectory() || directory.isSymbolicLink()) {
|
|
543
|
+
throw new Error("The Atlas store must be a real directory inside the target worktree");
|
|
544
|
+
}
|
|
545
|
+
if (resolve(realpathSync(atlasDirectory)) !== resolve(atlasDirectory)) {
|
|
546
|
+
throw new Error("The Atlas store resolves outside the target worktree");
|
|
547
|
+
}
|
|
548
|
+
const database = lstatSync(databasePath);
|
|
549
|
+
if (!database.isFile() || database.isSymbolicLink()) {
|
|
550
|
+
throw new Error("The shared Atlas database must be initialized by the structural backend");
|
|
551
|
+
}
|
|
552
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { GitRepository } from "../repository/types.js";
|
|
2
|
+
import type { RepositorySnapshot } from "../snapshots/types.js";
|
|
3
|
+
export interface StoredRepositorySnapshot {
|
|
4
|
+
readonly snapshot: RepositorySnapshot;
|
|
5
|
+
readonly createdAt: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class SnapshotStore implements Disposable {
|
|
8
|
+
#private;
|
|
9
|
+
readonly databasePath: string;
|
|
10
|
+
constructor(repository: GitRepository);
|
|
11
|
+
save(snapshot: RepositorySnapshot): void;
|
|
12
|
+
find(snapshotId: string): RepositorySnapshot | undefined;
|
|
13
|
+
findStored(snapshotId: string): StoredRepositorySnapshot | undefined;
|
|
14
|
+
latest(): RepositorySnapshot | undefined;
|
|
15
|
+
latestStored(): StoredRepositorySnapshot | undefined;
|
|
16
|
+
close(): void;
|
|
17
|
+
[Symbol.dispose](): void;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=snapshot-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"snapshot-store.d.ts","sourceRoot":"","sources":["../../src/storage/snapshot-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAGhE,MAAM,WAAW,wBAAwB;IACvC,QAAQ,CAAC,QAAQ,EAAE,kBAAkB,CAAC;IACtC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED,qBAAa,aAAc,YAAW,UAAU;;IAC9C,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;gBAIlB,UAAU,EAAE,aAAa;IAMrC,IAAI,CAAC,QAAQ,EAAE,kBAAkB,GAAG,IAAI;IA8BxC,IAAI,CAAC,UAAU,EAAE,MAAM,GAAG,kBAAkB,GAAG,SAAS;IAIxD,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,wBAAwB,GAAG,SAAS;IAepE,MAAM,IAAI,kBAAkB,GAAG,SAAS;IAIxC,YAAY,IAAI,wBAAwB,GAAG,SAAS;IAkBpD,KAAK,IAAI,IAAI;IAIb,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI;CAGzB"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { AtlasDatabase } from "./atlas-database.js";
|
|
2
|
+
export class SnapshotStore {
|
|
3
|
+
databasePath;
|
|
4
|
+
#atlasDatabase;
|
|
5
|
+
#repositoryId;
|
|
6
|
+
constructor(repository) {
|
|
7
|
+
this.#repositoryId = repository.repositoryId;
|
|
8
|
+
this.#atlasDatabase = new AtlasDatabase(repository);
|
|
9
|
+
this.databasePath = this.#atlasDatabase.databasePath;
|
|
10
|
+
}
|
|
11
|
+
save(snapshot) {
|
|
12
|
+
if (snapshot.repositoryId !== this.#repositoryId) {
|
|
13
|
+
throw new Error("Cannot store a snapshot from another repository");
|
|
14
|
+
}
|
|
15
|
+
const timestamp = new Date().toISOString();
|
|
16
|
+
const database = this.#atlasDatabase.connection;
|
|
17
|
+
database.exec("BEGIN IMMEDIATE");
|
|
18
|
+
try {
|
|
19
|
+
database.prepare(`
|
|
20
|
+
INSERT INTO atlas_repository_snapshots (
|
|
21
|
+
repository_id,
|
|
22
|
+
snapshot_id,
|
|
23
|
+
payload,
|
|
24
|
+
created_at
|
|
25
|
+
) VALUES (?, ?, ?, ?)
|
|
26
|
+
ON CONFLICT (repository_id, snapshot_id) DO NOTHING
|
|
27
|
+
`).run(this.#repositoryId, snapshot.snapshotId, JSON.stringify(snapshot), timestamp);
|
|
28
|
+
database.prepare(`
|
|
29
|
+
UPDATE atlas_repositories
|
|
30
|
+
SET latest_snapshot_id = ?, updated_at = ?
|
|
31
|
+
WHERE repository_id = ?
|
|
32
|
+
`).run(snapshot.snapshotId, timestamp, this.#repositoryId);
|
|
33
|
+
database.exec("COMMIT");
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
database.exec("ROLLBACK");
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
find(snapshotId) {
|
|
41
|
+
return this.findStored(snapshotId)?.snapshot;
|
|
42
|
+
}
|
|
43
|
+
findStored(snapshotId) {
|
|
44
|
+
const row = this.#atlasDatabase.connection.prepare(`
|
|
45
|
+
SELECT payload, created_at
|
|
46
|
+
FROM atlas_repository_snapshots
|
|
47
|
+
WHERE repository_id = ? AND snapshot_id = ?
|
|
48
|
+
`).get(this.#repositoryId, snapshotId);
|
|
49
|
+
return row === undefined ? undefined : {
|
|
50
|
+
snapshot: JSON.parse(row.payload),
|
|
51
|
+
createdAt: row.created_at,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
latest() {
|
|
55
|
+
return this.latestStored()?.snapshot;
|
|
56
|
+
}
|
|
57
|
+
latestStored() {
|
|
58
|
+
const row = this.#atlasDatabase.connection.prepare(`
|
|
59
|
+
SELECT snapshot.payload, snapshot.created_at
|
|
60
|
+
FROM atlas_repositories AS repository
|
|
61
|
+
JOIN atlas_repository_snapshots AS snapshot
|
|
62
|
+
ON snapshot.repository_id = repository.repository_id
|
|
63
|
+
AND snapshot.snapshot_id = repository.latest_snapshot_id
|
|
64
|
+
WHERE repository.repository_id = ?
|
|
65
|
+
`).get(this.#repositoryId);
|
|
66
|
+
return row === undefined ? undefined : {
|
|
67
|
+
snapshot: JSON.parse(row.payload),
|
|
68
|
+
createdAt: row.created_at,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
close() {
|
|
72
|
+
this.#atlasDatabase.close();
|
|
73
|
+
}
|
|
74
|
+
[Symbol.dispose]() {
|
|
75
|
+
this.close();
|
|
76
|
+
}
|
|
77
|
+
}
|