nx-kv 0.28.0 → 0.32.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/CHANGELOG.md +198 -0
- package/README.md +15 -1
- package/assets/kv-get/SKILL.md +3 -1
- package/assets/kv-get/references/localget.md +4 -3
- package/assets/kv-get/references/timegoal.md +2 -1
- package/assets/kv-get/references/wait-workflow.md +44 -14
- package/assets/kv-get/references/waitloop.md +6 -4
- package/bin/cli.mjs +18 -1
- package/package.json +7 -4
- package/src/core/atomic-file.js +45 -0
- package/src/core/attachmentdb.js +726 -0
- package/src/core/attachstore.js +123 -80
- package/src/core/config.js +83 -13
- package/src/core/db/bootstrap.js +424 -0
- package/src/core/db/driver.js +234 -0
- package/src/core/db/errors.js +144 -0
- package/src/core/db/import-json.js +1224 -0
- package/src/core/db/index.js +122 -0
- package/src/core/db/migrations/index.js +103 -0
- package/src/core/db/migrations/v1.sql +850 -0
- package/src/core/db/migrations/v2-data.js +149 -0
- package/src/core/db/migrations/v2.sql +89 -0
- package/src/core/db/migrator.js +203 -0
- package/src/core/db/quiet.js +19 -0
- package/src/core/db/seed-templates.js +265 -0
- package/src/core/db/settings.js +54 -0
- package/src/core/db/time.js +96 -0
- package/src/core/db/tx.js +188 -0
- package/src/core/db/verify.js +295 -0
- package/src/core/db/version.js +59 -0
- package/src/core/errors.js +7 -0
- package/src/core/instruction-template.js +129 -94
- package/src/core/keys.js +21 -20
- package/src/core/localatt.js +76 -57
- package/src/core/paths.js +39 -2
- package/src/core/topic-key.js +43 -0
- package/src/core/workflow/project.js +3 -2
- package/src/core/workflow/readiness.js +13 -6
- package/src/core/workflow/types.js +6 -5
- package/src/modules/db/index.js +54 -0
- package/src/modules/db/service.js +273 -0
- package/src/modules/docs/brief.js +11 -1
- package/src/modules/docs/index.js +292 -7
- package/src/modules/docs/knowledge.js +8 -1
- package/src/modules/docs/paths.js +2 -1
- package/src/modules/docs/refs.js +1 -1
- package/src/modules/docs/store.js +19 -1
- package/src/modules/file/index.js +20 -0
- package/src/modules/file/service.js +68 -0
- package/src/modules/localtask/deletion.js +14 -10
- package/src/modules/localtask/events.js +16 -7
- package/src/modules/localtask/index.js +161 -81
- package/src/modules/localtask/move-prims.js +68 -38
- package/src/modules/localtask/move.js +1 -1
- package/src/modules/localtask/panel-presence.js +5 -5
- package/src/modules/localtask/paths.js +16 -18
- package/src/modules/localtask/repo/goals.js +154 -0
- package/src/modules/localtask/repo/index.js +42 -0
- package/src/modules/localtask/repo/links.js +136 -0
- package/src/modules/localtask/repo/open.js +71 -0
- package/src/modules/localtask/repo/presence.js +117 -0
- package/src/modules/localtask/repo/rows.js +157 -0
- package/src/modules/localtask/repo/settings.js +384 -0
- package/src/modules/localtask/repo/snapshot.js +154 -0
- package/src/modules/localtask/repo/tasks.js +333 -0
- package/src/modules/localtask/repo/topics.js +293 -0
- package/src/modules/localtask/service.js +15 -0
- package/src/modules/localtask/store/index.js +8 -9
- package/src/modules/localtask/store/topic-adapter.js +8 -43
- package/src/modules/localtask/store-io.js +19 -161
- package/src/modules/localtask/task-lifecycle.js +216 -148
- package/src/modules/localtask/task-ops.js +195 -114
- package/src/modules/localtask/task-remove.js +151 -79
- package/src/modules/localtask/task-templates.js +41 -62
- package/src/modules/localtask/task-view.js +165 -151
- package/src/modules/localtask/topics.js +89 -80
- package/src/modules/localtask/waiters.js +6 -14
- package/src/modules/localtask/waiting.js +31 -44
- package/src/modules/repos/boards.js +44 -14
- package/src/modules/repos/index.js +28 -2
- package/src/modules/repos/keys.js +2 -1
- package/src/modules/repos/ref.js +6 -6
- package/src/modules/repos/repo/index.js +53 -0
- package/src/modules/repos/repo/open.js +84 -0
- package/src/modules/repos/repo/refs.js +165 -0
- package/src/modules/repos/repo/repos.js +177 -0
- package/src/modules/repos/repo/rows.js +174 -0
- package/src/modules/repos/service.js +375 -130
- package/src/modules/repos/transfer.js +43 -14
- package/src/modules/tasktool/attach.js +732 -86
- package/src/modules/tasktool/delivery-template.js +50 -64
- package/src/modules/tasktool/get.js +6 -27
- package/src/modules/tasktool/groupctx.js +5 -2
- package/src/modules/tasktool/plugins/timegoal-store.js +476 -363
- package/src/modules/tasktool/plugins/waitgoal-config.js +1 -1
- package/src/modules/tasktool/preview.js +28 -34
- package/src/modules/tasktool/system-blocks.js +78 -0
- package/src/modules/tasktool/wait.js +113 -125
- package/src/modules/templates/index.js +19 -4
- package/src/modules/templates/repo/index.js +16 -0
- package/src/modules/templates/repo/open.js +65 -0
- package/src/modules/templates/repo/templates.js +133 -0
- package/src/modules/templates/service.js +130 -119
- package/src/runtime/registry.js +2 -1
- package/src/shared/prompt-blocks.js +307 -0
- package/src/shared/repo-key.js +57 -0
- package/src/shared/task-status.js +105 -0
- package/src/web/frontend/App.jsx +78 -45
- package/src/web/frontend/components/Introduction/docs-modal.jsx +306 -0
- package/src/web/frontend/components/Introduction/index.jsx +23 -72
- package/src/web/frontend/components/Introduction/style.css +35 -28
- package/src/web/frontend/components/board/member-picker.jsx +13 -3
- package/src/web/frontend/components/button/index.jsx +91 -0
- package/src/web/frontend/components/button/style.css +45 -0
- package/src/web/frontend/components/doc/doc-head.jsx +110 -0
- package/src/web/frontend/components/layout/breadcrumb.jsx +24 -13
- package/src/web/frontend/components/layout/crumbs.js +22 -0
- package/src/web/frontend/components/layout/route-crumb.jsx +2 -2
- package/src/web/frontend/components/md/attach-area.jsx +307 -0
- package/src/web/frontend/components/md/attach-common.js +95 -0
- package/src/web/frontend/components/md/attach-modal.jsx +387 -0
- package/src/web/frontend/components/md/attach-picker.jsx +274 -0
- package/src/web/frontend/components/md/composer-launch.js +68 -0
- package/src/web/frontend/components/md/composer-panel.jsx +167 -0
- package/src/web/frontend/components/md/md-panes.jsx +337 -46
- package/src/web/frontend/components/md/sel-menu.jsx +37 -210
- package/src/web/frontend/components/md/task-chain.jsx +2 -5
- package/src/web/frontend/components/md/tool-bar.jsx +75 -40
- package/src/web/frontend/components/repos/desc-modal.jsx +1 -1
- package/src/web/frontend/components/repos/transfer-panel.jsx +4 -0
- package/src/web/frontend/components/sidebar/index.jsx +5 -16
- package/src/web/frontend/components/sidebar/panel.jsx +2 -0
- package/src/web/frontend/components/sidebar/task-children.jsx +11 -1
- package/src/web/frontend/modules/board/board-view.js +31 -13
- package/src/web/frontend/modules/md/attach-mention.js +48 -0
- package/src/web/frontend/modules/md/md-edit-ops.js +24 -0
- package/src/web/frontend/modules/md/md-parse.js +40 -10
- package/src/web/frontend/modules/md/style.css +283 -39
- package/src/web/frontend/modules/md/use-templates.js +3 -1
- package/src/web/frontend/modules/prompts/placeholders.js +0 -13
- package/src/web/frontend/modules/prompts/prompt-editor.jsx +12 -111
- package/src/web/frontend/modules/prompts/style.css +0 -14
- package/src/web/frontend/page/dag/index.jsx +3 -1
- package/src/web/frontend/page/dir/deleted-panel.jsx +63 -0
- package/src/web/frontend/page/dir/index.jsx +137 -4
- package/src/web/frontend/page/dir/landing.jsx +136 -8
- package/src/web/frontend/page/dir/task.jsx +153 -83
- package/src/web/frontend/page/doc/index.jsx +150 -19
- package/src/web/frontend/page/group/index.jsx +39 -2
- package/src/web/frontend/page/overview/index.jsx +11 -5
- package/src/web/frontend/page/refs/index.jsx +122 -0
- package/src/web/frontend/page/refs/style.css +41 -0
- package/src/web/frontend/page/repos/index.jsx +565 -161
- package/src/web/frontend/page/settings/index.jsx +133 -105
- package/src/web/frontend/page/settings/prompt-templates-section.jsx +622 -0
- package/src/web/frontend/page/settings/style.css +161 -12
- package/src/web/frontend/page/settings/task-templates-section.jsx +120 -0
- package/src/web/frontend/page/templates/index.jsx +8 -34
- package/src/web/frontend/panels.jsx +24 -0
- package/src/web/frontend/route-map.js +67 -0
- package/src/web/frontend/style-v2.css +45 -19
- package/src/web/frontend/style.css +443 -15
- package/src/web/frontend/workspace/areas.js +3 -0
- package/src/web/frontend/workspace.js +148 -17
- package/src/web/public/assets/Introduction-BcPrmROW.css +1 -0
- package/src/web/public/assets/Introduction-MAkTZvUX.js +1 -0
- package/src/web/public/assets/{attach-v8BxRJ7p.js → attach-Bw0necIf.js} +1 -1
- package/src/web/public/assets/{cloud-DMotpyNl.js → cloud-CG_hgVn9.js} +3 -3
- package/src/web/public/assets/composer-launch-q1uPzWGo.js +1 -0
- package/src/web/public/assets/composer-panel-S2d6A3fG.js +1 -0
- package/src/web/public/assets/dag-CwY66RrZ.js +1 -0
- package/src/web/public/assets/dir-B_ySdQT1.js +5 -0
- package/src/web/public/assets/group-6sWouRDt.js +1 -0
- package/src/web/public/assets/index-DKncYqtS.js +54 -0
- package/src/web/public/assets/index-DO7b1M3E.css +1 -0
- package/src/web/public/assets/{md-edit-ops-BIXUZ5BM.js → md-edit-ops-ZwnwtEax.js} +10 -10
- package/src/web/public/assets/md-history-BxbOQtPI.js +1 -0
- package/src/web/public/assets/md-panes-Cso2NHbG.js +7 -0
- package/src/web/public/assets/member-picker-BpB9ZpWx.js +1 -0
- package/src/web/public/assets/overview-Bo8ivqRU.js +1 -0
- package/src/web/public/assets/{progress-DRI-n9Gb.js → progress-DPkGjn0P.js} +3 -3
- package/src/web/public/assets/refs-CY4i7PoC.css +1 -0
- package/src/web/public/assets/refs-CskU6rI7.js +1 -0
- package/src/web/public/assets/repos-B_tskNGw.js +9 -0
- package/src/web/public/assets/route-crumb-BZ6ffDRj.js +1 -0
- package/src/web/public/assets/settings-BMTd_p9Y.js +7 -0
- package/src/web/public/assets/settings-qCi-niJC.css +1 -0
- package/src/web/public/assets/task-Cs-9RNQn.js +7 -0
- package/src/web/public/assets/task-status-Bgo2Nfiw.js +1 -0
- package/src/web/public/assets/template-editor-CRxfV218.js +1 -0
- package/src/web/public/assets/templates-Apc3JKXa.js +2 -0
- package/src/web/public/assets/{upload-F3aoeT7X.js → upload-C83Bwy06.js} +1 -1
- package/src/web/public/index.html +4 -3
- package/src/core/json-store.js +0 -219
- package/src/web/frontend/modules/prompts/template-slots.jsx +0 -87
- package/src/web/frontend/page/settings/prompts-section.jsx +0 -531
- package/src/web/public/assets/Introduction-9gBNccF6.js +0 -1
- package/src/web/public/assets/Introduction-B4k08SH9.css +0 -1
- package/src/web/public/assets/dag-B9_B7rnK.js +0 -1
- package/src/web/public/assets/dir-DdMpgTHK.js +0 -3
- package/src/web/public/assets/group-BF2tfUqG.js +0 -1
- package/src/web/public/assets/index-BbAo8n1E.js +0 -50
- package/src/web/public/assets/index-DUxHMjZA.css +0 -1
- package/src/web/public/assets/md-history-DunAB9Pp.js +0 -5
- package/src/web/public/assets/member-picker-CD66NFHl.js +0 -1
- package/src/web/public/assets/overview-_Xza9umy.js +0 -1
- package/src/web/public/assets/repos-BfbPRMJ0.js +0 -10
- package/src/web/public/assets/route-crumb-BHjAXhIY.js +0 -1
- package/src/web/public/assets/settings-CVOnFmxp.js +0 -12
- package/src/web/public/assets/settings-knWtG9Kc.css +0 -1
- package/src/web/public/assets/task-Nw-0PEB4.js +0 -7
- package/src/web/public/assets/template-slots-UVa4FnPn.css +0 -1
- package/src/web/public/assets/template-slots-bXUEg7EO.js +0 -1
- package/src/web/public/assets/templates-cWbR2cCh.js +0 -2
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// core/db 的门面:进程级单例 + 便捷再导出。
|
|
2
|
+
//
|
|
3
|
+
// 上层(repo / service / 测试)只认这里的四个函数:
|
|
4
|
+
// openStore() 首次调用执行 bootstrap(探测 → 建表/导入/升级),之后返回同一个连接
|
|
5
|
+
// getDb() 拿当前连接(没开就开)
|
|
6
|
+
// closeDb() 关连接 + 清单例(**测试删库前必须先调**:Windows 上连接开着会 EBUSY)
|
|
7
|
+
// dbPath() 库文件路径(每次现取 —— 测试靠改 NX_KV_DB 换路径)
|
|
8
|
+
//
|
|
9
|
+
// 为什么是**进程级单例**:SQLite 的写锁是"一个连接一个事务",同一进程里开两条
|
|
10
|
+
// 连接去写同一份库,等于自己跟自己抢锁(`SQLITE_BUSY`)。多进程并发靠 WAL +
|
|
11
|
+
// busy_timeout,进程内一律共用一条连接。
|
|
12
|
+
import { dbPathFromEnv, defaultStoreTarget } from '../paths.js';
|
|
13
|
+
import { openAndBootstrap, inspectStore } from './bootstrap.js';
|
|
14
|
+
import { currentDb, hasDefaultDb, setDefaultDb, resetDefaultDb, tx, txDepth } from './tx.js';
|
|
15
|
+
|
|
16
|
+
/** @type {{db: object, result: object, existedBefore: boolean}|null} */
|
|
17
|
+
let singleton = null;
|
|
18
|
+
/** 当前单例是按哪个目标打开的(env 换库检测用)。 */
|
|
19
|
+
let openedForTarget = null;
|
|
20
|
+
|
|
21
|
+
/** 库文件路径(`NX_KV_DB` 优先,否则 `APP_DIR/nxkv.db`)。**每次现取**。 */
|
|
22
|
+
export function dbPath() {
|
|
23
|
+
return dbPathFromEnv();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 打开(或复用)进程级连接,必要时完成建表 / 导入 / 升级。
|
|
28
|
+
*
|
|
29
|
+
* @param {object} [opts]
|
|
30
|
+
* @param {string} [opts.path] 库路径(缺省 `dbPath()`)
|
|
31
|
+
* @param {string} [opts.appDir] legacy 数据目录(缺省 `APP_DIR`)
|
|
32
|
+
* @param {boolean} [opts.importLegacyData=true] 是否执行 JSON 导入
|
|
33
|
+
* @param {boolean} [opts.dryRun=false] 收尾回滚(演练用)
|
|
34
|
+
* @param {boolean} [opts.readOnly=false] 只读连接(不建表、不升级)
|
|
35
|
+
* @returns {object} 连接
|
|
36
|
+
*/
|
|
37
|
+
export function openStore(opts = {}) {
|
|
38
|
+
const target = opts.path
|
|
39
|
+
? { path: opts.path, appDir: opts.appDir }
|
|
40
|
+
: { path: dbPathFromEnv(), appDir: defaultStoreTarget().appDir };
|
|
41
|
+
if (singleton && !singleton.db.closed) {
|
|
42
|
+
// env 指向了另一个库(测试中途换目录 / legacy env 切换)→ 关旧开新。
|
|
43
|
+
// 这是「读路径静默换库」的统一防线:任何裸调 openStore() 的调用方
|
|
44
|
+
//(config / templates / core 内部)都自动获得同一语义。
|
|
45
|
+
if (
|
|
46
|
+
!opts.path &&
|
|
47
|
+
openedForTarget &&
|
|
48
|
+
(openedForTarget.path !== target.path || (openedForTarget.appDir || '') !== (target.appDir || ''))
|
|
49
|
+
) {
|
|
50
|
+
closeDb();
|
|
51
|
+
} else {
|
|
52
|
+
return singleton.db;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const appDir = opts.appDir || target.appDir;
|
|
56
|
+
const { db, result, existedBefore } = openAndBootstrap({
|
|
57
|
+
path: target.path,
|
|
58
|
+
appDir,
|
|
59
|
+
importLegacyData: opts.importLegacyData !== false,
|
|
60
|
+
dryRun: opts.dryRun === true,
|
|
61
|
+
readOnly: opts.readOnly === true,
|
|
62
|
+
atMs: opts.atMs,
|
|
63
|
+
});
|
|
64
|
+
singleton = { db, result, existedBefore };
|
|
65
|
+
openedForTarget = { path: target.path, appDir };
|
|
66
|
+
setDefaultDb(db);
|
|
67
|
+
return db;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 当前连接(没开就开)。 */
|
|
71
|
+
export function getDb() {
|
|
72
|
+
if (singleton && !singleton.db.closed) return singleton.db;
|
|
73
|
+
return openStore();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 上一次 bootstrap 的结果(CLI 渲染"建库 / 导入 / 升级"用)。 */
|
|
77
|
+
export function getBootstrapResult() {
|
|
78
|
+
return singleton ? singleton.result : null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** 有没有已在使用的连接。 */
|
|
82
|
+
export function isStoreOpen() {
|
|
83
|
+
return !!(singleton && !singleton.db.closed);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 关连接并清单例。
|
|
88
|
+
* 测试删库文件之前必须调它 —— 否则 Windows 上 `rmSync` 必报 EBUSY,
|
|
89
|
+
* 而错误会在**下一个**测试里以"库文件已存在 / 数据不对"的形式出现。
|
|
90
|
+
*/
|
|
91
|
+
export function closeDb() {
|
|
92
|
+
if (singleton) {
|
|
93
|
+
try {
|
|
94
|
+
singleton.db.close();
|
|
95
|
+
} finally {
|
|
96
|
+
singleton = null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
openedForTarget = null;
|
|
100
|
+
resetDefaultDb();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** 测试用:等价于 closeDb()(名字表明意图,防止生产代码顺手调用)。 */
|
|
104
|
+
export function resetDbForTests() {
|
|
105
|
+
closeDb();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 库状态快照(只读;`db status` 用)。 */
|
|
109
|
+
export function storeStatus() {
|
|
110
|
+
const db = getDb();
|
|
111
|
+
return inspectStore(db);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// 便捷再导出:上层不必同时记三个文件路径。
|
|
115
|
+
export { tx, currentDb, hasDefaultDb, txDepth };
|
|
116
|
+
export { inspectStore } from './bootstrap.js';
|
|
117
|
+
export { openRaw, bindValue } from './driver.js';
|
|
118
|
+
export { CURRENT_SCHEMA_VERSION, REQUIRED_NODE_VERSION, checkNodeVersion } from './version.js';
|
|
119
|
+
export { StoreMigrationError, StoreTooNewError, NotNxKvStoreError } from './errors.js';
|
|
120
|
+
export { verifyStore, tableCounts, statusDistribution, findLegacyLocalTasks } from './verify.js';
|
|
121
|
+
export { topicKeyOf, GLOBAL_TOPIC_KEY } from '../topic-key.js';
|
|
122
|
+
export { nowMs, parseStampMs, parseIsoMs, msToStamp, stampFileTag } from './time.js';
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// 存储版本迁移区:每个版本一份**不可变** DDL 快照,一条边一个跃迁。
|
|
2
|
+
//
|
|
3
|
+
// 目录约定:
|
|
4
|
+
// v1.sql = 版本 1 的完整 schema(历史快照,永不修改)
|
|
5
|
+
// v2.sql = 1 → 2 的跃迁脚本(建 attachments/task_attach/ref_attach、删两张空表)
|
|
6
|
+
// index.js= 边映射表 + 路径规划(本文件)
|
|
7
|
+
//
|
|
8
|
+
// 规则(与 docs/decisions/versions 下的版本说明同一口径):
|
|
9
|
+
// · 只升不降,禁止跨版本回写旧结构;
|
|
10
|
+
// · 启动时按 EDGE_MAP 规划路径,缺边直接报错,不静默跳过;
|
|
11
|
+
// · 组合边(如 0→2)不写脚本,由直接边串联,EDGE_MAP 里显式登记。
|
|
12
|
+
import { readFileSync } from 'node:fs';
|
|
13
|
+
import { migrateAttachments } from './v2-data.js';
|
|
14
|
+
import { CURRENT_SCHEMA_VERSION } from '../version.js';
|
|
15
|
+
|
|
16
|
+
/** 每条直接边 = 一次版本跃迁。`apply` 与 DDL 在同一个事务里执行。 */
|
|
17
|
+
export const DIRECT_EDGES = [
|
|
18
|
+
{ from: 0, to: 1, file: 'v1.sql', title: '初始 schema(topics/tasks/refs/…)' },
|
|
19
|
+
{ from: 1, to: 2, file: 'v2.sql', title: '附件/资源统一模型', apply: migrateAttachments },
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
// 人读版本说明(docs/decisions 是决策的唯一权威位置,里面对应一份 date 化的 .md)
|
|
23
|
+
export const VERSION_NOTES = {
|
|
24
|
+
1: 'docs/decisions/versions/2026-11-08-v1.md',
|
|
25
|
+
2: 'docs/decisions/versions/2026-11-08-v2.md',
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const keyOf = (from, to) => `${from}->${to}`;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 显式登记所有可用边:直接边 + 由直接边串联出的组合边。
|
|
32
|
+
* 组合边记录 `via`(中间经过的版本),启动时按它逐步执行。
|
|
33
|
+
*/
|
|
34
|
+
export function buildEdgeMap() {
|
|
35
|
+
const map = new Map();
|
|
36
|
+
for (const e of DIRECT_EDGES) {
|
|
37
|
+
map.set(keyOf(e.from, e.to), { from: e.from, to: e.to, kind: 'direct', steps: [e], via: [e.to] });
|
|
38
|
+
}
|
|
39
|
+
// 递增版本的组合边(0..CURRENT 只有个位数,BFS 足够)。
|
|
40
|
+
for (const start of [0, ...DIRECT_EDGES.flatMap((e) => [e.from, e.to])]) {
|
|
41
|
+
for (const target of [0, ...DIRECT_EDGES.flatMap((e) => [e.from, e.to])]) {
|
|
42
|
+
if (start >= target) continue;
|
|
43
|
+
if (map.has(keyOf(start, target))) continue;
|
|
44
|
+
const steps = [];
|
|
45
|
+
let cur = start;
|
|
46
|
+
const via = [start];
|
|
47
|
+
while (cur < target) {
|
|
48
|
+
const next = DIRECT_EDGES.find((e) => e.from === cur && e.to <= target);
|
|
49
|
+
if (!next) break;
|
|
50
|
+
steps.push(next);
|
|
51
|
+
cur = next.to;
|
|
52
|
+
via.push(cur);
|
|
53
|
+
}
|
|
54
|
+
if (cur === target) {
|
|
55
|
+
map.set(keyOf(start, target), { from: start, to: target, kind: 'composed', steps, via });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return map;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const EDGE_MAP = buildEdgeMap();
|
|
63
|
+
|
|
64
|
+
/** 人读的边清单(CLI/文档用):直接边与组合边各一行。 */
|
|
65
|
+
export function describeEdges() {
|
|
66
|
+
return [...EDGE_MAP.values()].map((e) => ({
|
|
67
|
+
from: e.from,
|
|
68
|
+
to: e.to,
|
|
69
|
+
kind: e.kind,
|
|
70
|
+
via: e.via,
|
|
71
|
+
title: e.kind === 'direct' ? e.steps[0].title : `${e.via.join(' → ')}(组合)`,
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 规划 from → to 的迁移路径。
|
|
77
|
+
* @returns {Array<object>} 直接边步骤(按顺序)
|
|
78
|
+
* @throws 缺边时明确报错,列出当前已登记的边
|
|
79
|
+
*/
|
|
80
|
+
export function planMigrationPath(from, to = CURRENT_SCHEMA_VERSION) {
|
|
81
|
+
const a = Math.trunc(Number(from) || 0);
|
|
82
|
+
const b = Math.trunc(Number(to) || 0);
|
|
83
|
+
if (b < a) {
|
|
84
|
+
throw new Error(`存储版本不支持降级:${a} → ${b}(只允许单向升级)`);
|
|
85
|
+
}
|
|
86
|
+
if (a === b) return [];
|
|
87
|
+
const edge = EDGE_MAP.get(keyOf(a, b));
|
|
88
|
+
if (!edge) {
|
|
89
|
+
const known = describeEdges().map((e) => `${e.from}→${e.to}${e.kind === 'composed' ? `(经 ${e.via.join('→')})` : '(直接)'}`);
|
|
90
|
+
throw new Error(
|
|
91
|
+
`缺少迁移边 ${a} → ${b};当前可用: ${known.join('、') || '无'}。请先在 migrations/index.js 登记一条直接边。`
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
return edge.steps;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** 读一份版本快照 / 跃迁脚本(随包发布,SQL 保持可独立 review)。 */
|
|
98
|
+
export function readMigrationSql(file) {
|
|
99
|
+
return readFileSync(new URL('./' + file, import.meta.url), 'utf8');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** 所有直接边按目标版本排序(供 pendingSteps 使用)。 */
|
|
103
|
+
export const MIGRATION_STEPS = [...DIRECT_EDGES].sort((x, y) => x.to - y.to);
|