feat: add spacetimedb json migration tooling
Some checks failed
CI / verify (push) Has been cancelled

This commit is contained in:
2026-04-27 14:54:26 +08:00
parent ded6f6ee2a
commit 9a79494c68
13 changed files with 1532 additions and 2 deletions

View File

@@ -0,0 +1,55 @@
#!/usr/bin/env node
import { writeFile } from 'node:fs/promises';
import path from 'node:path';
import {
callSpacetimeProcedureAuto,
ensureParentDir,
ensureProcedureOk,
parseArgs,
} from './spacetime-migration-common.mjs';
try {
const options = parseArgs(process.argv.slice(2));
if (!options.out) {
throw new Error('必须传入 --out。');
}
const input = {
include_tables: options.includeTables,
};
const result = await callSpacetimeProcedureAuto(
options,
'export_database_migration_to_file',
input,
);
ensureProcedureOk(result);
if (typeof result.migration_json !== 'string' || result.migration_json.trim() === '') {
throw new Error('导出 procedure 没有返回 migration_json。');
}
const outPath = path.resolve(options.out);
await ensureParentDir(outPath);
await writeFile(outPath, result.migration_json, 'utf8');
console.log(`[spacetime:migration:export] 已写入 ${outPath}`);
printTableStats(result.table_stats);
} catch (error) {
console.error(
`[spacetime:migration:export] ${error instanceof Error ? error.message : String(error)}`,
);
process.exit(1);
}
function printTableStats(tableStats) {
if (!Array.isArray(tableStats) || tableStats.length === 0) {
return;
}
const rows = tableStats.map((stat) => ({
table: stat.table_name,
exported: stat.exported_row_count,
}));
console.table(rows);
}