Add select domain for move tool (#7057)

Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov 2024-10-28 21:29:04 +05:00 committed by GitHub
parent 7a8ee3cf32
commit 3a37a16fc8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 21 deletions

View File

@ -67,24 +67,30 @@ async function moveWorkspace (
mongo: MongoClient, mongo: MongoClient,
pgClient: postgres.Sql, pgClient: postgres.Sql,
ws: Workspace, ws: Workspace,
region: string region: string,
include?: Set<string>
): Promise<void> { ): Promise<void> {
try { try {
const wsId = getWorkspaceId(ws.workspace) const wsId = getWorkspaceId(ws.workspace)
const mongoDB = getWorkspaceMongoDB(mongo, wsId) const mongoDB = getWorkspaceMongoDB(mongo, wsId)
const collections = await mongoDB.collections() const collections = await mongoDB.collections()
await createTable( let tables = collections.map((c) => c.collectionName)
pgClient, if (include !== undefined) {
collections.map((c) => c.collectionName) tables = tables.filter((t) => include.has(t))
) }
await createTable(pgClient, tables)
const token = generateToken(systemAccountEmail, wsId) const token = generateToken(systemAccountEmail, wsId)
const endpoint = await getTransactorEndpoint(token, 'external') const endpoint = await getTransactorEndpoint(token, 'external')
const connection = (await connect(endpoint, wsId, undefined, { const connection = (await connect(endpoint, wsId, undefined, {
model: 'upgrade' model: 'upgrade'
})) as unknown as Client & BackupClient })) as unknown as Client & BackupClient
for (const collection of collections) { for (const collection of collections) {
const cursor = collection.find()
const domain = translateDomain(collection.collectionName) const domain = translateDomain(collection.collectionName)
if (include !== undefined && !include.has(domain)) {
continue
}
const cursor = collection.find()
const current = await pgClient`SELECT _id FROM ${pgClient(domain)} WHERE "workspaceId" = ${ws.workspace}` const current = await pgClient`SELECT _id FROM ${pgClient(domain)} WHERE "workspaceId" = ${ws.workspace}`
const currentIds = new Set(current.map((r) => r._id)) const currentIds = new Set(current.map((r) => r._id))
console.log('move domain', domain) console.log('move domain', domain)
@ -131,7 +137,8 @@ export async function moveWorkspaceFromMongoToPG (
mongoUrl: string, mongoUrl: string,
dbUrl: string | undefined, dbUrl: string | undefined,
ws: Workspace, ws: Workspace,
region: string region: string,
include?: Set<string>
): Promise<void> { ): Promise<void> {
if (dbUrl === undefined) { if (dbUrl === undefined) {
throw new Error('dbUrl is required') throw new Error('dbUrl is required')
@ -141,7 +148,7 @@ export async function moveWorkspaceFromMongoToPG (
const pg = getDBClient(dbUrl) const pg = getDBClient(dbUrl)
const pgClient = await pg.getClient() const pgClient = await pg.getClient()
await moveWorkspace(accountDb, mongo, pgClient, ws, region) await moveWorkspace(accountDb, mongo, pgClient, ws, region, include)
pg.close() pg.close()
client.close() client.close()
} }

View File

@ -1666,21 +1666,39 @@ export function devTool (
}) })
}) })
program.command('move-workspace-to-pg <workspace> <region>').action(async (workspace: string, region: string) => { program
const { dbUrl } = prepareTools() .command('move-workspace-to-pg <workspace> <region>')
const mongodbUri = getMongoDBUrl() .option('-i, --include <include>', 'A list of ; separated domain names to include during backup', '*')
.action(
async (
workspace: string,
region: string,
cmd: {
include: string
}
) => {
const { dbUrl } = prepareTools()
const mongodbUri = getMongoDBUrl()
await withDatabase(mongodbUri, async (db) => { await withDatabase(mongodbUri, async (db) => {
const workspaceInfo = await getWorkspaceById(db, workspace) const workspaceInfo = await getWorkspaceById(db, workspace)
if (workspaceInfo === null) { if (workspaceInfo === null) {
throw new Error(`workspace ${workspace} not found`) throw new Error(`workspace ${workspace} not found`)
}
if (workspaceInfo.region === region) {
throw new Error(`workspace ${workspace} is already migrated`)
}
await moveWorkspaceFromMongoToPG(
db,
mongodbUri,
dbUrl,
workspaceInfo,
region,
cmd.include === '*' ? undefined : new Set(cmd.include.split(';').map((it) => it.trim()))
)
})
} }
if (workspaceInfo.region === region) { )
throw new Error(`workspace ${workspace} is already migrated`)
}
await moveWorkspaceFromMongoToPG(db, mongodbUri, dbUrl, workspaceInfo, region)
})
})
program.command('move-account-db-to-pg').action(async () => { program.command('move-account-db-to-pg').action(async () => {
const { dbUrl } = prepareTools() const { dbUrl } = prepareTools()