Skip to content

Commit d2e0868

Browse files
author
Theodore Li
committed
Fix row count bug
1 parent 353ef31 commit d2e0868

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

packages/db/scripts/migrate-block-api-keys-to-byok.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
// # Live run: insert BYOK keys
1313
// bun run packages/db/scripts/migrate-block-api-keys-to-byok.ts \
1414
// --map jina=jina --map perplexity=perplexity --map google_books=google_cloud
15+
//
16+
// # Optionally scope to specific users (repeatable)
17+
// bun run packages/db/scripts/migrate-block-api-keys-to-byok.ts --dry-run \
18+
// --map jina=jina --user user_abc123 --user user_def456
1519

1620
import { createCipheriv, createDecipheriv, randomBytes } from 'crypto'
1721
import { eq, sql } from 'drizzle-orm'
@@ -50,6 +54,20 @@ if (Object.keys(BLOCK_TYPE_TO_PROVIDER).length === 0) {
5054
process.exit(1)
5155
}
5256

57+
function parseUserArgs(): string[] {
58+
const users: string[] = []
59+
const args = process.argv.slice(2)
60+
for (let i = 0; i < args.length; i++) {
61+
if (args[i] === '--user' && args[i + 1]) {
62+
users.push(args[i + 1])
63+
i++
64+
}
65+
}
66+
return users
67+
}
68+
69+
const USER_FILTER = parseUserArgs()
70+
5371
// ---------- Env ----------
5472
function getEnv(name: string): string | undefined {
5573
if (typeof process !== 'undefined' && process.env && name in process.env) {
@@ -250,6 +268,7 @@ async function run() {
250268
console.log(
251269
`Mappings: ${Object.entries(BLOCK_TYPE_TO_PROVIDER).map(([b, p]) => `${b}=${p}`).join(', ')}`
252270
)
271+
console.log(`Users: ${USER_FILTER.length > 0 ? USER_FILTER.join(', ') : 'all'}`)
253272
console.log('---\n')
254273

255274
const stats = {
@@ -268,6 +287,14 @@ async function run() {
268287
const agentTypes = Object.keys(TOOL_INPUT_SUBBLOCK_IDS)
269288
const allBlockTypes = [...new Set([...mappedBlockTypes, ...agentTypes])]
270289

290+
const userFilter =
291+
USER_FILTER.length > 0
292+
? sql` AND ${workflow.userId} IN (${sql.join(
293+
USER_FILTER.map((id) => sql`${id}`),
294+
sql`, `
295+
)})`
296+
: sql``
297+
271298
const workspaceIdRows = await db
272299
.selectDistinct({ workspaceId: workflow.workspaceId })
273300
.from(workflowBlocks)
@@ -276,7 +303,7 @@ async function run() {
276303
sql`${workflow.workspaceId} IS NOT NULL AND ${workflowBlocks.type} IN (${sql.join(
277304
allBlockTypes.map((t) => sql`${t}`),
278305
sql`, `
279-
)})`
306+
)})${userFilter}`
280307
)
281308

282309
const workspaceIds = workspaceIdRows
@@ -303,7 +330,7 @@ async function run() {
303330
sql`${workflow.workspaceId} = ${workspaceId} AND ${workflowBlocks.type} IN (${sql.join(
304331
allBlockTypes.map((t) => sql`${t}`),
305332
sql`, `
306-
)})`
333+
)})${userFilter}`
307334
)
308335

309336
console.log(`[Workspace ${workspaceId}] ${blocks.length} blocks`)
@@ -447,8 +474,9 @@ async function run() {
447474
.onConflictDoNothing({
448475
target: [workspaceBYOKKeys.workspaceId, workspaceBYOKKeys.providerId],
449476
})
477+
.returning({ id: workspaceBYOKKeys.id })
450478

451-
if ((result as any).rowCount === 0) {
479+
if (result.length === 0) {
452480
console.log(` [SKIP] BYOK already exists for provider "${providerId}"`)
453481
stats.skippedExisting++
454482
} else {

0 commit comments

Comments
 (0)