-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(rethinkdb): Organization: Phase 2 (#9931)
Signed-off-by: Matt Krick <matt.krick@gmail.com>
- Loading branch information
Showing
3 changed files
with
176 additions
and
26 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
121 changes: 121 additions & 0 deletions
121
packages/server/postgres/migrations/1720026588542_Organization-phase2.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,121 @@ | ||
import {Kysely, PostgresDialect, sql} from 'kysely' | ||
import {r} from 'rethinkdb-ts' | ||
import connectRethinkDB from '../../database/connectRethinkDB' | ||
import getPg from '../getPg' | ||
|
||
const toCreditCard = (creditCard: any) => { | ||
if (!creditCard) return null | ||
return sql<string>`(select json_populate_record(null::"CreditCard", ${JSON.stringify(creditCard)}))` | ||
} | ||
|
||
export async function up() { | ||
await connectRethinkDB() | ||
const pg = new Kysely<any>({ | ||
dialect: new PostgresDialect({ | ||
pool: getPg() | ||
}) | ||
}) | ||
try { | ||
console.log('Adding index') | ||
await r | ||
.table('Organization') | ||
.indexCreate('updatedAtId', (row: any) => [row('updatedAt'), row('id')]) | ||
.run() | ||
await r.table('Organization').indexWait().run() | ||
} catch { | ||
// index already exists | ||
} | ||
console.log('Adding index complete') | ||
const MAX_PG_PARAMS = 65545 | ||
const PG_COLS = [ | ||
'id', | ||
'activeDomain', | ||
'isActiveDomainTouched', | ||
'creditCard', | ||
'createdAt', | ||
'name', | ||
'payLaterClickCount', | ||
'periodEnd', | ||
'periodStart', | ||
'picture', | ||
'showConversionModal', | ||
'stripeId', | ||
'stripeSubscriptionId', | ||
'upcomingInvoiceEmailSentAt', | ||
'tier', | ||
'tierLimitExceededAt', | ||
'trialStartDate', | ||
'scheduledLockAt', | ||
'lockedAt', | ||
'updatedAt', | ||
'featureFlags' | ||
] as const | ||
type Organization = { | ||
[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++) { | ||
console.log('inserting row', i * BATCH_SIZE, curUpdatedAt, curId) | ||
const rawRowsToInsert = (await r | ||
.table('Organization') | ||
.between([curUpdatedAt, curId], [r.maxval, r.maxval], { | ||
index: 'updatedAtId', | ||
leftBound: 'open', | ||
rightBound: 'closed' | ||
}) | ||
.orderBy({index: 'updatedAtId'}) | ||
.limit(BATCH_SIZE) | ||
.pluck(...PG_COLS) | ||
.run()) as Organization[] | ||
|
||
const rowsToInsert = rawRowsToInsert.map((row) => ({ | ||
...row, | ||
activeDomain: row.activeDomain?.slice(0, 100) ?? null, | ||
name: row.name.slice(0, 100), | ||
creditCard: toCreditCard(row.creditCard) | ||
})) | ||
if (rowsToInsert.length === 0) break | ||
const lastRow = rowsToInsert[rowsToInsert.length - 1] | ||
curUpdatedAt = lastRow.updatedAt | ||
curId = lastRow.id | ||
try { | ||
await pg | ||
.insertInto('Organization') | ||
.values(rowsToInsert) | ||
.onConflict((oc) => oc.doNothing()) | ||
.execute() | ||
} catch (e) { | ||
await Promise.all( | ||
rowsToInsert.map(async (row) => { | ||
try { | ||
await pg | ||
.insertInto('Organization') | ||
.values(row) | ||
.onConflict((oc) => oc.doNothing()) | ||
.execute() | ||
} catch (e) { | ||
console.log(e, row) | ||
} | ||
}) | ||
) | ||
} | ||
} | ||
} | ||
|
||
export async function down() { | ||
await connectRethinkDB() | ||
try { | ||
await r.table('Organization').indexDrop('updatedAtId').run() | ||
} catch { | ||
// index already dropped | ||
} | ||
const pg = new Kysely<any>({ | ||
dialect: new PostgresDialect({ | ||
pool: getPg() | ||
}) | ||
}) | ||
await pg.deleteFrom('Organization').execute() | ||
} |
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