-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(rethinkdb): PasswordResetRequest: One-shot (#10210)
Signed-off-by: Matt Krick <matt.krick@gmail.com>
- Loading branch information
Showing
7 changed files
with
115 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
import {sql} from 'kysely' | ||
import '../../../scripts/webpack/utils/dotenv' | ||
import getRethink from '../database/rethinkDriver' | ||
import getKysely from '../postgres/getKysely' | ||
|
||
async function setup() { | ||
const r = await getRethink() | ||
// The IP address is always localhost | ||
// so the safety checks will eventually fail if run too much | ||
|
||
await Promise.all([r.table('PasswordResetRequest').delete().run()]) | ||
await sql`TRUNCATE TABLE "PasswordResetRequest"`.execute(getKysely()) | ||
} | ||
|
||
export default setup |
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 was deleted.
Oops, something went wrong.
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
62 changes: 62 additions & 0 deletions
62
packages/server/postgres/migrations/1725996598345_PasswordResetRequest.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,62 @@ | ||
import {Kysely, PostgresDialect, sql} from 'kysely' | ||
import {Client} from 'pg' | ||
import {r} from 'rethinkdb-ts' | ||
import connectRethinkDB from '../../database/connectRethinkDB' | ||
import getPg from '../getPg' | ||
import getPgConfig from '../getPgConfig' | ||
|
||
export async function up() { | ||
await connectRethinkDB() | ||
const pg = new Kysely<any>({ | ||
dialect: new PostgresDialect({ | ||
pool: getPg() | ||
}) | ||
}) | ||
await sql` | ||
DO $$ | ||
BEGIN | ||
CREATE TABLE IF NOT EXISTS "PasswordResetRequest" ( | ||
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, | ||
"ip" cidr NOT NULL, | ||
"email" "citext" NOT NULL, | ||
"time" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), | ||
"token" VARCHAR(64) NOT NULL, | ||
"isValid" BOOLEAN NOT NULL DEFAULT TRUE | ||
); | ||
CREATE INDEX IF NOT EXISTS "idx_PasswordResetRequest_ip" ON "PasswordResetRequest"("ip"); | ||
CREATE INDEX IF NOT EXISTS "idx_PasswordResetRequest_email" ON "PasswordResetRequest"("email"); | ||
CREATE INDEX IF NOT EXISTS "idx_PasswordResetRequest_token" ON "PasswordResetRequest"("token"); | ||
END $$; | ||
`.execute(pg) | ||
|
||
const rRequests = await r.table('PasswordResetRequest').coerceTo('array').run() | ||
|
||
await Promise.all( | ||
rRequests.map(async (row) => { | ||
const {ip, email, time, token, isValid} = row | ||
try { | ||
return await pg | ||
.insertInto('PasswordResetRequest') | ||
.values({ | ||
ip, | ||
email, | ||
time, | ||
token, | ||
isValid | ||
}) | ||
.execute() | ||
} catch (e) { | ||
console.log(e, row) | ||
} | ||
}) | ||
) | ||
} | ||
|
||
export async function down() { | ||
const client = new Client(getPgConfig()) | ||
await client.connect() | ||
await client.query(` | ||
DROP TABLE IF EXISTS "PasswordResetRequest"; | ||
` /* Do undo magic */) | ||
await client.end() | ||
} |