#!/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); }