Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit 28f260f

Browse files
authored
chore: update raw queries to use explicit schemas for OpenGB 0.2 (#115)
1 parent 01d9791 commit 28f260f

File tree

6 files changed

+1592
-32
lines changed

6 files changed

+1592
-32
lines changed

modules/friends/scripts/accept_request.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ export async function run(
2525
acceptedAt: Date | null;
2626
declinedAt: Date | null;
2727
}
28-
const friendRequests = await tx.$queryRaw<FriendRequestRow[]>`
28+
const friendRequests = await tx.$queryRawUnsafe<FriendRequestRow[]>(
29+
`
2930
SELECT "senderUserId", "targetUserId", "acceptedAt", "declinedAt"
30-
FROM "FriendRequest"
31-
WHERE "id" = ${req.friendRequestId}
31+
FROM "${ctx.dbSchema}"."FriendRequest"
32+
WHERE "id" = $1
3233
FOR UPDATE
33-
`;
34+
`,
35+
req.friendRequestId
36+
);
3437
const friendRequest = friendRequests[0];
3538
if (!friendRequest) {
3639
throw new RuntimeError("FRIEND_REQUEST_NOT_FOUND", {

modules/friends/scripts/decline_request.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ export async function run(
2828
acceptedAt: Date | null;
2929
declinedAt: Date | null;
3030
}
31-
const friendRequests = await tx.$queryRaw<FriendRequestRow[]>`
32-
SELECT "senderUserId", "targetUserId", "acceptedAt", "declinedAt"
33-
FROM "FriendRequest"
34-
WHERE "id" = ${req.friendRequestId}
35-
FOR UPDATE
36-
`;
31+
const friendRequests = await tx.$queryRawUnsafe<FriendRequestRow[]>(
32+
`
33+
SELECT "senderUserId", "targetUserId", "acceptedAt", "declinedAt"
34+
FROM "${ctx.dbSchema}"."FriendRequest"
35+
WHERE "id" = $1
36+
FOR UPDATE
37+
`,
38+
req.friendRequestId
39+
);
3740
const friendRequest = friendRequests[0];
3841
if (!friendRequest) {
3942
throw new RuntimeError("FRIEND_REQUEST_NOT_FOUND", {

modules/friends/scripts/send_request.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@ export async function run(
3030
const row = await ctx.db.$transaction(async (tx) => {
3131
// Validate that the users are not already friends
3232
// TODO: Remove this `any` and replace with a proper type
33-
const existingFriendRows = await tx.$queryRaw<any[]>`
33+
const existingFriendRows = await tx.$queryRawUnsafe<any[]>(
34+
`
3435
SELECT 1
35-
FROM "Friend"
36-
WHERE "userIdA" = ${userIdA} OR "userIdB" = ${userIdA}
36+
FROM "${ctx.dbSchema}"."Friend"
37+
WHERE "userIdA" = $1 OR "userIdB" = $2
3738
FOR UPDATE
38-
`;
39+
`,
40+
userIdA,
41+
userIdB,
42+
);
3943
if (existingFriendRows.length > 0) {
4044
throw new RuntimeError("ALREADY_FRIENDS", { meta: { userIdA, userIdB } });
4145
}

modules/rate_limit/scripts/throttle.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,40 @@ export async function run(
3838
// `TokenBucket` is an unlogged table which are significantly faster to
3939
// write to than regular tables, but are not durable. This is important
4040
// because this script will be called on every request.
41-
const rows = await ctx.db.$queryRaw<TokenBucket[]>`
42-
WITH updated_bucket AS (
43-
UPDATE "TokenBuckets" b
41+
const rows = await ctx.db.$queryRawUnsafe<TokenBucket[]>(
42+
`
43+
WITH
44+
"UpdateBucket" AS (
45+
UPDATE "${ctx.dbSchema}"."TokenBuckets" b
4446
SET
4547
"tokens" = CASE
4648
-- Reset the bucket and consume 1 token
47-
WHEN now() > b."lastRefill" + make_interval(secs => ${req.period}) THEN ${
48-
req.requests - 1
49-
}
49+
WHEN now() > b."lastRefill" + make_interval(secs => $4) THEN $3 - 1
5050
-- Consume 1 token
5151
ELSE b.tokens - 1
5252
END,
5353
"lastRefill" = CASE
54-
WHEN now() > b."lastRefill" + make_interval(secs => ${req.period}) THEN now()
54+
WHEN now() > b."lastRefill" + make_interval(secs => $4) THEN now()
5555
ELSE b."lastRefill"
5656
END
57-
WHERE b."type" = ${req.type} AND b."key" = ${req.key}
57+
WHERE b."type" = $1 AND b."key" = $2
5858
RETURNING b."tokens", b."lastRefill"
5959
),
6060
inserted AS (
61-
INSERT INTO "TokenBuckets" ("type", "key", "tokens", "lastRefill")
62-
SELECT ${req.type}, ${req.key}, ${req.requests - 1}, now()
63-
WHERE NOT EXISTS (SELECT 1 FROM updated_bucket)
61+
INSERT INTO "${ctx.dbSchema}"."TokenBuckets" ("type", "key", "tokens", "lastRefill")
62+
SELECT $1, $2, $3 - 1, now()
63+
WHERE NOT EXISTS (SELECT 1 FROM "UpdateBucket")
6464
RETURNING "tokens", "lastRefill"
6565
)
66-
SELECT * FROM updated_bucket
66+
SELECT * FROM "UpdateBucket"
6767
UNION ALL
6868
SELECT * FROM inserted;
69-
`;
69+
`,
70+
req.type,
71+
req.key,
72+
req.requests,
73+
req.period,
74+
);
7075
const { tokens, lastRefill } = rows[0];
7176

7277
// If the bucket is empty, throw an error

modules/tokens/scripts/revoke.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,21 @@ export async function run(
2525

2626
// Sets revokedAt on all tokens that have not already been revoked. Returns
2727
// wether or not each token was revoked.
28-
const rows = await ctx.db.$queryRaw<TokenRow[]>`
28+
const rows = await ctx.db.$queryRawUnsafe<TokenRow[]>(
29+
`
2930
WITH "PreUpdate" AS (
3031
SELECT "id", "revokedAt"
31-
FROM "Token"
32-
WHERE "id" IN (${Prisma.join(req.tokenIds)})
32+
FROM "${ctx.dbSchema}"."Token"
33+
WHERE "id" = ANY($1)
3334
)
34-
UPDATE "Token"
35+
UPDATE "${ctx.dbSchema}"."Token"
3536
SET "revokedAt" = COALESCE("Token"."revokedAt", current_timestamp)
3637
FROM "PreUpdate"
3738
WHERE "Token"."id" = "PreUpdate"."id"
3839
RETURNING "Token"."id" AS "id", "PreUpdate"."revokedAt" IS NOT NULL AS "alreadyRevoked"
39-
`;
40+
`,
41+
req.tokenIds,
42+
);
4043

4144
const updates: Record<string, TokenUpdate> = {};
4245
for (const tokenId of req.tokenIds) {

0 commit comments

Comments
 (0)