CREATE TYPE IF NOT EXISTS drive_file ( "id" ascii, "type" ascii, "createdAt" timestamp, "name" text, "comment" text, "blurhash" text, "url" text, "thumbnailUrl" text, "isSensitive" boolean, "isLink" boolean, "md5" ascii, "size" int, "width" int, "height" int, ); CREATE TYPE IF NOT EXISTS note_edit_history ( "content" text, "cw" text, "files" set>, "updatedAt" timestamp, ); CREATE TYPE IF NOT EXISTS emoji ( "name" text, "url" text, "width" int, "height" int, ); CREATE TABLE IF NOT EXISTS note ( -- Models timeline "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>, "visibleUserIds" set, "mentions" set, "mentionedRemoteUsers" text, "emojis" set, "tags" set, "hasPoll" boolean, "threadId" ascii, "channelId" ascii, -- Channel "userId" ascii, -- User "userHost" text, "replyId" ascii, -- Reply "replyUserId" ascii, "replyUserHost" text, "replyContent" text, "replyCw" text, "replyFiles" set>, "renoteId" ascii, -- Boost "renoteUserId" ascii, "renoteUserHost" text, "renoteContent" text, "renoteCw" text, "renoteFiles" set>, "reactions" map, -- Reactions "noteEdit" set>, -- Edit History "updatedAt" timestamp, PRIMARY KEY ("createdAtDate", "createdAt", "userId") ) WITH CLUSTERING ORDER BY ("createdAt" DESC); 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 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 SELECT * FROM note WHERE "userId" IS NOT NULL AND "createdAt" IS NOT NULL AND "createdAtDate" IS NOT NULL PRIMARY KEY ("userId", "createdAt", "createdAtDate") WITH CLUSTERING ORDER BY ("createdAt" DESC); 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 AND "userId" IS NOT NULL PRIMARY KEY ("renoteId", "createdAt", "createdAtDate", "userId") WITH CLUSTERING ORDER BY ("createdAt" DESC); CREATE TABLE IF NOT EXISTS home_feed ( "userId" ascii, "fedAtDate" date, "fedAt" timestamp, "noteId" ascii, "noteUserId" ascii, "noteUserHost" text, "replyUserId" ascii, "replyUserHost" text, "renoteUserId" ascii, "renoteUserHost" text, PRIMARY KEY (("userId", "fedAtDate"), "fedAt") ) WITH CLUSTERING ORDER BY ("fedAt" DESC); CREATE TABLE IF NOT EXISTS local_feed ( "fedAtDate" date, "fedAt" timestamp, "noteId" ascii, "noteUserId" ascii, "noteUserHost" text, "replyUserId" ascii, "replyUserHost" text, "renoteUserId" ascii, "renoteUserHost" text, PRIMARY KEY ("fedAtDate", "fedAt") ) WITH CLUSTERING ORDER BY ("fedAt" DESC); CREATE TABLE IF NOT EXISTS reaction ( "id" text, "noteId" ascii, "userId" ascii, "reaction" text, "emoji" frozen, "createdAt" timestamp, PRIMARY KEY ("noteId", "userId") -- this key constraints one reaction per user for the same post ); CREATE MATERIALIZED VIEW IF NOT EXISTS reaction_by_user_id AS 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");