@anchrd/intel-api 0.3.2 → 0.3.3
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.
|
@@ -2,15 +2,39 @@
|
|
|
2
2
|
-- `parent_id`, same folder grants, same immutable `knowledge_versions` rows, same R2 body
|
|
3
3
|
-- (ADR-0004 §1). Nothing here creates a second content model; only the CHECK has to learn the word.
|
|
4
4
|
--
|
|
5
|
-
-- SQLite cannot alter a CHECK constraint, so the table is rebuilt.
|
|
6
|
-
--
|
|
7
|
-
--
|
|
8
|
-
--
|
|
5
|
+
-- SQLite cannot alter a CHECK constraint, so the table is rebuilt. The rebuild is written the long
|
|
6
|
+
-- way round, and both detours are here because the short way was tried and D1 rolled it back. The
|
|
7
|
+
-- table this migration leaves behind is the same one either way — same columns, same constraints,
|
|
8
|
+
-- same indexes — so an installation that already applied this file keeps exactly what it has.
|
|
9
|
+
--
|
|
10
|
+
-- ⚠️ First detour: the new table is created under the final name rather than built beside the old
|
|
11
|
+
-- one and renamed over it. `DROP TABLE` on a parent runs an implicit `DELETE FROM` first, so the
|
|
12
|
+
-- moment the old `knowledge_nodes` goes, every row of `knowledge_versions`, `tree_grants` and
|
|
13
|
+
-- `flows` pointing at a node is a foreign-key violation. `defer_foreign_keys` postpones the
|
|
14
|
+
-- complaint to COMMIT but does not withdraw it, and `ALTER TABLE ... RENAME` does not settle it
|
|
15
|
+
-- either: a rename puts the name back, not the rows. Only inserting the nodes again, under the name
|
|
16
|
+
-- the children have referenced all along, does. On an empty database none of this is visible —
|
|
17
|
+
-- nothing points at anything — which is precisely how the first version passed a green test suite
|
|
18
|
+
-- and then failed against the first database that had content in it.
|
|
19
|
+
--
|
|
20
|
+
-- ⚠️ Second detour: `knowledge_links` is the only child of `knowledge_nodes` declared ON DELETE
|
|
21
|
+
-- CASCADE, so that same implicit delete does not merely flag its rows, it removes them — the
|
|
22
|
+
-- migration would have committed with every relationship between two documents quietly gone. The
|
|
23
|
+
-- rows are carried out of the way first and put back afterwards. That is a rescue, not a decision
|
|
24
|
+
-- about the data: nothing is dropped, rewritten or reinterpreted here.
|
|
9
25
|
PRAGMA defer_foreign_keys = TRUE;
|
|
10
26
|
|
|
11
|
-
|
|
27
|
+
-- Plain holding tables on purpose: no keys, no CHECKs, no foreign keys, and the column set taken
|
|
28
|
+
-- from whatever the live table has. Anything enforced here would only be enforced a second time on
|
|
29
|
+
-- the way back in, and a holding table that can reject a row is a holding table that can lose one.
|
|
30
|
+
CREATE TABLE knowledge_nodes_carry AS SELECT * FROM knowledge_nodes;
|
|
31
|
+
CREATE TABLE knowledge_links_carry AS SELECT * FROM knowledge_links;
|
|
32
|
+
|
|
33
|
+
DROP TABLE knowledge_nodes;
|
|
34
|
+
|
|
35
|
+
CREATE TABLE knowledge_nodes (
|
|
12
36
|
id TEXT PRIMARY KEY NOT NULL,
|
|
13
|
-
parent_id TEXT REFERENCES
|
|
37
|
+
parent_id TEXT REFERENCES knowledge_nodes(id),
|
|
14
38
|
kind TEXT NOT NULL CHECK (kind IN ('folder', 'document', 'attachment', 'table')),
|
|
15
39
|
title TEXT NOT NULL CHECK (length(title) BETWEEN 1 AND 240),
|
|
16
40
|
description TEXT CHECK (description IS NULL OR length(description) <= 2000),
|
|
@@ -22,18 +46,23 @@ CREATE TABLE knowledge_nodes_next (
|
|
|
22
46
|
archived_at TEXT
|
|
23
47
|
);
|
|
24
48
|
|
|
25
|
-
INSERT INTO
|
|
49
|
+
INSERT INTO knowledge_nodes (
|
|
26
50
|
id, parent_id, kind, title, description, context_policy, owner_id,
|
|
27
51
|
current_version_id, created_at, updated_at, archived_at
|
|
28
52
|
)
|
|
29
53
|
SELECT
|
|
30
54
|
id, parent_id, kind, title, description, context_policy, owner_id,
|
|
31
55
|
current_version_id, created_at, updated_at, archived_at
|
|
32
|
-
FROM
|
|
56
|
+
FROM knowledge_nodes_carry;
|
|
33
57
|
|
|
34
|
-
|
|
58
|
+
-- `OR IGNORE` because whether the cascade above actually fired is SQLite's business, not this
|
|
59
|
+
-- migration's: if it did, this puts the rows back; if it did not, each one is already present under
|
|
60
|
+
-- the same primary key and this is a no-op. Either way `knowledge_links` ends up holding exactly
|
|
61
|
+
-- what it held before, which is the only outcome this statement is permitted to have.
|
|
62
|
+
INSERT OR IGNORE INTO knowledge_links SELECT * FROM knowledge_links_carry;
|
|
35
63
|
|
|
36
|
-
|
|
64
|
+
DROP TABLE knowledge_nodes_carry;
|
|
65
|
+
DROP TABLE knowledge_links_carry;
|
|
37
66
|
|
|
38
67
|
CREATE INDEX knowledge_nodes_parent_idx ON knowledge_nodes(parent_id, archived_at, title);
|
|
39
68
|
CREATE INDEX knowledge_nodes_owner_idx ON knowledge_nodes(owner_id, archived_at);
|