diff --git a/package.json b/package.json index 93e658a..1ff61ca 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "e2e": "playwright test", "check": "tsc --noEmit", "verify": "tsx scripts/verify.ts", - "fixtures": "tsx scripts/fixtures.ts" + "fixtures": "tsx scripts/fixtures.ts", + "migrate": "tsx server/migrateLibraries.ts" }, "dependencies": { "@anthropic-ai/claude-agent-sdk": "^0.3.325", diff --git a/server/migrateLibraries.ts b/server/migrateLibraries.ts new file mode 100644 index 0000000..bcb3cc1 --- /dev/null +++ b/server/migrateLibraries.ts @@ -0,0 +1,427 @@ +/** + * From the single-library layout to the libraries layout (libraries.ts), once, when asked: + * + * npm run migrate migrate IE_DATA (or .data) + * npm run migrate -- --check a dry run: build, verify and report, writing nothing + * npm run migrate -- [--check] DIR another data directory + * + * The library's text is what must come across, not its history: the tree is copied, in this + * process, into a new library with a new id and a history of one commit ("Bring the library over"), + * with `Doc.graft`, which carries everything that is content: titles, bodies, kinds, notes with + * their anchors (a note on the root's own text too), who wrote each node, locks, forms and lenses, + * targets, declined phrases, the Wikipedia container and its articles' notes, the order of + * everything, and the hashes each node was last reconciled with (so nothing turns stale). Every + * reference to a node is then rewritten through the map of old ids to new: node targets (links, + * cards) get the new id and the titles down to it from the root, as the page would write them; a + * target that already named nothing keeps its hint, put in today's form (an old target's document + * slug becomes that top's title). Open conflicts come along with their ids rewritten. The link + * reviewer's verdicts, people's decisions and pending suggestions (links/pending.json) are + * re-keyed to the new ids; vectors are rebuilt when first needed. + * + * Before anything is written, the copy is checked against the original node by node: the same tree + * (each node's parent and children, in order), the same number of nodes, the same title, body, + * kind, author, lock, form, lens, flags, declined phrases, anchors (start, end, quoted and current + * words) and staleness for each, every target that found its node before finding the same node + * (by its new id) now, and the same number of open conflicts. It is checked again after being + * written out and read back, and again from the file on disk. Any difference stops the migration + * with a report, and the data directory is left as it was. + * + * Then: the libraries are written to libraries.tmp-