Skip to content

Commit e537aa5

Browse files
improvement(tables): make repair-script dry-run lock-free and clearly labeled
1 parent 075f3fe commit e537aa5

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

apps/sim/scripts/repair-table-order-key-collation.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ export async function runRepair(): Promise<void> {
9393
for (const { table_id: tableId } of pending) {
9494
stats.tables += 1
9595
try {
96+
if (dryRun) {
97+
// Sizing only — count outside any transaction/lock so we never serialize
98+
// live inserts on the table (taking the advisory lock just to count would
99+
// make the dry run the opposite of safe).
100+
const [row] = await db
101+
.select({ rowCount: sql<number>`count(*)`.mapWith(Number) })
102+
.from(userTableRows)
103+
.where(eq(userTableRows.tableId, tableId))
104+
stats.tablesKeyed += 1
105+
stats.rowsKeyed += row.rowCount
106+
console.log(` ${tableId}: would re-key ${row.rowCount} rows`)
107+
continue
108+
}
109+
96110
const keyed = await db.transaction(async (trx) => {
97111
// Serialize with concurrent inserts on this table (same lock the app uses).
98112
await trx.execute(
@@ -106,7 +120,6 @@ export async function runRepair(): Promise<void> {
106120

107121
if (rows.length === 0) return 0
108122
const keys = nKeysBetween(null, null, rows.length)
109-
if (dryRun) return rows.length
110123

111124
// Chunked UPDATE … FROM (VALUES …) mapping id → key (see WRITE_CHUNK_SIZE).
112125
for (let start = 0; start < rows.length; start += WRITE_CHUNK_SIZE) {
@@ -133,11 +146,12 @@ export async function runRepair(): Promise<void> {
133146
}
134147
}
135148

136-
console.log('Repair complete.')
137-
console.log(` tables scanned: ${stats.tables}`)
138-
console.log(` tables re-keyed: ${stats.tablesKeyed}`)
139-
console.log(` rows re-keyed: ${stats.rowsKeyed}`)
140-
console.log(` failed: ${stats.failed}`)
149+
const verb = dryRun ? 'to re-key' : 're-keyed'
150+
console.log(`Repair complete.${dryRun ? ' [DRY RUN — no rows written]' : ''}`)
151+
console.log(` tables scanned: ${stats.tables}`)
152+
console.log(` tables ${verb}: ${stats.tablesKeyed}`)
153+
console.log(` rows ${verb}: ${stats.rowsKeyed}`)
154+
console.log(` failed: ${stats.failed}`)
141155
if (stats.failed > 0) process.exitCode = 1
142156
} finally {
143157
await client.end({ timeout: 5 }).catch(() => {})

0 commit comments

Comments
 (0)