-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: migration reflection groups to pg (#9514)
Signed-off-by: Matt Krick <matt.krick@gmail.com>
- Loading branch information
Showing
6 changed files
with
145 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
packages/server/postgres/migrations/1712075131388_retroReflectionGroups2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import {Kysely, PostgresDialect} from 'kysely' | ||
import {r} from 'rethinkdb-ts' | ||
import connectRethinkDB from '../../database/connectRethinkDB' | ||
import getPg from '../getPg' | ||
|
||
export async function up() { | ||
await connectRethinkDB() | ||
const pg = new Kysely<any>({ | ||
dialect: new PostgresDialect({ | ||
pool: getPg() | ||
}) | ||
}) | ||
try { | ||
await r | ||
.table('RetroReflectionGroup') | ||
.indexCreate('updatedAtId', (row: any) => [row('updatedAt'), row('id')]) | ||
.run() | ||
await r.table('RetroReflectionGroup').indexWait().run() | ||
} catch { | ||
// index already exists | ||
} | ||
|
||
const MAX_PG_PARAMS = 65545 | ||
const PG_COLS = [ | ||
'id', | ||
'createdAt', | ||
'updatedAt', | ||
'isActive', | ||
'meetingId', | ||
'promptId', | ||
'sortOrder', | ||
'voterIds', | ||
'smartTitle', | ||
'title', | ||
'summary', | ||
'discussionPromptQuestion' | ||
] as const | ||
type RetroReflectionGroup = { | ||
[K in (typeof PG_COLS)[number]]: any | ||
} | ||
const BATCH_SIZE = Math.trunc(MAX_PG_PARAMS / PG_COLS.length) | ||
|
||
let curUpdatedAt = r.minval | ||
let curId = r.minval | ||
for (let i = 0; i < 1e6; i++) { | ||
const rawRowsToInsert = (await r | ||
.table('RetroReflectionGroup') | ||
.between([curUpdatedAt, curId], [r.maxval, r.maxval], { | ||
index: 'updatedAtId', | ||
leftBound: 'open', | ||
rightBound: 'closed' | ||
}) | ||
.orderBy({index: 'updatedAtId'}) | ||
.limit(BATCH_SIZE) | ||
.pluck(...PG_COLS) | ||
.run()) as RetroReflectionGroup[] | ||
|
||
const rowsToInsert = rawRowsToInsert.map((row) => ({ | ||
...row, | ||
title: row.title?.slice(0, 255), | ||
smartTitle: row.smartTitle?.slice(0, 255), | ||
summary: row.summary?.slice(0, 2000) | ||
})) | ||
if (rowsToInsert.length === 0) break | ||
const lastRow = rowsToInsert[rowsToInsert.length - 1] | ||
curUpdatedAt = lastRow.updatedAt | ||
curId = lastRow.id | ||
try { | ||
await pg | ||
.insertInto('RetroReflectionGroup') | ||
.values(rowsToInsert) | ||
.onConflict((oc) => oc.doNothing()) | ||
.execute() | ||
} catch (e) { | ||
console.log({lastRow}, rowsToInsert.length) | ||
throw e | ||
} | ||
} | ||
} | ||
|
||
export async function down() { | ||
await connectRethinkDB() | ||
try { | ||
await r.table('RetroReflectionGroup').indexDrop('updatedAtId').run() | ||
} catch { | ||
// index already dropped | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters