Skip to content

Commit 3a37a16

Browse files
authored
Add select domain for move tool (#7057)
Signed-off-by: Denis Bykhov <[email protected]>
1 parent 7a8ee3c commit 3a37a16

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

dev/tool/src/db.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,30 @@ async function moveWorkspace (
6767
mongo: MongoClient,
6868
pgClient: postgres.Sql,
6969
ws: Workspace,
70-
region: string
70+
region: string,
71+
include?: Set<string>
7172
): Promise<void> {
7273
try {
7374
const wsId = getWorkspaceId(ws.workspace)
7475
const mongoDB = getWorkspaceMongoDB(mongo, wsId)
7576
const collections = await mongoDB.collections()
76-
await createTable(
77-
pgClient,
78-
collections.map((c) => c.collectionName)
79-
)
77+
let tables = collections.map((c) => c.collectionName)
78+
if (include !== undefined) {
79+
tables = tables.filter((t) => include.has(t))
80+
}
81+
82+
await createTable(pgClient, tables)
8083
const token = generateToken(systemAccountEmail, wsId)
8184
const endpoint = await getTransactorEndpoint(token, 'external')
8285
const connection = (await connect(endpoint, wsId, undefined, {
8386
model: 'upgrade'
8487
})) as unknown as Client & BackupClient
8588
for (const collection of collections) {
86-
const cursor = collection.find()
8789
const domain = translateDomain(collection.collectionName)
90+
if (include !== undefined && !include.has(domain)) {
91+
continue
92+
}
93+
const cursor = collection.find()
8894
const current = await pgClient`SELECT _id FROM ${pgClient(domain)} WHERE "workspaceId" = ${ws.workspace}`
8995
const currentIds = new Set(current.map((r) => r._id))
9096
console.log('move domain', domain)
@@ -131,7 +137,8 @@ export async function moveWorkspaceFromMongoToPG (
131137
mongoUrl: string,
132138
dbUrl: string | undefined,
133139
ws: Workspace,
134-
region: string
140+
region: string,
141+
include?: Set<string>
135142
): Promise<void> {
136143
if (dbUrl === undefined) {
137144
throw new Error('dbUrl is required')
@@ -141,7 +148,7 @@ export async function moveWorkspaceFromMongoToPG (
141148
const pg = getDBClient(dbUrl)
142149
const pgClient = await pg.getClient()
143150

144-
await moveWorkspace(accountDb, mongo, pgClient, ws, region)
151+
await moveWorkspace(accountDb, mongo, pgClient, ws, region, include)
145152
pg.close()
146153
client.close()
147154
}

dev/tool/src/index.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,21 +1666,39 @@ export function devTool (
16661666
})
16671667
})
16681668

1669-
program.command('move-workspace-to-pg <workspace> <region>').action(async (workspace: string, region: string) => {
1670-
const { dbUrl } = prepareTools()
1671-
const mongodbUri = getMongoDBUrl()
1669+
program
1670+
.command('move-workspace-to-pg <workspace> <region>')
1671+
.option('-i, --include <include>', 'A list of ; separated domain names to include during backup', '*')
1672+
.action(
1673+
async (
1674+
workspace: string,
1675+
region: string,
1676+
cmd: {
1677+
include: string
1678+
}
1679+
) => {
1680+
const { dbUrl } = prepareTools()
1681+
const mongodbUri = getMongoDBUrl()
16721682

1673-
await withDatabase(mongodbUri, async (db) => {
1674-
const workspaceInfo = await getWorkspaceById(db, workspace)
1675-
if (workspaceInfo === null) {
1676-
throw new Error(`workspace ${workspace} not found`)
1677-
}
1678-
if (workspaceInfo.region === region) {
1679-
throw new Error(`workspace ${workspace} is already migrated`)
1683+
await withDatabase(mongodbUri, async (db) => {
1684+
const workspaceInfo = await getWorkspaceById(db, workspace)
1685+
if (workspaceInfo === null) {
1686+
throw new Error(`workspace ${workspace} not found`)
1687+
}
1688+
if (workspaceInfo.region === region) {
1689+
throw new Error(`workspace ${workspace} is already migrated`)
1690+
}
1691+
await moveWorkspaceFromMongoToPG(
1692+
db,
1693+
mongodbUri,
1694+
dbUrl,
1695+
workspaceInfo,
1696+
region,
1697+
cmd.include === '*' ? undefined : new Set(cmd.include.split(';').map((it) => it.trim()))
1698+
)
1699+
})
16801700
}
1681-
await moveWorkspaceFromMongoToPG(db, mongodbUri, dbUrl, workspaceInfo, region)
1682-
})
1683-
})
1701+
)
16841702

16851703
program.command('move-account-db-to-pg').action(async () => {
16861704
const { dbUrl } = prepareTools()

0 commit comments

Comments
 (0)