fix: use userId as primary key
This commit is contained in:
parent
bfc96e3c8a
commit
5828fa7a14
6 changed files with 18 additions and 24 deletions
|
@ -69,7 +69,7 @@ CREATE TABLE IF NOT EXISTS note ( -- Models timeline
|
||||||
"reactions" map<text, int>, -- Reactions
|
"reactions" map<text, int>, -- Reactions
|
||||||
"noteEdit" set<frozen<note_edit_history>>, -- Edit History
|
"noteEdit" set<frozen<note_edit_history>>, -- Edit History
|
||||||
"updatedAt" timestamp,
|
"updatedAt" timestamp,
|
||||||
PRIMARY KEY ("createdAtDate", "createdAt", "id")
|
PRIMARY KEY ("createdAtDate", "createdAt", "userId")
|
||||||
) WITH CLUSTERING ORDER BY ("createdAt" DESC);
|
) WITH CLUSTERING ORDER BY ("createdAt" DESC);
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS note_by_uri ON note ("uri");
|
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
|
WHERE "id" IS NOT NULL
|
||||||
AND "createdAt" IS NOT NULL
|
AND "createdAt" IS NOT NULL
|
||||||
AND "createdAtDate" 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);
|
WITH CLUSTERING ORDER BY ("createdAt" DESC);
|
||||||
|
|
||||||
CREATE MATERIALIZED VIEW IF NOT EXISTS note_by_user_id AS
|
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
|
WHERE "userId" IS NOT NULL
|
||||||
AND "createdAt" IS NOT NULL
|
AND "createdAt" IS NOT NULL
|
||||||
AND "createdAtDate" IS NOT NULL
|
AND "createdAtDate" IS NOT NULL
|
||||||
AND "id" IS NOT NULL
|
PRIMARY KEY ("userId", "createdAt", "createdAtDate")
|
||||||
PRIMARY KEY ("userId", "createdAt", "createdAtDate", "id")
|
|
||||||
WITH CLUSTERING ORDER BY ("createdAt" DESC);
|
WITH CLUSTERING ORDER BY ("createdAt" DESC);
|
||||||
|
|
||||||
CREATE MATERIALIZED VIEW IF NOT EXISTS note_by_renote_id AS
|
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
|
WHERE "renoteId" IS NOT NULL
|
||||||
AND "createdAt" IS NOT NULL
|
AND "createdAt" IS NOT NULL
|
||||||
AND "createdAtDate" IS NOT NULL
|
AND "createdAtDate" IS NOT NULL
|
||||||
AND "id" IS NOT NULL
|
AND "userId" IS NOT NULL
|
||||||
PRIMARY KEY ("renoteId", "createdAt", "createdAtDate", "id")
|
PRIMARY KEY ("renoteId", "createdAt", "createdAtDate", "userId")
|
||||||
WITH CLUSTERING ORDER BY ("createdAt" DESC);
|
WITH CLUSTERING ORDER BY ("createdAt" DESC);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS reaction (
|
CREATE TABLE IF NOT EXISTS reaction (
|
||||||
|
|
|
@ -115,20 +115,20 @@ export const prepared = {
|
||||||
byUserId: `SELECT * FROM note_by_user_id WHERE "userId" IN ?`,
|
byUserId: `SELECT * FROM note_by_user_id WHERE "userId" IN ?`,
|
||||||
byRenoteId: `SELECT * FROM note_by_renote_id WHERE "renoteId" = ?`,
|
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: {
|
update: {
|
||||||
renoteCount: `UPDATE note SET
|
renoteCount: `UPDATE note SET
|
||||||
"renoteCount" = ?,
|
"renoteCount" = ?,
|
||||||
"score" = ?
|
"score" = ?
|
||||||
WHERE "createdAtDate" = ? AND "createdAt" = ? AND "id" = ? IF EXISTS`,
|
WHERE "createdAtDate" = ? AND "createdAt" = ? AND "userId" = ? IF EXISTS`,
|
||||||
repliesCount: `UPDATE note SET
|
repliesCount: `UPDATE note SET
|
||||||
"repliesCount" = ?
|
"repliesCount" = ?
|
||||||
WHERE "createdAtDate" = ? AND "createdAt" = ? AND "id" = ? IF EXISTS`,
|
WHERE "createdAtDate" = ? AND "createdAt" = ? AND "userId" = ? IF EXISTS`,
|
||||||
reactions: `UPDATE note SET
|
reactions: `UPDATE note SET
|
||||||
"emojis" = ?,
|
"emojis" = ?,
|
||||||
"reactions" = ?,
|
"reactions" = ?,
|
||||||
"score" = ?
|
"score" = ?
|
||||||
WHERE "createdAtDate" = ? AND "createdAt" = ? AND "id" = ? IF EXISTS`,
|
WHERE "createdAtDate" = ? AND "createdAt" = ? AND "userId" = ? IF EXISTS`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
reaction: {
|
reaction: {
|
||||||
|
@ -327,15 +327,9 @@ export async function execNotePaginationQuery(
|
||||||
if (result.rowLength === 0) {
|
if (result.rowLength === 0) {
|
||||||
// Reached the end of partition. Queries posts created one day before.
|
// Reached the end of partition. Queries posts created one day before.
|
||||||
scannedEmptyPartitions++;
|
scannedEmptyPartitions++;
|
||||||
untilDate = new Date(
|
const yesterday = new Date(untilDate.getTime() - 86400000);
|
||||||
untilDate.getFullYear(),
|
yesterday.setUTCHours(23, 59, 59, 999);
|
||||||
untilDate.getMonth(),
|
untilDate = yesterday;
|
||||||
untilDate.getDate() - 1,
|
|
||||||
23,
|
|
||||||
59,
|
|
||||||
59,
|
|
||||||
999,
|
|
||||||
);
|
|
||||||
if (sinceDate && untilDate < sinceDate) break;
|
if (sinceDate && untilDate < sinceDate) break;
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -685,7 +685,7 @@ async function incRenoteCount(renote: Note) {
|
||||||
const score = isNaN(renote.score) ? 0 : renote.score;
|
const score = isNaN(renote.score) ? 0 : renote.score;
|
||||||
await scyllaClient.execute(
|
await scyllaClient.execute(
|
||||||
prepared.note.update.renoteCount,
|
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 },
|
{ prepare: true },
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -1002,7 +1002,7 @@ async function saveReply(reply: Note) {
|
||||||
const count = isNaN(reply.repliesCount) ? 0 : reply.repliesCount;
|
const count = isNaN(reply.repliesCount) ? 0 : reply.repliesCount;
|
||||||
await scyllaClient.execute(
|
await scyllaClient.execute(
|
||||||
prepared.note.update.repliesCount,
|
prepared.note.update.repliesCount,
|
||||||
[count + 1, reply.createdAt, reply.createdAt, reply.id],
|
[count + 1, reply.createdAt, reply.createdAt, reply.userId],
|
||||||
{ prepare: true },
|
{ prepare: true },
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -119,7 +119,7 @@ export default async function (
|
||||||
|
|
||||||
if (scyllaClient) {
|
if (scyllaClient) {
|
||||||
const date = new Date(note.createdAt.getTime());
|
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,
|
prepare: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,7 +116,7 @@ export default async (
|
||||||
(note.score ?? 0) + 1,
|
(note.score ?? 0) + 1,
|
||||||
date,
|
date,
|
||||||
date,
|
date,
|
||||||
note.id,
|
note.userId,
|
||||||
],
|
],
|
||||||
{ prepare: true },
|
{ prepare: true },
|
||||||
);
|
);
|
||||||
|
|
|
@ -67,7 +67,7 @@ export default async (
|
||||||
Math.max((note.score ?? 0) - 1, 0),
|
Math.max((note.score ?? 0) - 1, 0),
|
||||||
date,
|
date,
|
||||||
date,
|
date,
|
||||||
note.id,
|
note.userId,
|
||||||
],
|
],
|
||||||
{ prepare: true },
|
{ prepare: true },
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue