feat: add spacetimedb json migration tooling
Some checks failed
CI / verify (push) Has been cancelled
Some checks failed
CI / verify (push) Has been cancelled
This commit is contained in:
60
scripts/spacetime-import-migration-json.mjs
Normal file
60
scripts/spacetime-import-migration-json.mjs
Normal file
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import {
|
||||
assertReadableFile,
|
||||
callSpacetimeProcedureAuto,
|
||||
ensureProcedureOk,
|
||||
parseArgs,
|
||||
} from './spacetime-migration-common.mjs';
|
||||
|
||||
try {
|
||||
const options = parseArgs(process.argv.slice(2));
|
||||
if (!options.in) {
|
||||
throw new Error('必须传入 --in。');
|
||||
}
|
||||
|
||||
const inPath = path.resolve(options.in);
|
||||
await assertReadableFile(inPath);
|
||||
const migrationJson = await readFile(inPath, 'utf8');
|
||||
if (!migrationJson.trim()) {
|
||||
throw new Error(`迁移文件为空: ${inPath}`);
|
||||
}
|
||||
|
||||
const input = {
|
||||
migration_json: migrationJson,
|
||||
include_tables: options.includeTables,
|
||||
replace_existing: options.replaceExisting === true,
|
||||
dry_run: options.dryRun === true,
|
||||
};
|
||||
const result = await callSpacetimeProcedureAuto(
|
||||
options,
|
||||
'import_database_migration_from_file',
|
||||
input,
|
||||
);
|
||||
ensureProcedureOk(result);
|
||||
|
||||
console.log(
|
||||
`[spacetime:migration:import] ${options.dryRun ? 'dry-run 完成' : '导入完成'}: ${inPath}`,
|
||||
);
|
||||
printTableStats(result.table_stats);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`[spacetime:migration:import] ${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,
|
||||
imported: stat.imported_row_count,
|
||||
skipped: stat.skipped_row_count,
|
||||
}));
|
||||
console.table(rows);
|
||||
}
|
||||
Reference in New Issue
Block a user