feat: add incremental spacetime migration import
Some checks failed
CI / verify (push) Has been cancelled
Some checks failed
CI / verify (push) Has been cancelled
This commit is contained in:
@@ -16,6 +16,9 @@ try {
|
||||
if (!options.in) {
|
||||
throw new Error('必须传入 --in。');
|
||||
}
|
||||
if (options.incremental === true && options.replaceExisting === true) {
|
||||
throw new Error('--incremental 不能和 --replace-existing 同时使用。');
|
||||
}
|
||||
|
||||
const inPath = path.resolve(options.in);
|
||||
await assertReadableFile(inPath);
|
||||
@@ -75,13 +78,69 @@ async function prepareWebImportOptions(options) {
|
||||
}
|
||||
|
||||
async function importMigrationJsonDirect(options, migrationJson) {
|
||||
const includeTables = resolveImportIncludeTables(options, migrationJson);
|
||||
const procedureName =
|
||||
options.incremental === true
|
||||
? 'import_database_migration_incremental_from_file'
|
||||
: 'import_database_migration_from_file';
|
||||
const input = {
|
||||
migration_json: migrationJson,
|
||||
include_tables: options.includeTables,
|
||||
include_tables: includeTables,
|
||||
replace_existing: options.replaceExisting === true,
|
||||
dry_run: options.dryRun === true,
|
||||
};
|
||||
return callSpacetimeProcedure(options, 'import_database_migration_from_file', input);
|
||||
if (options.replaceExisting === true) {
|
||||
console.log(
|
||||
`[spacetime:migration:import] replace-existing 仅覆盖本次文件内的表: ${includeTables.join(', ') || '无'}`,
|
||||
);
|
||||
} else if (options.incremental === true) {
|
||||
console.log(`[spacetime:migration:import] 使用增量模式,已存在或冲突的行会跳过`);
|
||||
}
|
||||
return callSpacetimeProcedure(options, procedureName, input);
|
||||
}
|
||||
|
||||
function resolveImportIncludeTables(options, migrationJson) {
|
||||
if (options.replaceExisting !== true) {
|
||||
return options.includeTables;
|
||||
}
|
||||
|
||||
const migrationTables = readMigrationTableNames(migrationJson);
|
||||
if (options.includeTables.length === 0) {
|
||||
return migrationTables;
|
||||
}
|
||||
|
||||
const requestedTables = new Set(options.includeTables);
|
||||
return migrationTables.filter((tableName) => requestedTables.has(tableName));
|
||||
}
|
||||
|
||||
function readMigrationTableNames(migrationJson) {
|
||||
let payload;
|
||||
try {
|
||||
payload = JSON.parse(migrationJson);
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`迁移文件 JSON 解析失败: ${error instanceof Error ? error.message : String(error)}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (!payload || !Array.isArray(payload.tables)) {
|
||||
throw new Error('迁移文件缺少 tables 数组。');
|
||||
}
|
||||
|
||||
const tableNames = [];
|
||||
const seen = new Set();
|
||||
for (const table of payload.tables) {
|
||||
if (!table || typeof table.name !== 'string' || !table.name.trim()) {
|
||||
throw new Error('迁移文件 tables 内存在缺少 name 的表项。');
|
||||
}
|
||||
const tableName = table.name.trim();
|
||||
if (!seen.has(tableName)) {
|
||||
tableNames.push(tableName);
|
||||
seen.add(tableName);
|
||||
}
|
||||
}
|
||||
|
||||
return tableNames;
|
||||
}
|
||||
|
||||
async function revokeTemporaryWebIdentity(options) {
|
||||
|
||||
@@ -67,6 +67,8 @@ export function parseArgs(argv) {
|
||||
.filter(Boolean);
|
||||
} else if (arg === '--replace-existing') {
|
||||
options.replaceExisting = true;
|
||||
} else if (arg === '--incremental') {
|
||||
options.incremental = true;
|
||||
} else if (arg === '--dry-run') {
|
||||
options.dryRun = true;
|
||||
} else if (arg === '--anonymous' || arg === '--no-config') {
|
||||
|
||||
Reference in New Issue
Block a user