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

196 lines
5.2 KiB
Cassandra CQL
Raw Normal View History

2023-07-19 16:35:47 +02:00
CREATE TYPE IF NOT EXISTS 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
);
CREATE TYPE IF NOT EXISTS 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
);
CREATE TYPE IF NOT EXISTS 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-12 09:01:21 +02:00
CREATE TABLE IF NOT EXISTS 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,
"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-12 09:01:21 +02:00
PRIMARY KEY ("createdAtDate", "createdAt", "userId", "userHost", "visibility")
2023-07-24 11:35:44 +02:00
) WITH CLUSTERING ORDER BY ("createdAt" DESC);
2023-07-19 16:35:47 +02:00
2023-07-31 03:41:45 +02:00
CREATE INDEX IF NOT EXISTS note_by_uri ON note ("uri");
CREATE INDEX IF NOT EXISTS note_by_url ON note ("url");
CREATE MATERIALIZED VIEW IF NOT EXISTS note_by_id AS
SELECT * FROM note
WHERE "id" 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
PRIMARY KEY ("id", "createdAt", "createdAtDate", "userId", "userHost", "visibility")
2023-07-31 03:41:45 +02:00
WITH CLUSTERING ORDER BY ("createdAt" DESC);
2023-07-19 16:35:47 +02:00
CREATE MATERIALIZED VIEW IF NOT EXISTS 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
PRIMARY KEY ("userId", "createdAt", "createdAtDate", "userHost", "visibility")
2023-07-24 11:35:44 +02:00
WITH CLUSTERING ORDER BY ("createdAt" DESC);
2023-07-19 16:35:47 +02:00
CREATE MATERIALIZED VIEW IF NOT EXISTS 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
PRIMARY KEY ("renoteId", "createdAt", "createdAtDate", "userId", "userHost", "visibility")
WITH CLUSTERING ORDER BY ("createdAt" DESC);
2023-08-12 09:01:21 +02:00
CREATE MATERIALIZED VIEW IF NOT EXISTS global_timeline AS
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-12 09:15:20 +02:00
AND "visibility" = 'public'
2023-08-12 09:01:21 +02:00
PRIMARY KEY ("createdAtDate", "createdAt", "userId", "userHost", "visibility");
2023-08-10 20:32:05 +02:00
2023-08-12 09:01:21 +02:00
CREATE MATERIALIZED VIEW IF NOT EXISTS local_timeline AS
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-12 09:01:21 +02:00
PRIMARY KEY ("createdAtDate", "createdAt", "userId", "userHost", "visibility");
CREATE TABLE IF NOT EXISTS home_timeline (
"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,
"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")
) WITH CLUSTERING ORDER BY ("createdAt" DESC);
2023-08-10 20:32:05 +02:00
2023-07-19 16:35:47 +02:00
CREATE TABLE IF NOT EXISTS 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
);
CREATE MATERIALIZED VIEW IF NOT EXISTS 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);
CREATE MATERIALIZED VIEW IF NOT EXISTS 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");