-
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 FailedAuthRequest to pg
- Loading branch information
Showing
6 changed files
with
129 additions
and
28 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
52 changes: 32 additions & 20 deletions
52
packages/server/graphql/mutations/helpers/attemptLogin.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
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
28 changes: 28 additions & 0 deletions
28
packages/server/postgres/migrations/1709362249000_addFailedAuthRequest.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,28 @@ | ||
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 "FailedAuthRequest" ( | ||
"id" SERIAL PRIMARY KEY, | ||
"email" "citext" NOT NULL, | ||
"ip" "inet" NOT NULL, | ||
"time" TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL | ||
); | ||
CREATE INDEX IF NOT EXISTS "idx_FailedAuthRequest_email" ON "FailedAuthRequest"("email"); | ||
CREATE INDEX IF NOT EXISTS "idx_FailedAuthRequest_ip" ON "FailedAuthRequest"("ip"); | ||
`) | ||
await client.end() | ||
} | ||
|
||
export async function down() { | ||
const client = new Client(getPgConfig()) | ||
await client.connect() | ||
await client.query(` | ||
DROP TABLE IF EXISTS "FailedAuthRequest"; | ||
`) | ||
await client.end() | ||
} |
64 changes: 64 additions & 0 deletions
64
packages/server/postgres/migrations/1709362273000_moveFailedAuthRequest.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,64 @@ | ||
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('FailedAuthRequest').indexCreate('time').run() | ||
await r.table('FailedAuthRequest').indexWait().run() | ||
} catch {} | ||
|
||
const columnSet = new pgp.helpers.ColumnSet(['email', 'ip', 'time'], {table: 'FailedAuthRequest'}) | ||
|
||
const getNextData = async (leftBoundCursor: Date | undefined) => { | ||
const startAt = leftBoundCursor || r.minval | ||
const nextBatch = await r | ||
.table('FailedAuthRequest') | ||
.between(startAt, r.maxval, {index: 'time', leftBound: 'open'}) | ||
.orderBy({index: 'time'}) | ||
.limit(batchSize) | ||
.run() | ||
if (nextBatch.length === 0) return null | ||
if (nextBatch.length < batchSize) return nextBatch | ||
const lastItem = nextBatch.pop() | ||
const lastMatchingTime = nextBatch.findLastIndex((item) => item.time !== lastItem!.time) | ||
if (lastMatchingTime === -1) { | ||
throw new Error( | ||
'batchSize is smaller than the number of items that share the same cursor. Increase batchSize' | ||
) | ||
} | ||
return nextBatch.slice(0, lastMatchingTime) | ||
} | ||
|
||
await pg.tx('FailedAuthRequest', (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 "FailedAuthRequest"`) | ||
await client.end() | ||
try { | ||
await r.table('FailedAuthRequest').indexDrop('time').run() | ||
} catch {} | ||
} |