-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: migrate EmailVerification to pg
- Loading branch information
Showing
8 changed files
with
143 additions
and
39 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
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
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
31 changes: 31 additions & 0 deletions
31
packages/server/postgres/migrations/1709351538000_addEmailVerification.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,31 @@ | ||
import {Client} from 'pg' | ||
import getPgConfig from '../getPgConfig' | ||
|
||
export async function up() { | ||
const client = new Client(getPgConfig()) | ||
await client.connect() | ||
await client.query(` | ||
CREATE TABLE "EmailVerification" ( | ||
"id" SERIAL PRIMARY KEY, | ||
"email" "citext" NOT NULL, | ||
"expiration" TIMESTAMP WITH TIME ZONE NOT NULL, | ||
"token" VARCHAR(100) NOT NULL, | ||
"hashedPassword" VARCHAR(100), | ||
"invitationToken" VARCHAR(100), | ||
"pseudoId" VARCHAR(100) | ||
); | ||
CREATE INDEX IF NOT EXISTS "idx_EmailVerification_email" ON "EmailVerification"("email"); | ||
CREATE INDEX IF NOT EXISTS "idx_EmailVerification_token" ON "EmailVerification"("token"); | ||
`) | ||
await client.end() | ||
} | ||
|
||
export async function down() { | ||
const client = new Client(getPgConfig()) | ||
await client.connect() | ||
await client.query(` | ||
DROP TABLE IF EXISTS "EmailVerification"; | ||
`) | ||
await client.end() | ||
} |
76 changes: 76 additions & 0 deletions
76
packages/server/postgres/migrations/1709351575000_moveEmailVerification.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,76 @@ | ||
import {FirstParam} from 'parabol-client/types/generics' | ||
import {Client} from 'pg' | ||
import {r} from 'rethinkdb-ts' | ||
import getPgConfig from '../getPgConfig' | ||
import connectRethinkDB from '../../database/connectRethinkDB' | ||
import getPgp from '../getPgp' | ||
|
||
export async function up() { | ||
await connectRethinkDB() | ||
const {pgp, pg} = getPgp() | ||
const batchSize = 1000 | ||
|
||
try { | ||
await r.table('EmailVerification').indexCreate('expiration').run() | ||
await r.table('EmailVerification').indexWait().run() | ||
} catch {} | ||
|
||
const columnSet = new pgp.helpers.ColumnSet( | ||
[ | ||
'email', | ||
'expiration', | ||
'hashedPassword', | ||
'token', | ||
{name: 'invitationToken', def: null}, | ||
{name: 'pseudoId', def: null} | ||
], | ||
{table: 'EmailVerification'} | ||
) | ||
|
||
const getNextData = async (leftBoundCursor: Date | undefined) => { | ||
const expiration = leftBoundCursor || r.minval | ||
const nextBatch = await r | ||
.table('EmailVerification') | ||
.between(expiration, r.maxval, {index: 'expiration', leftBound: 'open'}) | ||
.orderBy({index: 'expiration'}) | ||
.limit(batchSize) | ||
.run() | ||
if (nextBatch.length === 0) return null | ||
if (nextBatch.length < batchSize) return nextBatch | ||
const lastItem = nextBatch.pop() | ||
const lastMatchingExpiration = nextBatch.findLastIndex( | ||
(item) => item.expiration !== lastItem!.expiration | ||
) | ||
if (lastMatchingExpiration === -1) { | ||
throw new Error( | ||
'batchSize is smaller than the number of items that share the same cursor. Increase batchSize' | ||
) | ||
} | ||
return nextBatch.slice(0, lastMatchingExpiration + 1) | ||
} | ||
|
||
await pg.tx('EmailVerification', (task) => { | ||
const fetchAndProcess: FirstParam<typeof task.sequence> = async ( | ||
_index, | ||
leftBoundCursor: undefined | Date | ||
) => { | ||
const nextData = await getNextData(leftBoundCursor) | ||
if (!nextData) return undefined | ||
const insert = pgp.helpers.insert(nextData, columnSet) | ||
await task.none(insert) | ||
return nextData.at(-1)!.runAt | ||
} | ||
return task.sequence(fetchAndProcess) | ||
}) | ||
await r.getPoolMaster()?.drain() | ||
} | ||
|
||
export async function down() { | ||
const client = new Client(getPgConfig()) | ||
await client.connect() | ||
await client.query(`DELETE FROM "EmailVerification"`) | ||
await client.end() | ||
try { | ||
await r.table('EmailVerification').indexDrop('expiration').run() | ||
} catch {} | ||
} |