Files
one_divine_lot/scripts/migrate-json-to-sqlite.mjs
T
kyugao a4902bb730 feat(R-013+重构): 策略自定义字段需求文档 + 结构归类优化
需求与文档:
- R-013 策略自定义字段配置(定义随策略 configSchema 存 settings、值落
  strategy_holdings.values JSON、类型化文本/数字/布尔/枚举)定稿并进入
  PLAN-012 / 迭代 11(含目标数据模型 + 端到端示例 + 技术方案 + 验收标准)
- R-011(Tab设置)/ R-012(UI主题适配)归档至 已完成/,索引标记已归档
- 沉淀约束: 产品约束-010 / 技术约束-015 / 技术约束-016 / UI约束-005

结构重构(技术约束-016):
- src/component/ 平铺还原为语义分域目录: data-source/ storage/ position/
  market/ trades/(git rename 保留历史)
- 清理死代码: 删除 AllocationStorage.js(0 引用)、DataStore
  setDataset/removeDataset(无调用方)
- 同步更新 src/index.js / scripts/*.mjs / tsdown.config.ts 引用
- typecheck + build + test-r009 回归 14/14 通过
2026-09-02 15:38:51 +08:00

44 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 一次性迁移脚本:旧 JSON data store → SQLiteR-008 / 迭代 06D6
*
* 用途:
* - 独立执行(手动 / CI),逻辑与 DataStore 启动自动迁移共用(都调用 SqliteStore.migrateJson
* - 手动/CI 环境下可显式触发迁移,无需等插件启动
*
* 用法:
* node scripts/migrate-json-to-sqlite.mjs # 默认数据目录 ~/.dsh/one-divine-lot
* ODL_TEST_DATA_DIR=/tmp/xxx node scripts/migrate-json-to-sqlite.mjs # 指定数据目录(测试隔离,技术约束-011)
*
* 行为(与启动自动迁移一致,幂等):
* 1. 检测 SQLite 是否为空(strategy_holdings / market_quotes_cache 均无数据)——非空则跳过;
* 2. store.json → strategy_holdings(策略为中心平铺,created_at=迁移时间戳);
* 3. store.market.json → market_quotes_cache(抽取 lastPrice/lastClose 两列);
* 4. 旧 allocations.json(若存在)→ strategy_holdingscode 为中心聚合,跳过重复);
* 5. 迁移前备份 JSON 为 *.bak;迁移失败不破坏原文件。
*/
import { SqliteStore } from '../lib/storage/SqliteStore.js';
async function main() {
const store = new SqliteStore({}); // dataDir 默认 ~/.dsh/one-divine-lot,或 ODL_TEST_DATA_DIR
try {
if (!store.isEmpty()) {
console.log('[migrate] SQLite 已有数据,跳过迁移(幂等)');
return;
}
const result = await store.migrateJson();
console.log('[migrate] 迁移完成:', JSON.stringify(result));
if (result.holdings === 0 && result.quotes === 0) {
console.log('[migrate] 无数据可迁移(store.json / store.market.json / allocations.json 均不存在或为空)');
} else {
console.log('[migrate] 旧 JSON 已备份为 *.bak,可安全删除或保留');
}
} catch (err) {
console.error('[migrate] 迁移失败:', err.message);
process.exitCode = 1;
} finally {
store.close();
}
}
main();