Skip to content

Commit 1e6978a

Browse files
author
Theodore Li
committed
Add skipping if byok already provided
1 parent 9102bcd commit 1e6978a

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,19 @@ async function processWorkspace(
499499

500500
stats.workspacesProcessed++
501501

502+
const existingBYOKProviders = new Set<string>()
503+
if (DRY_RUN) {
504+
const existingRows = await db
505+
.select({ providerId: workspaceBYOKKeys.providerId })
506+
.from(workspaceBYOKKeys)
507+
.where(eq(workspaceBYOKKeys.workspaceId, workspaceId))
508+
for (const row of existingRows) {
509+
existingBYOKProviders.add(row.providerId)
510+
}
511+
}
512+
513+
let hasNewInserts = false
514+
502515
for (const [providerId, refs] of providerKeys) {
503516
const resolved: { ref: RawKeyRef; key: string; source: KeySource }[] = []
504517
const resolveCtx: ResolveKeyContext = { workspaceId, workspaceOwnerId }
@@ -543,6 +556,14 @@ async function processWorkspace(
543556
const chosen = resolved[0]
544557

545558
if (DRY_RUN) {
559+
if (existingBYOKProviders.has(providerId)) {
560+
console.log(
561+
` [DRY RUN] BYOK already exists for provider "${providerId}", skipping`
562+
)
563+
stats.skippedExisting++
564+
continue
565+
}
566+
hasNewInserts = true
546567
console.log(
547568
` [DRY RUN] Would insert BYOK for provider "${providerId}": ${maskKey(chosen.key)}`
548569
)
@@ -579,7 +600,7 @@ async function processWorkspace(
579600
}
580601

581602
console.log(` [${index}/${total}] Done with workspace ${workspaceId}\n`)
582-
return { stats, shouldWriteWorkspaceId: DRY_RUN }
603+
return { stats, shouldWriteWorkspaceId: DRY_RUN && hasNewInserts }
583604
} catch (error) {
584605
console.error(` [ERROR] Failed workspace ${workspaceId}:`, error)
585606
stats.errors++
@@ -600,6 +621,7 @@ async function run() {
600621
console.log('---\n')
601622

602623
const stats = createEmptyStats()
624+
let workspaceIdsWritten = 0
603625

604626
try {
605627
// 1. Get distinct workspace IDs that have matching blocks
@@ -687,6 +709,7 @@ async function run() {
687709
resolve('migrate-byok-workspace-ids.txt'),
688710
`${workspaceIdsWithKeys.join('\n')}\n`
689711
)
712+
workspaceIdsWritten += workspaceIdsWithKeys.length
690713
}
691714
}
692715

@@ -716,7 +739,7 @@ async function run() {
716739

717740
if (DRY_RUN) {
718741
console.log(
719-
`\n[DRY RUN] Wrote ${stats.workspacesProcessed} workspace IDs (with keys) to migrate-byok-workspace-ids.txt`
742+
`\n[DRY RUN] Wrote ${workspaceIdsWritten} workspace IDs (with new keys to insert) to migrate-byok-workspace-ids.txt`
720743
)
721744
console.log('[DRY RUN] No changes were made to the database.')
722745
console.log('Run without --dry-run to apply changes.')

0 commit comments

Comments
 (0)