From 5828fa7a147a57f92c56ebe08acc826728c1b9f5 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Thu, 10 Aug 2023 01:09:37 -0400 Subject: [PATCH] fix: use userId as primary key --- .../cql/1689400417034_timeline/up.cql | 12 +++++------ packages/backend/src/db/scylla.ts | 20 +++++++------------ packages/backend/src/services/note/create.ts | 4 ++-- packages/backend/src/services/note/delete.ts | 2 +- .../src/services/note/reaction/create.ts | 2 +- .../src/services/note/reaction/delete.ts | 2 +- 6 files changed, 18 insertions(+), 24 deletions(-) diff --git a/packages/backend/native-utils/scylla-migration/cql/1689400417034_timeline/up.cql b/packages/backend/native-utils/scylla-migration/cql/1689400417034_timeline/up.cql index 64fba60827..6c4cb8b854 100644 --- a/packages/backend/native-utils/scylla-migration/cql/1689400417034_timeline/up.cql +++ b/packages/backend/native-utils/scylla-migration/cql/1689400417034_timeline/up.cql @@ -69,7 +69,7 @@ CREATE TABLE IF NOT EXISTS note ( -- Models timeline "reactions" map, -- Reactions "noteEdit" set>, -- Edit History "updatedAt" timestamp, - PRIMARY KEY ("createdAtDate", "createdAt", "id") + PRIMARY KEY ("createdAtDate", "createdAt", "userId") ) WITH CLUSTERING ORDER BY ("createdAt" DESC); CREATE INDEX IF NOT EXISTS note_by_uri ON note ("uri"); @@ -80,7 +80,8 @@ CREATE MATERIALIZED VIEW IF NOT EXISTS note_by_id AS WHERE "id" IS NOT NULL AND "createdAt" IS NOT NULL AND "createdAtDate" IS NOT NULL - PRIMARY KEY ("id", "createdAt", "createdAtDate") + AND "userId" IS NOT NULL + PRIMARY KEY ("id", "createdAt", "createdAtDate", "userId") WITH CLUSTERING ORDER BY ("createdAt" DESC); CREATE MATERIALIZED VIEW IF NOT EXISTS note_by_user_id AS @@ -88,8 +89,7 @@ CREATE MATERIALIZED VIEW IF NOT EXISTS note_by_user_id AS WHERE "userId" IS NOT NULL AND "createdAt" IS NOT NULL AND "createdAtDate" IS NOT NULL - AND "id" IS NOT NULL - PRIMARY KEY ("userId", "createdAt", "createdAtDate", "id") + PRIMARY KEY ("userId", "createdAt", "createdAtDate") WITH CLUSTERING ORDER BY ("createdAt" DESC); CREATE MATERIALIZED VIEW IF NOT EXISTS note_by_renote_id AS @@ -97,8 +97,8 @@ CREATE MATERIALIZED VIEW IF NOT EXISTS note_by_renote_id AS WHERE "renoteId" IS NOT NULL AND "createdAt" IS NOT NULL AND "createdAtDate" IS NOT NULL - AND "id" IS NOT NULL - PRIMARY KEY ("renoteId", "createdAt", "createdAtDate", "id") + AND "userId" IS NOT NULL + PRIMARY KEY ("renoteId", "createdAt", "createdAtDate", "userId") WITH CLUSTERING ORDER BY ("createdAt" DESC); CREATE TABLE IF NOT EXISTS reaction ( diff --git a/packages/backend/src/db/scylla.ts b/packages/backend/src/db/scylla.ts index 3a22436eb1..41f667f1f2 100644 --- a/packages/backend/src/db/scylla.ts +++ b/packages/backend/src/db/scylla.ts @@ -115,20 +115,20 @@ export const prepared = { byUserId: `SELECT * FROM note_by_user_id WHERE "userId" IN ?`, byRenoteId: `SELECT * FROM note_by_renote_id WHERE "renoteId" = ?`, }, - delete: `DELETE FROM note WHERE "createdAtDate" = ? AND "createdAt" = ? AND "id" = ?`, + delete: `DELETE FROM note WHERE "createdAtDate" = ? AND "createdAt" = ? AND "userId" = ?`, update: { renoteCount: `UPDATE note SET "renoteCount" = ?, "score" = ? - WHERE "createdAtDate" = ? AND "createdAt" = ? AND "id" = ? IF EXISTS`, + WHERE "createdAtDate" = ? AND "createdAt" = ? AND "userId" = ? IF EXISTS`, repliesCount: `UPDATE note SET "repliesCount" = ? - WHERE "createdAtDate" = ? AND "createdAt" = ? AND "id" = ? IF EXISTS`, + WHERE "createdAtDate" = ? AND "createdAt" = ? AND "userId" = ? IF EXISTS`, reactions: `UPDATE note SET "emojis" = ?, "reactions" = ?, "score" = ? - WHERE "createdAtDate" = ? AND "createdAt" = ? AND "id" = ? IF EXISTS`, + WHERE "createdAtDate" = ? AND "createdAt" = ? AND "userId" = ? IF EXISTS`, }, }, reaction: { @@ -327,15 +327,9 @@ export async function execNotePaginationQuery( if (result.rowLength === 0) { // Reached the end of partition. Queries posts created one day before. scannedEmptyPartitions++; - untilDate = new Date( - untilDate.getFullYear(), - untilDate.getMonth(), - untilDate.getDate() - 1, - 23, - 59, - 59, - 999, - ); + const yesterday = new Date(untilDate.getTime() - 86400000); + yesterday.setUTCHours(23, 59, 59, 999); + untilDate = yesterday; if (sinceDate && untilDate < sinceDate) break; continue; diff --git a/packages/backend/src/services/note/create.ts b/packages/backend/src/services/note/create.ts index 5dddf83b86..2d2ea3a0ad 100644 --- a/packages/backend/src/services/note/create.ts +++ b/packages/backend/src/services/note/create.ts @@ -685,7 +685,7 @@ async function incRenoteCount(renote: Note) { const score = isNaN(renote.score) ? 0 : renote.score; await scyllaClient.execute( prepared.note.update.renoteCount, - [count + 1, score + 1, renote.createdAt, renote.createdAt, renote.id], + [count + 1, score + 1, renote.createdAt, renote.createdAt, renote.userId], { prepare: true }, ); } else { @@ -1002,7 +1002,7 @@ async function saveReply(reply: Note) { const count = isNaN(reply.repliesCount) ? 0 : reply.repliesCount; await scyllaClient.execute( prepared.note.update.repliesCount, - [count + 1, reply.createdAt, reply.createdAt, reply.id], + [count + 1, reply.createdAt, reply.createdAt, reply.userId], { prepare: true }, ); } else { diff --git a/packages/backend/src/services/note/delete.ts b/packages/backend/src/services/note/delete.ts index 9cc800bbef..4ddf6ec85c 100644 --- a/packages/backend/src/services/note/delete.ts +++ b/packages/backend/src/services/note/delete.ts @@ -119,7 +119,7 @@ export default async function ( if (scyllaClient) { const date = new Date(note.createdAt.getTime()); - await scyllaClient.execute(prepared.note.delete, [date, date, note.id], { + await scyllaClient.execute(prepared.note.delete, [date, date, note.userId], { prepare: true, }); } diff --git a/packages/backend/src/services/note/reaction/create.ts b/packages/backend/src/services/note/reaction/create.ts index 935f9361ff..8752fb5cec 100644 --- a/packages/backend/src/services/note/reaction/create.ts +++ b/packages/backend/src/services/note/reaction/create.ts @@ -116,7 +116,7 @@ export default async ( (note.score ?? 0) + 1, date, date, - note.id, + note.userId, ], { prepare: true }, ); diff --git a/packages/backend/src/services/note/reaction/delete.ts b/packages/backend/src/services/note/reaction/delete.ts index 8a04bd8ce2..1e78b14112 100644 --- a/packages/backend/src/services/note/reaction/delete.ts +++ b/packages/backend/src/services/note/reaction/delete.ts @@ -67,7 +67,7 @@ export default async ( Math.max((note.score ?? 0) - 1, 0), date, date, - note.id, + note.userId, ], { prepare: true }, );