Add SpacetimeDB conflict migration flow
This commit is contained in:
@@ -3,7 +3,9 @@
|
||||
import { writeFile } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import {
|
||||
callSpacetimeProcedureAuto,
|
||||
callSpacetimeProcedure,
|
||||
callSpacetimeProcedureViaCli,
|
||||
createSpacetimeWebIdentity,
|
||||
ensureParentDir,
|
||||
ensureProcedureOk,
|
||||
parseArgs,
|
||||
@@ -18,11 +20,13 @@ try {
|
||||
const input = {
|
||||
include_tables: options.includeTables,
|
||||
};
|
||||
const result = await callSpacetimeProcedureAuto(
|
||||
options,
|
||||
'export_database_migration_to_file',
|
||||
input,
|
||||
);
|
||||
const webOptions = await prepareWebExportOptions(options);
|
||||
let result;
|
||||
try {
|
||||
result = await callSpacetimeProcedure(webOptions, 'export_database_migration_to_file', input);
|
||||
} finally {
|
||||
await revokeTemporaryWebIdentity(webOptions);
|
||||
}
|
||||
ensureProcedureOk(result);
|
||||
|
||||
if (typeof result.migration_json !== 'string' || result.migration_json.trim() === '') {
|
||||
@@ -35,6 +39,7 @@ try {
|
||||
|
||||
console.log(`[spacetime:migration:export] 已写入 ${outPath}`);
|
||||
printTableStats(result.table_stats);
|
||||
printMigrationWarnings(result.warnings);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`[spacetime:migration:export] ${error instanceof Error ? error.message : String(error)}`,
|
||||
@@ -42,6 +47,58 @@ try {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
async function prepareWebExportOptions(options) {
|
||||
if (options.token) {
|
||||
return { ...options, useHttp: true };
|
||||
}
|
||||
|
||||
const identity = await createSpacetimeWebIdentity(options);
|
||||
console.log(
|
||||
`[spacetime:migration:export] 已通过 Web API 创建临时 identity: ${identity.identity}`,
|
||||
);
|
||||
|
||||
const authorizeResult = await callSpacetimeProcedureViaCli(
|
||||
options,
|
||||
'authorize_database_migration_operator',
|
||||
{
|
||||
bootstrap_secret: options.bootstrapSecret || '',
|
||||
operator_identity_hex: identity.identity,
|
||||
note: options.note || 'temporary web api migration export',
|
||||
},
|
||||
);
|
||||
ensureProcedureOk(authorizeResult);
|
||||
console.log(`[spacetime:migration:export] 已授权临时 Web API identity`);
|
||||
|
||||
return {
|
||||
...options,
|
||||
token: identity.token,
|
||||
temporaryWebIdentity: identity.identity,
|
||||
useHttp: true,
|
||||
};
|
||||
}
|
||||
|
||||
async function revokeTemporaryWebIdentity(options) {
|
||||
if (!options.temporaryWebIdentity) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const revokeResult = await callSpacetimeProcedure(
|
||||
options,
|
||||
'revoke_database_migration_operator',
|
||||
{ operator_identity_hex: options.temporaryWebIdentity },
|
||||
);
|
||||
ensureProcedureOk(revokeResult);
|
||||
console.log(`[spacetime:migration:export] 已撤销临时 Web API identity`);
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
`[spacetime:migration:export] 撤销临时 Web API identity 失败: ${
|
||||
error instanceof Error ? error.message : String(error)
|
||||
}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function printTableStats(tableStats) {
|
||||
if (!Array.isArray(tableStats) || tableStats.length === 0) {
|
||||
return;
|
||||
@@ -53,3 +110,18 @@ function printTableStats(tableStats) {
|
||||
}));
|
||||
console.table(rows);
|
||||
}
|
||||
|
||||
function printMigrationWarnings(warnings) {
|
||||
if (!Array.isArray(warnings) || warnings.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.warn('[spacetime:migration:export] 迁移告警:');
|
||||
console.table(
|
||||
warnings.map((warning) => ({
|
||||
table: warning.table_name,
|
||||
kind: warning.warning_kind,
|
||||
message: warning.message,
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user