hippofish/packages/backend/native-utils/scylla-migration/cql/1689400417034_timeline/up.cql

258 lines
7 KiB
Cassandra CQL
Raw Normal View History

2023-08-19 11:01:25 +02:00
CREATE TYPE drive_file (
2023-07-24 11:35:44 +02:00
"id" ascii,
"type" ascii,
"createdAt" timestamp,
"name" text,
"comment" text,
"blurhash" text,
"url" text,
"thumbnailUrl" text,
"isSensitive" boolean,
"isLink" boolean,
"md5" ascii,
2023-07-31 03:41:45 +02:00
"size" int,
"width" int,
"height" int,
2023-07-19 16:35:47 +02:00
);
2023-08-19 11:01:25 +02:00
CREATE TYPE note_edit_history (
2023-07-24 11:35:44 +02:00
"content" text,
"cw" text,
"files" set<frozen<drive_file>>,
"updatedAt" timestamp,
2023-07-19 16:35:47 +02:00
);
2023-08-19 11:01:25 +02:00
CREATE TYPE emoji (
2023-07-24 11:35:44 +02:00
"name" text,
"url" text,
"width" int,
"height" int,
2023-07-19 16:35:47 +02:00
);
2023-08-19 11:01:25 +02:00
CREATE TYPE poll (
2023-08-17 15:45:58 +02:00
"expiresAt" timestamp,
"multiple" boolean,
"choices" map<int, text>,
);
2023-08-19 11:01:25 +02:00
CREATE TABLE note ( -- Store all posts
2023-07-24 11:35:44 +02:00
"createdAtDate" date, -- For partitioning
"createdAt" timestamp,
"id" ascii, -- Post
"visibility" ascii,
"content" text,
"name" text,
"cw" text,
"localOnly" boolean,
"renoteCount" int,
"repliesCount" int,
"uri" text,
"url" text,
"score" int,
"files" set<frozen<drive_file>>,
2023-07-25 13:28:08 +02:00
"visibleUserIds" set<ascii>,
2023-07-24 11:35:44 +02:00
"mentions" set<ascii>,
2023-07-31 03:41:45 +02:00
"mentionedRemoteUsers" text,
2023-07-25 13:28:08 +02:00
"emojis" set<text>,
2023-07-24 11:35:44 +02:00
"tags" set<text>,
"hasPoll" boolean,
2023-08-18 06:24:24 +02:00
"poll" frozen<poll>,
2023-07-24 11:35:44 +02:00
"threadId" ascii,
"channelId" ascii, -- Channel
"userId" ascii, -- User
2023-07-27 03:58:38 +02:00
"userHost" text,
2023-07-24 11:35:44 +02:00
"replyId" ascii, -- Reply
2023-07-27 03:58:38 +02:00
"replyUserId" ascii,
"replyUserHost" text,
2023-08-05 23:42:45 +02:00
"replyContent" text,
"replyCw" text,
"replyFiles" set<frozen<drive_file>>,
2023-07-24 11:35:44 +02:00
"renoteId" ascii, -- Boost
2023-07-27 03:58:38 +02:00
"renoteUserId" ascii,
"renoteUserHost" text,
2023-08-05 23:42:45 +02:00
"renoteContent" text,
"renoteCw" text,
"renoteFiles" set<frozen<drive_file>>,
2023-07-27 03:58:38 +02:00
"reactions" map<text, int>, -- Reactions
2023-07-24 11:35:44 +02:00
"noteEdit" set<frozen<note_edit_history>>, -- Edit History
"updatedAt" timestamp,
2023-08-22 16:10:43 +02:00
PRIMARY KEY ("createdAtDate", "createdAt", "userId", "userHost", "visibility", "score")
2023-07-24 11:35:44 +02:00
) WITH CLUSTERING ORDER BY ("createdAt" DESC);
2023-07-19 16:35:47 +02:00
2023-08-19 11:01:25 +02:00
CREATE INDEX note_by_uri ON note ("uri");
CREATE INDEX note_by_url ON note ("url");
CREATE INDEX note_by_reply_id ON note ("replyId");
2023-07-19 16:35:47 +02:00
2023-08-19 11:01:25 +02:00
CREATE MATERIALIZED VIEW note_by_id AS
2023-08-14 08:49:59 +02:00
SELECT * FROM note
WHERE "id" IS NOT NULL
AND "createdAt" IS NOT NULL
AND "createdAtDate" IS NOT NULL
AND "userId" IS NOT NULL
AND "userHost" IS NOT NULL
AND "visibility" IS NOT NULL
2023-08-22 16:10:43 +02:00
AND "score" IS NOT NULL
PRIMARY KEY ("id", "createdAt", "createdAtDate", "userId", "userHost", "visibility", "score")
2023-08-14 08:49:59 +02:00
WITH CLUSTERING ORDER BY ("createdAt" DESC);
2023-08-19 11:01:25 +02:00
CREATE MATERIALIZED VIEW note_by_user_id AS
2023-07-19 16:35:47 +02:00
SELECT * FROM note
2023-07-24 11:35:44 +02:00
WHERE "userId" IS NOT NULL
AND "createdAt" IS NOT NULL
AND "createdAtDate" IS NOT NULL
2023-08-12 09:01:21 +02:00
AND "userHost" IS NOT NULL
AND "visibility" IS NOT NULL
2023-08-22 16:10:43 +02:00
AND "score" IS NOT NULL
PRIMARY KEY ("userId", "createdAt", "createdAtDate", "userHost", "visibility", "score")
2023-07-24 11:35:44 +02:00
WITH CLUSTERING ORDER BY ("createdAt" DESC);
2023-07-19 16:35:47 +02:00
2023-08-19 11:01:25 +02:00
CREATE MATERIALIZED VIEW note_by_renote_id AS
SELECT * FROM note
WHERE "renoteId" IS NOT NULL
AND "createdAt" IS NOT NULL
AND "createdAtDate" IS NOT NULL
2023-08-10 07:09:37 +02:00
AND "userId" IS NOT NULL
2023-08-12 09:01:21 +02:00
AND "userHost" IS NOT NULL
AND "visibility" IS NOT NULL
2023-08-22 16:10:43 +02:00
AND "score" IS NOT NULL
PRIMARY KEY ("renoteId", "createdAt", "createdAtDate", "userId", "userHost", "visibility", "score")
WITH CLUSTERING ORDER BY ("createdAt" DESC);
2023-08-19 11:01:25 +02:00
CREATE MATERIALIZED VIEW note_by_renote_id_and_user_id AS
SELECT "renoteId", "userId", "createdAt", "createdAtDate", "userHost", "visibility", "id" FROM note
WHERE "renoteId" IS NOT NULL
AND "createdAt" IS NOT NULL
AND "createdAtDate" IS NOT NULL
AND "userId" IS NOT NULL
AND "userHost" IS NOT NULL
AND "visibility" IS NOT NULL
2023-08-22 16:10:43 +02:00
AND "score" IS NOT NULL
PRIMARY KEY (("renoteId", "userId"), "createdAt", "createdAtDate", "userHost", "visibility", "score")
WITH CLUSTERING ORDER BY ("createdAt" DESC);
2023-08-19 11:01:25 +02:00
CREATE MATERIALIZED VIEW note_by_channel_id AS
2023-08-17 09:56:42 +02:00
SELECT * FROM note
WHERE "channelId" IS NOT NULL
AND "createdAt" IS NOT NULL
AND "createdAtDate" IS NOT NULL
AND "userId" IS NOT NULL
AND "userHost" IS NOT NULL
AND "visibility" IS NOT NULL
2023-08-22 16:10:43 +02:00
AND "score" IS NOT NULL
PRIMARY KEY ("channelId", "createdAt", "createdAtDate", "userId", "userHost", "visibility", "score")
2023-08-17 09:56:42 +02:00
WITH CLUSTERING ORDER BY ("createdAt" DESC);
2023-08-19 11:01:25 +02:00
CREATE MATERIALIZED VIEW global_timeline AS
2023-08-12 09:01:21 +02:00
SELECT * FROM note
WHERE "createdAtDate" IS NOT NULL
AND "createdAt" IS NOT NULL
AND "userId" IS NOT NULL
AND "userHost" IS NOT NULL
2023-08-22 16:10:43 +02:00
AND "score" IS NOT NULL
2023-08-12 09:15:20 +02:00
AND "visibility" = 'public'
2023-08-22 16:10:43 +02:00
PRIMARY KEY ("createdAtDate", "createdAt", "userId", "userHost", "visibility", "score")
2023-08-12 10:00:46 +02:00
WITH CLUSTERING ORDER BY ("createdAt" DESC);
2023-08-10 20:32:05 +02:00
2023-08-19 11:01:25 +02:00
CREATE MATERIALIZED VIEW local_timeline AS
2023-08-12 09:01:21 +02:00
SELECT * FROM note
WHERE "createdAtDate" IS NOT NULL
AND "createdAt" IS NOT NULL
AND "userId" IS NOT NULL
2023-08-12 09:15:20 +02:00
AND "userHost" = 'local'
AND "visibility" = 'public'
2023-08-22 16:10:43 +02:00
AND "score" IS NOT NULL
PRIMARY KEY ("createdAtDate", "createdAt", "userId", "userHost", "visibility", "score")
2023-08-12 10:00:46 +02:00
WITH CLUSTERING ORDER BY ("createdAt" DESC);
2023-08-22 15:42:01 +02:00
CREATE MATERIALIZED VIEW score_feed AS
SELECT * FROM note
WHERE "createdAtDate" IS NOT NULL
AND "createdAt" IS NOT NULL
AND "userId" IS NOT NULL
AND "userHost" IS NOT NULL
AND "score" IS NOT NULL
AND "visibility" = 'public'
2023-08-22 16:10:43 +02:00
AND "score" > 0
PRIMARY KEY ("createdAtDate", "score", "userHost", "createdAt", "userId" "visibility")
2023-08-22 15:42:01 +02:00
WITH CLUSTERING ORDER BY ("score" DESC, "createdAt" DESC);
2023-08-12 09:01:21 +02:00
2023-08-19 11:01:25 +02:00
CREATE TABLE home_timeline (
2023-08-12 09:01:21 +02:00
"feedUserId" ascii, -- For partitioning
"createdAtDate" date, -- For partitioning
"createdAt" timestamp,
"id" ascii, -- Post
"visibility" ascii,
"content" text,
"name" text,
"cw" text,
"localOnly" boolean,
"renoteCount" int,
"repliesCount" int,
"uri" text,
"url" text,
"score" int,
"files" set<frozen<drive_file>>,
"visibleUserIds" set<ascii>,
"mentions" set<ascii>,
"mentionedRemoteUsers" text,
"emojis" set<text>,
"tags" set<text>,
"hasPoll" boolean,
2023-08-18 06:24:24 +02:00
"poll" frozen<poll>,
2023-08-12 09:01:21 +02:00
"threadId" ascii,
"channelId" ascii, -- Channel
"userId" ascii, -- User
"userHost" text,
"replyId" ascii, -- Reply
2023-08-10 20:32:05 +02:00
"replyUserId" ascii,
"replyUserHost" text,
2023-08-12 09:01:21 +02:00
"replyContent" text,
"replyCw" text,
"replyFiles" set<frozen<drive_file>>,
"renoteId" ascii, -- Boost
2023-08-10 20:32:05 +02:00
"renoteUserId" ascii,
"renoteUserHost" text,
2023-08-12 09:01:21 +02:00
"renoteContent" text,
"renoteCw" text,
"renoteFiles" set<frozen<drive_file>>,
"reactions" map<text, int>, -- Reactions
"noteEdit" set<frozen<note_edit_history>>, -- Edit History
"updatedAt" timestamp,
PRIMARY KEY (("feedUserId", "createdAtDate"), "createdAt", "userId")
2023-08-12 09:01:21 +02:00
) WITH CLUSTERING ORDER BY ("createdAt" DESC);
2023-08-10 20:32:05 +02:00
2023-08-19 11:01:25 +02:00
CREATE INDEX home_by_id ON home_timeline ("id");
2023-08-19 11:01:25 +02:00
CREATE TABLE reaction (
2023-07-24 13:52:35 +02:00
"id" text,
2023-07-24 11:35:44 +02:00
"noteId" ascii,
"userId" ascii,
2023-07-24 13:52:35 +02:00
"reaction" text,
"emoji" frozen<emoji>,
"createdAt" timestamp,
PRIMARY KEY ("noteId", "userId") -- this key constraints one reaction per user for the same post
2023-07-24 13:52:35 +02:00
);
2023-08-19 11:01:25 +02:00
CREATE MATERIALIZED VIEW reaction_by_user_id AS
2023-07-24 13:52:35 +02:00
SELECT * FROM reaction
WHERE "userId" IS NOT NULL
AND "createdAt" IS NOT NULL
AND "noteId" IS NOT NULL
PRIMARY KEY ("userId", "createdAt", "noteId")
WITH CLUSTERING ORDER BY ("createdAt" DESC);
2023-08-19 11:01:25 +02:00
CREATE MATERIALIZED VIEW reaction_by_id AS
SELECT * FROM reaction
WHERE "noteId" IS NOT NULL
AND "reaction" IS NOT NULL
AND "userId" IS NOT NULL
PRIMARY KEY ("noteId", "reaction", "userId");
2023-08-17 15:45:58 +02:00
2023-08-19 11:01:25 +02:00
CREATE TABLE poll_vote (
2023-08-17 15:45:58 +02:00
"noteId" ascii,
"userId" ascii,
"choice" set<int>,
"createdAt" timestamp,
PRIMARY KEY ("noteId", "userId")
);