You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

647 lines
42 KiB

  1. CREATE TABLE application_services_txns( as_id TEXT NOT NULL, txn_id INTEGER NOT NULL, event_ids TEXT NOT NULL, UNIQUE(as_id, txn_id) );
  2. CREATE INDEX application_services_txns_id ON application_services_txns ( as_id );
  3. CREATE TABLE presence( user_id TEXT NOT NULL, state VARCHAR(20), status_msg TEXT, mtime BIGINT, UNIQUE (user_id) );
  4. CREATE TABLE users( name TEXT, password_hash TEXT, creation_ts BIGINT, admin SMALLINT DEFAULT 0 NOT NULL, upgrade_ts BIGINT, is_guest SMALLINT DEFAULT 0 NOT NULL, appservice_id TEXT, consent_version TEXT, consent_server_notice_sent TEXT, user_type TEXT DEFAULT NULL, deactivated SMALLINT DEFAULT 0 NOT NULL, shadow_banned BOOLEAN, consent_ts bigint, UNIQUE(name) );
  5. CREATE TABLE user_ips ( user_id TEXT NOT NULL, access_token TEXT NOT NULL, device_id TEXT, ip TEXT NOT NULL, user_agent TEXT NOT NULL, last_seen BIGINT NOT NULL );
  6. CREATE TABLE profiles( user_id TEXT NOT NULL, displayname TEXT, avatar_url TEXT, UNIQUE(user_id) );
  7. CREATE TABLE received_transactions( transaction_id TEXT, origin TEXT, ts BIGINT, response_code INTEGER, response_json bytea, has_been_referenced smallint default 0, UNIQUE (transaction_id, origin) );
  8. CREATE TABLE destinations( destination TEXT PRIMARY KEY, retry_last_ts BIGINT, retry_interval INTEGER , failure_ts BIGINT, last_successful_stream_ordering BIGINT);
  9. CREATE TABLE events( stream_ordering INTEGER PRIMARY KEY, topological_ordering BIGINT NOT NULL, event_id TEXT NOT NULL, type TEXT NOT NULL, room_id TEXT NOT NULL, content TEXT, unrecognized_keys TEXT, processed BOOL NOT NULL, outlier BOOL NOT NULL, depth BIGINT DEFAULT 0 NOT NULL, origin_server_ts BIGINT, received_ts BIGINT, sender TEXT, contains_url BOOLEAN, instance_name TEXT, state_key TEXT DEFAULT NULL, rejection_reason TEXT DEFAULT NULL, UNIQUE (event_id) );
  10. CREATE INDEX events_order_room ON events ( room_id, topological_ordering, stream_ordering );
  11. CREATE TABLE event_json( event_id TEXT NOT NULL, room_id TEXT NOT NULL, internal_metadata TEXT NOT NULL, json TEXT NOT NULL, format_version INTEGER, UNIQUE (event_id) );
  12. CREATE TABLE state_events( event_id TEXT NOT NULL, room_id TEXT NOT NULL, type TEXT NOT NULL, state_key TEXT NOT NULL, prev_state TEXT, UNIQUE (event_id) );
  13. CREATE TABLE current_state_events( event_id TEXT NOT NULL, room_id TEXT NOT NULL, type TEXT NOT NULL, state_key TEXT NOT NULL, membership TEXT, UNIQUE (event_id), UNIQUE (room_id, type, state_key) );
  14. CREATE TABLE room_memberships( event_id TEXT NOT NULL, user_id TEXT NOT NULL, sender TEXT NOT NULL, room_id TEXT NOT NULL, membership TEXT NOT NULL, forgotten INTEGER DEFAULT 0, display_name TEXT, avatar_url TEXT, UNIQUE (event_id) );
  15. CREATE INDEX room_memberships_room_id ON room_memberships (room_id);
  16. CREATE INDEX room_memberships_user_id ON room_memberships (user_id);
  17. CREATE TABLE rooms( room_id TEXT PRIMARY KEY NOT NULL, is_public BOOL, creator TEXT , room_version TEXT, has_auth_chain_index BOOLEAN);
  18. CREATE TABLE server_signature_keys( server_name TEXT, key_id TEXT, from_server TEXT, ts_added_ms BIGINT, verify_key bytea, ts_valid_until_ms BIGINT, UNIQUE (server_name, key_id) );
  19. CREATE TABLE rejections( event_id TEXT NOT NULL, reason TEXT NOT NULL, last_check TEXT NOT NULL, UNIQUE (event_id) );
  20. CREATE TABLE push_rules ( id BIGINT PRIMARY KEY, user_name TEXT NOT NULL, rule_id TEXT NOT NULL, priority_class SMALLINT NOT NULL, priority INTEGER NOT NULL DEFAULT 0, conditions TEXT NOT NULL, actions TEXT NOT NULL, UNIQUE(user_name, rule_id) );
  21. CREATE INDEX push_rules_user_name on push_rules (user_name);
  22. CREATE TABLE push_rules_enable ( id BIGINT PRIMARY KEY, user_name TEXT NOT NULL, rule_id TEXT NOT NULL, enabled SMALLINT, UNIQUE(user_name, rule_id) );
  23. CREATE INDEX push_rules_enable_user_name on push_rules_enable (user_name);
  24. CREATE TABLE event_forward_extremities( event_id TEXT NOT NULL, room_id TEXT NOT NULL, UNIQUE (event_id, room_id) );
  25. CREATE INDEX ev_extrem_room ON event_forward_extremities(room_id);
  26. CREATE INDEX ev_extrem_id ON event_forward_extremities(event_id);
  27. CREATE TABLE event_backward_extremities( event_id TEXT NOT NULL, room_id TEXT NOT NULL, UNIQUE (event_id, room_id) );
  28. CREATE INDEX ev_b_extrem_room ON event_backward_extremities(room_id);
  29. CREATE INDEX ev_b_extrem_id ON event_backward_extremities(event_id);
  30. CREATE TABLE room_depth( room_id TEXT NOT NULL, min_depth INTEGER NOT NULL, UNIQUE (room_id) );
  31. CREATE TABLE event_to_state_groups( event_id TEXT NOT NULL, state_group BIGINT NOT NULL, UNIQUE (event_id) );
  32. CREATE TABLE local_media_repository ( media_id TEXT, media_type TEXT, media_length INTEGER, created_ts BIGINT, upload_name TEXT, user_id TEXT, quarantined_by TEXT, url_cache TEXT, last_access_ts BIGINT, safe_from_quarantine BOOLEAN NOT NULL DEFAULT 0, UNIQUE (media_id) );
  33. CREATE TABLE remote_media_cache ( media_origin TEXT, media_id TEXT, media_type TEXT, created_ts BIGINT, upload_name TEXT, media_length INTEGER, filesystem_id TEXT, last_access_ts BIGINT, quarantined_by TEXT, UNIQUE (media_origin, media_id) );
  34. CREATE TABLE redactions ( event_id TEXT NOT NULL, redacts TEXT NOT NULL, have_censored BOOL NOT NULL DEFAULT false, received_ts BIGINT, UNIQUE (event_id) );
  35. CREATE INDEX redactions_redacts ON redactions (redacts);
  36. CREATE TABLE room_aliases( room_alias TEXT NOT NULL, room_id TEXT NOT NULL, creator TEXT, UNIQUE (room_alias) );
  37. CREATE INDEX room_aliases_id ON room_aliases(room_id);
  38. CREATE TABLE room_alias_servers( room_alias TEXT NOT NULL, server TEXT NOT NULL );
  39. CREATE INDEX room_alias_servers_alias ON room_alias_servers(room_alias);
  40. CREATE TABLE IF NOT EXISTS "server_keys_json" ( server_name TEXT NOT NULL, key_id TEXT NOT NULL, from_server TEXT NOT NULL, ts_added_ms BIGINT NOT NULL, ts_valid_until_ms BIGINT NOT NULL, key_json bytea NOT NULL, CONSTRAINT server_keys_json_uniqueness UNIQUE (server_name, key_id, from_server) );
  41. CREATE TABLE e2e_device_keys_json ( user_id TEXT NOT NULL, device_id TEXT NOT NULL, ts_added_ms BIGINT NOT NULL, key_json TEXT NOT NULL, CONSTRAINT e2e_device_keys_json_uniqueness UNIQUE (user_id, device_id) );
  42. CREATE TABLE e2e_one_time_keys_json ( user_id TEXT NOT NULL, device_id TEXT NOT NULL, algorithm TEXT NOT NULL, key_id TEXT NOT NULL, ts_added_ms BIGINT NOT NULL, key_json TEXT NOT NULL, CONSTRAINT e2e_one_time_keys_json_uniqueness UNIQUE (user_id, device_id, algorithm, key_id) );
  43. CREATE TABLE IF NOT EXISTS "user_threepids" ( user_id TEXT NOT NULL, medium TEXT NOT NULL, address TEXT NOT NULL, validated_at BIGINT NOT NULL, added_at BIGINT NOT NULL, CONSTRAINT medium_address UNIQUE (medium, address) );
  44. CREATE INDEX user_threepids_user_id ON user_threepids(user_id);
  45. CREATE VIRTUAL TABLE event_search USING fts4 ( event_id, room_id, sender, key, value )
  46. /* event_search(event_id,room_id,sender,"key",value) */;
  47. CREATE TABLE room_tags( user_id TEXT NOT NULL, room_id TEXT NOT NULL, tag TEXT NOT NULL, content TEXT NOT NULL, CONSTRAINT room_tag_uniqueness UNIQUE (user_id, room_id, tag) );
  48. CREATE TABLE room_tags_revisions ( user_id TEXT NOT NULL, room_id TEXT NOT NULL, stream_id BIGINT NOT NULL, instance_name TEXT, CONSTRAINT room_tag_revisions_uniqueness UNIQUE (user_id, room_id) );
  49. CREATE TABLE account_data( user_id TEXT NOT NULL, account_data_type TEXT NOT NULL, stream_id BIGINT NOT NULL, content TEXT NOT NULL, instance_name TEXT, CONSTRAINT account_data_uniqueness UNIQUE (user_id, account_data_type) );
  50. CREATE TABLE room_account_data( user_id TEXT NOT NULL, room_id TEXT NOT NULL, account_data_type TEXT NOT NULL, stream_id BIGINT NOT NULL, content TEXT NOT NULL, instance_name TEXT, CONSTRAINT room_account_data_uniqueness UNIQUE (user_id, room_id, account_data_type) );
  51. CREATE INDEX account_data_stream_id on account_data(user_id, stream_id);
  52. CREATE INDEX room_account_data_stream_id on room_account_data(user_id, stream_id);
  53. CREATE INDEX events_ts ON events(origin_server_ts, stream_ordering);
  54. CREATE TABLE event_push_actions( room_id TEXT NOT NULL, event_id TEXT NOT NULL, user_id TEXT NOT NULL, profile_tag VARCHAR(32), actions TEXT NOT NULL, topological_ordering BIGINT, stream_ordering BIGINT, notif SMALLINT, highlight SMALLINT, unread SMALLINT, thread_id TEXT, CONSTRAINT event_id_user_id_profile_tag_uniqueness UNIQUE (room_id, event_id, user_id, profile_tag) );
  55. CREATE INDEX event_push_actions_room_id_user_id on event_push_actions(room_id, user_id);
  56. CREATE INDEX events_room_stream on events(room_id, stream_ordering);
  57. CREATE INDEX public_room_index on rooms(is_public);
  58. CREATE INDEX event_push_actions_rm_tokens on event_push_actions( user_id, room_id, topological_ordering, stream_ordering );
  59. CREATE TABLE presence_stream( stream_id BIGINT, user_id TEXT, state TEXT, last_active_ts BIGINT, last_federation_update_ts BIGINT, last_user_sync_ts BIGINT, status_msg TEXT, currently_active BOOLEAN , instance_name TEXT);
  60. CREATE INDEX presence_stream_id ON presence_stream(stream_id, user_id);
  61. CREATE INDEX presence_stream_user_id ON presence_stream(user_id);
  62. CREATE TABLE push_rules_stream( stream_id BIGINT NOT NULL, event_stream_ordering BIGINT NOT NULL, user_id TEXT NOT NULL, rule_id TEXT NOT NULL, op TEXT NOT NULL, priority_class SMALLINT, priority INTEGER, conditions TEXT, actions TEXT );
  63. CREATE INDEX push_rules_stream_id ON push_rules_stream(stream_id);
  64. CREATE INDEX push_rules_stream_user_stream_id on push_rules_stream(user_id, stream_id);
  65. CREATE TABLE ex_outlier_stream( event_stream_ordering BIGINT PRIMARY KEY NOT NULL, event_id TEXT NOT NULL, state_group BIGINT NOT NULL , instance_name TEXT);
  66. CREATE TABLE threepid_guest_access_tokens( medium TEXT, address TEXT, guest_access_token TEXT, first_inviter TEXT );
  67. CREATE UNIQUE INDEX threepid_guest_access_tokens_index ON threepid_guest_access_tokens(medium, address);
  68. CREATE INDEX event_push_actions_stream_ordering on event_push_actions( stream_ordering, user_id );
  69. CREATE TABLE open_id_tokens ( token TEXT NOT NULL PRIMARY KEY, ts_valid_until_ms bigint NOT NULL, user_id TEXT NOT NULL, UNIQUE (token) );
  70. CREATE INDEX open_id_tokens_ts_valid_until_ms ON open_id_tokens(ts_valid_until_ms);
  71. CREATE TABLE pusher_throttle( pusher BIGINT NOT NULL, room_id TEXT NOT NULL, last_sent_ts BIGINT, throttle_ms BIGINT, PRIMARY KEY (pusher, room_id) );
  72. CREATE TABLE event_reports( id BIGINT NOT NULL PRIMARY KEY, received_ts BIGINT NOT NULL, room_id TEXT NOT NULL, event_id TEXT NOT NULL, user_id TEXT NOT NULL, reason TEXT, content TEXT );
  73. CREATE TABLE appservice_stream_position( Lock CHAR(1) NOT NULL DEFAULT 'X' UNIQUE, stream_ordering BIGINT, CHECK (Lock='X') );
  74. CREATE TABLE device_inbox ( user_id TEXT NOT NULL, device_id TEXT NOT NULL, stream_id BIGINT NOT NULL, message_json TEXT NOT NULL , instance_name TEXT);
  75. CREATE INDEX device_inbox_user_stream_id ON device_inbox(user_id, device_id, stream_id);
  76. CREATE INDEX received_transactions_ts ON received_transactions(ts);
  77. CREATE TABLE device_federation_outbox ( destination TEXT NOT NULL, stream_id BIGINT NOT NULL, queued_ts BIGINT NOT NULL, messages_json TEXT NOT NULL , instance_name TEXT);
  78. CREATE INDEX device_federation_outbox_destination_id ON device_federation_outbox(destination, stream_id);
  79. CREATE TABLE device_federation_inbox ( origin TEXT NOT NULL, message_id TEXT NOT NULL, received_ts BIGINT NOT NULL , instance_name TEXT);
  80. CREATE INDEX device_federation_inbox_sender_id ON device_federation_inbox(origin, message_id);
  81. CREATE TABLE stream_ordering_to_exterm ( stream_ordering BIGINT NOT NULL, room_id TEXT NOT NULL, event_id TEXT NOT NULL );
  82. CREATE INDEX stream_ordering_to_exterm_idx on stream_ordering_to_exterm( stream_ordering );
  83. CREATE INDEX stream_ordering_to_exterm_rm_idx on stream_ordering_to_exterm( room_id, stream_ordering );
  84. CREATE TABLE IF NOT EXISTS "event_auth"( event_id TEXT NOT NULL, auth_id TEXT NOT NULL, room_id TEXT NOT NULL );
  85. CREATE INDEX evauth_edges_id ON event_auth(event_id);
  86. CREATE INDEX user_threepids_medium_address on user_threepids (medium, address);
  87. CREATE TABLE appservice_room_list( appservice_id TEXT NOT NULL, network_id TEXT NOT NULL, room_id TEXT NOT NULL );
  88. CREATE UNIQUE INDEX appservice_room_list_idx ON appservice_room_list( appservice_id, network_id, room_id );
  89. CREATE INDEX device_federation_outbox_id ON device_federation_outbox(stream_id);
  90. CREATE TABLE federation_stream_position( type TEXT NOT NULL, stream_id INTEGER NOT NULL , instance_name TEXT NOT NULL DEFAULT 'master');
  91. CREATE TABLE device_lists_remote_cache ( user_id TEXT NOT NULL, device_id TEXT NOT NULL, content TEXT NOT NULL );
  92. CREATE TABLE device_lists_remote_extremeties ( user_id TEXT NOT NULL, stream_id TEXT NOT NULL );
  93. CREATE TABLE device_lists_stream ( stream_id BIGINT NOT NULL, user_id TEXT NOT NULL, device_id TEXT NOT NULL );
  94. CREATE INDEX device_lists_stream_id ON device_lists_stream(stream_id, user_id);
  95. CREATE TABLE device_lists_outbound_pokes ( destination TEXT NOT NULL, stream_id BIGINT NOT NULL, user_id TEXT NOT NULL, device_id TEXT NOT NULL, sent BOOLEAN NOT NULL, ts BIGINT NOT NULL , opentracing_context TEXT);
  96. CREATE INDEX device_lists_outbound_pokes_id ON device_lists_outbound_pokes(destination, stream_id);
  97. CREATE INDEX device_lists_outbound_pokes_user ON device_lists_outbound_pokes(destination, user_id);
  98. CREATE TABLE event_push_summary ( user_id TEXT NOT NULL, room_id TEXT NOT NULL, notif_count BIGINT NOT NULL, stream_ordering BIGINT NOT NULL , unread_count BIGINT, last_receipt_stream_ordering BIGINT, thread_id TEXT);
  99. CREATE TABLE event_push_summary_stream_ordering ( Lock CHAR(1) NOT NULL DEFAULT 'X' UNIQUE, stream_ordering BIGINT NOT NULL, CHECK (Lock='X') );
  100. CREATE TABLE IF NOT EXISTS "pushers" ( id BIGINT PRIMARY KEY, user_name TEXT NOT NULL, access_token BIGINT DEFAULT NULL, profile_tag TEXT NOT NULL, kind TEXT NOT NULL, app_id TEXT NOT NULL, app_display_name TEXT NOT NULL, device_display_name TEXT NOT NULL, pushkey TEXT NOT NULL, ts BIGINT NOT NULL, lang TEXT, data TEXT, last_stream_ordering INTEGER, last_success BIGINT, failing_since BIGINT, UNIQUE (app_id, pushkey, user_name) );
  101. CREATE INDEX device_lists_outbound_pokes_stream ON device_lists_outbound_pokes(stream_id);
  102. CREATE TABLE ratelimit_override ( user_id TEXT NOT NULL, messages_per_second BIGINT, burst_count BIGINT );
  103. CREATE UNIQUE INDEX ratelimit_override_idx ON ratelimit_override(user_id);
  104. CREATE TABLE current_state_delta_stream ( stream_id BIGINT NOT NULL, room_id TEXT NOT NULL, type TEXT NOT NULL, state_key TEXT NOT NULL, event_id TEXT, prev_event_id TEXT , instance_name TEXT);
  105. CREATE INDEX current_state_delta_stream_idx ON current_state_delta_stream(stream_id);
  106. CREATE TABLE user_directory_stream_pos ( Lock CHAR(1) NOT NULL DEFAULT 'X' UNIQUE, stream_id BIGINT, CHECK (Lock='X') );
  107. CREATE VIRTUAL TABLE user_directory_search USING fts4 ( user_id, value )
  108. /* user_directory_search(user_id,value) */;
  109. CREATE TABLE blocked_rooms ( room_id TEXT NOT NULL, user_id TEXT NOT NULL );
  110. CREATE UNIQUE INDEX blocked_rooms_idx ON blocked_rooms(room_id);
  111. CREATE TABLE IF NOT EXISTS "local_media_repository_url_cache"( url TEXT, response_code INTEGER, etag TEXT, expires_ts BIGINT, og TEXT, media_id TEXT, download_ts BIGINT );
  112. CREATE INDEX local_media_repository_url_cache_expires_idx ON local_media_repository_url_cache(expires_ts);
  113. CREATE INDEX local_media_repository_url_cache_by_url_download_ts ON local_media_repository_url_cache(url, download_ts);
  114. CREATE INDEX local_media_repository_url_cache_media_idx ON local_media_repository_url_cache(media_id);
  115. CREATE TABLE IF NOT EXISTS "deleted_pushers" ( stream_id BIGINT NOT NULL, app_id TEXT NOT NULL, pushkey TEXT NOT NULL, user_id TEXT NOT NULL );
  116. CREATE INDEX deleted_pushers_stream_id ON deleted_pushers (stream_id);
  117. CREATE TABLE IF NOT EXISTS "user_directory" ( user_id TEXT NOT NULL, room_id TEXT, display_name TEXT, avatar_url TEXT );
  118. CREATE INDEX user_directory_room_idx ON user_directory(room_id);
  119. CREATE UNIQUE INDEX user_directory_user_idx ON user_directory(user_id);
  120. CREATE TABLE event_push_actions_staging ( event_id TEXT NOT NULL, user_id TEXT NOT NULL, actions TEXT NOT NULL, notif SMALLINT NOT NULL, highlight SMALLINT NOT NULL , unread SMALLINT, thread_id TEXT);
  121. CREATE INDEX event_push_actions_staging_id ON event_push_actions_staging(event_id);
  122. CREATE TABLE users_pending_deactivation ( user_id TEXT NOT NULL );
  123. CREATE TABLE user_daily_visits ( user_id TEXT NOT NULL, device_id TEXT, timestamp BIGINT NOT NULL , user_agent TEXT);
  124. CREATE INDEX user_daily_visits_uts_idx ON user_daily_visits(user_id, timestamp);
  125. CREATE INDEX user_daily_visits_ts_idx ON user_daily_visits(timestamp);
  126. CREATE TABLE erased_users ( user_id TEXT NOT NULL );
  127. CREATE UNIQUE INDEX erased_users_user ON erased_users(user_id);
  128. CREATE TABLE monthly_active_users ( user_id TEXT NOT NULL, timestamp BIGINT NOT NULL );
  129. CREATE UNIQUE INDEX monthly_active_users_users ON monthly_active_users(user_id);
  130. CREATE INDEX monthly_active_users_time_stamp ON monthly_active_users(timestamp);
  131. CREATE TABLE IF NOT EXISTS "e2e_room_keys_versions" ( user_id TEXT NOT NULL, version BIGINT NOT NULL, algorithm TEXT NOT NULL, auth_data TEXT NOT NULL, deleted SMALLINT DEFAULT 0 NOT NULL , etag BIGINT);
  132. CREATE UNIQUE INDEX e2e_room_keys_versions_idx ON e2e_room_keys_versions(user_id, version);
  133. CREATE TABLE IF NOT EXISTS "e2e_room_keys" ( user_id TEXT NOT NULL, room_id TEXT NOT NULL, session_id TEXT NOT NULL, version BIGINT NOT NULL, first_message_index INT, forwarded_count INT, is_verified BOOLEAN, session_data TEXT NOT NULL );
  134. CREATE TABLE users_who_share_private_rooms ( user_id TEXT NOT NULL, other_user_id TEXT NOT NULL, room_id TEXT NOT NULL );
  135. CREATE UNIQUE INDEX users_who_share_private_rooms_u_idx ON users_who_share_private_rooms(user_id, other_user_id, room_id);
  136. CREATE INDEX users_who_share_private_rooms_r_idx ON users_who_share_private_rooms(room_id);
  137. CREATE INDEX users_who_share_private_rooms_o_idx ON users_who_share_private_rooms(other_user_id);
  138. CREATE TABLE user_threepid_id_server ( user_id TEXT NOT NULL, medium TEXT NOT NULL, address TEXT NOT NULL, id_server TEXT NOT NULL );
  139. CREATE UNIQUE INDEX user_threepid_id_server_idx ON user_threepid_id_server( user_id, medium, address, id_server );
  140. CREATE TABLE users_in_public_rooms ( user_id TEXT NOT NULL, room_id TEXT NOT NULL );
  141. CREATE UNIQUE INDEX users_in_public_rooms_u_idx ON users_in_public_rooms(user_id, room_id);
  142. CREATE TABLE account_validity ( user_id TEXT PRIMARY KEY, expiration_ts_ms BIGINT NOT NULL, email_sent BOOLEAN NOT NULL, renewal_token TEXT , token_used_ts_ms BIGINT);
  143. CREATE TABLE event_relations ( event_id TEXT NOT NULL, relates_to_id TEXT NOT NULL, relation_type TEXT NOT NULL, aggregation_key TEXT );
  144. CREATE UNIQUE INDEX event_relations_id ON event_relations(event_id);
  145. CREATE INDEX event_relations_relates ON event_relations(relates_to_id, relation_type, aggregation_key);
  146. CREATE TABLE room_stats_earliest_token ( room_id TEXT NOT NULL, token BIGINT NOT NULL );
  147. CREATE UNIQUE INDEX room_stats_earliest_token_idx ON room_stats_earliest_token(room_id);
  148. CREATE INDEX user_ips_device_id ON user_ips (user_id, device_id, last_seen);
  149. CREATE INDEX event_push_actions_u_highlight ON event_push_actions (user_id, stream_ordering);
  150. CREATE INDEX device_inbox_stream_id_user_id ON device_inbox (stream_id, user_id);
  151. CREATE INDEX device_lists_stream_user_id ON device_lists_stream (user_id, device_id);
  152. CREATE INDEX user_ips_last_seen ON user_ips (user_id, last_seen);
  153. CREATE INDEX user_ips_last_seen_only ON user_ips (last_seen);
  154. CREATE INDEX users_creation_ts ON users (creation_ts);
  155. CREATE INDEX event_to_state_groups_sg_index ON event_to_state_groups (state_group);
  156. CREATE UNIQUE INDEX device_lists_remote_cache_unique_id ON device_lists_remote_cache (user_id, device_id);
  157. CREATE UNIQUE INDEX device_lists_remote_extremeties_unique_idx ON device_lists_remote_extremeties (user_id);
  158. CREATE UNIQUE INDEX user_ips_user_token_ip_unique_index ON user_ips (user_id, access_token, ip);
  159. CREATE TABLE threepid_validation_session (
  160. session_id TEXT PRIMARY KEY,
  161. medium TEXT NOT NULL,
  162. address TEXT NOT NULL,
  163. client_secret TEXT NOT NULL,
  164. last_send_attempt BIGINT NOT NULL,
  165. validated_at BIGINT
  166. );
  167. CREATE TABLE threepid_validation_token (
  168. token TEXT PRIMARY KEY,
  169. session_id TEXT NOT NULL,
  170. next_link TEXT,
  171. expires BIGINT NOT NULL
  172. );
  173. CREATE INDEX threepid_validation_token_session_id ON threepid_validation_token(session_id);
  174. CREATE TABLE event_expiry (
  175. event_id TEXT PRIMARY KEY,
  176. expiry_ts BIGINT NOT NULL
  177. );
  178. CREATE INDEX event_expiry_expiry_ts_idx ON event_expiry(expiry_ts);
  179. CREATE TABLE event_labels (
  180. event_id TEXT,
  181. label TEXT,
  182. room_id TEXT NOT NULL,
  183. topological_ordering BIGINT NOT NULL,
  184. PRIMARY KEY(event_id, label)
  185. );
  186. CREATE INDEX event_labels_room_id_label_idx ON event_labels(room_id, label, topological_ordering);
  187. CREATE UNIQUE INDEX e2e_room_keys_with_version_idx ON e2e_room_keys(user_id, version, room_id, session_id);
  188. CREATE TABLE IF NOT EXISTS "devices" (
  189. user_id TEXT NOT NULL,
  190. device_id TEXT NOT NULL,
  191. display_name TEXT,
  192. last_seen BIGINT,
  193. ip TEXT,
  194. user_agent TEXT,
  195. hidden BOOLEAN DEFAULT 0,
  196. CONSTRAINT device_uniqueness UNIQUE (user_id, device_id)
  197. );
  198. CREATE TABLE room_retention(
  199. room_id TEXT,
  200. event_id TEXT,
  201. min_lifetime BIGINT,
  202. max_lifetime BIGINT,
  203. PRIMARY KEY(room_id, event_id)
  204. );
  205. CREATE INDEX room_retention_max_lifetime_idx on room_retention(max_lifetime);
  206. CREATE TABLE e2e_cross_signing_keys (
  207. user_id TEXT NOT NULL,
  208. -- the type of cross-signing key (master, user_signing, or self_signing)
  209. keytype TEXT NOT NULL,
  210. -- the full key information, as a json-encoded dict
  211. keydata TEXT NOT NULL,
  212. -- for keeping the keys in order, so that we can fetch the latest one
  213. stream_id BIGINT NOT NULL
  214. );
  215. CREATE UNIQUE INDEX e2e_cross_signing_keys_idx ON e2e_cross_signing_keys(user_id, keytype, stream_id);
  216. CREATE TABLE e2e_cross_signing_signatures (
  217. -- user who did the signing
  218. user_id TEXT NOT NULL,
  219. -- key used to sign
  220. key_id TEXT NOT NULL,
  221. -- user who was signed
  222. target_user_id TEXT NOT NULL,
  223. -- device/key that was signed
  224. target_device_id TEXT NOT NULL,
  225. -- the actual signature
  226. signature TEXT NOT NULL
  227. );
  228. CREATE TABLE user_signature_stream (
  229. -- uses the same stream ID as device list stream
  230. stream_id BIGINT NOT NULL,
  231. -- user who did the signing
  232. from_user_id TEXT NOT NULL,
  233. -- list of users who were signed, as a JSON array
  234. user_ids TEXT NOT NULL
  235. );
  236. CREATE UNIQUE INDEX user_signature_stream_idx ON user_signature_stream(stream_id);
  237. CREATE INDEX e2e_cross_signing_signatures2_idx ON e2e_cross_signing_signatures(user_id, target_user_id, target_device_id);
  238. CREATE TABLE stats_incremental_position (
  239. Lock CHAR(1) NOT NULL DEFAULT 'X' UNIQUE, -- Makes sure this table only has one row.
  240. stream_id BIGINT NOT NULL,
  241. CHECK (Lock='X')
  242. );
  243. CREATE TABLE room_stats_current (
  244. room_id TEXT NOT NULL PRIMARY KEY,
  245. -- These are absolute counts
  246. current_state_events INT NOT NULL,
  247. joined_members INT NOT NULL,
  248. invited_members INT NOT NULL,
  249. left_members INT NOT NULL,
  250. banned_members INT NOT NULL,
  251. local_users_in_room INT NOT NULL,
  252. -- The maximum delta stream position that this row takes into account.
  253. completed_delta_stream_id BIGINT NOT NULL
  254. , knocked_members INT);
  255. CREATE TABLE user_stats_current (
  256. user_id TEXT NOT NULL PRIMARY KEY,
  257. joined_rooms BIGINT NOT NULL,
  258. -- The maximum delta stream position that this row takes into account.
  259. completed_delta_stream_id BIGINT NOT NULL
  260. );
  261. CREATE TABLE room_stats_state (
  262. room_id TEXT NOT NULL,
  263. name TEXT,
  264. canonical_alias TEXT,
  265. join_rules TEXT,
  266. history_visibility TEXT,
  267. encryption TEXT,
  268. avatar TEXT,
  269. guest_access TEXT,
  270. is_federatable BOOLEAN,
  271. topic TEXT
  272. , room_type TEXT);
  273. CREATE UNIQUE INDEX room_stats_state_room ON room_stats_state(room_id);
  274. CREATE TABLE IF NOT EXISTS "user_filters" ( user_id TEXT NOT NULL, filter_id BIGINT NOT NULL, filter_json BYTEA NOT NULL );
  275. CREATE UNIQUE INDEX user_filters_unique ON "user_filters" (user_id, filter_id);
  276. CREATE TABLE user_external_ids (
  277. auth_provider TEXT NOT NULL,
  278. external_id TEXT NOT NULL,
  279. user_id TEXT NOT NULL,
  280. UNIQUE (auth_provider, external_id)
  281. );
  282. CREATE INDEX users_in_public_rooms_r_idx ON users_in_public_rooms(room_id);
  283. CREATE TABLE device_lists_remote_resync (
  284. user_id TEXT NOT NULL,
  285. added_ts BIGINT NOT NULL
  286. );
  287. CREATE UNIQUE INDEX device_lists_remote_resync_idx ON device_lists_remote_resync (user_id);
  288. CREATE INDEX device_lists_remote_resync_ts_idx ON device_lists_remote_resync (added_ts);
  289. CREATE TABLE local_current_membership (
  290. room_id TEXT NOT NULL,
  291. user_id TEXT NOT NULL,
  292. event_id TEXT NOT NULL,
  293. membership TEXT NOT NULL
  294. );
  295. CREATE UNIQUE INDEX local_current_membership_idx ON local_current_membership(user_id, room_id);
  296. CREATE INDEX local_current_membership_room_idx ON local_current_membership(room_id);
  297. CREATE TABLE ui_auth_sessions(
  298. session_id TEXT NOT NULL, -- The session ID passed to the client.
  299. creation_time BIGINT NOT NULL, -- The time this session was created (epoch time in milliseconds).
  300. serverdict TEXT NOT NULL, -- A JSON dictionary of arbitrary data added by Synapse.
  301. clientdict TEXT NOT NULL, -- A JSON dictionary of arbitrary data from the client.
  302. uri TEXT NOT NULL, -- The URI the UI authentication session is using.
  303. method TEXT NOT NULL, -- The HTTP method the UI authentication session is using.
  304. -- The clientdict, uri, and method make up an tuple that must be immutable
  305. -- throughout the lifetime of the UI Auth session.
  306. description TEXT NOT NULL, -- A human readable description of the operation which caused the UI Auth flow to occur.
  307. UNIQUE (session_id)
  308. );
  309. CREATE TABLE ui_auth_sessions_credentials(
  310. session_id TEXT NOT NULL, -- The corresponding UI Auth session.
  311. stage_type TEXT NOT NULL, -- The stage type.
  312. result TEXT NOT NULL, -- The result of the stage verification, stored as JSON.
  313. UNIQUE (session_id, stage_type),
  314. FOREIGN KEY (session_id)
  315. REFERENCES ui_auth_sessions (session_id)
  316. );
  317. CREATE TABLE IF NOT EXISTS "device_lists_outbound_last_success" ( destination TEXT NOT NULL, user_id TEXT NOT NULL, stream_id BIGINT NOT NULL );
  318. CREATE UNIQUE INDEX device_lists_outbound_last_success_unique_idx ON "device_lists_outbound_last_success" (destination, user_id);
  319. CREATE TABLE IF NOT EXISTS "local_media_repository_thumbnails" ( media_id TEXT, thumbnail_width INTEGER, thumbnail_height INTEGER, thumbnail_type TEXT, thumbnail_method TEXT, thumbnail_length INTEGER, UNIQUE ( media_id, thumbnail_width, thumbnail_height, thumbnail_type, thumbnail_method ) );
  320. CREATE INDEX local_media_repository_thumbnails_media_id ON local_media_repository_thumbnails (media_id);
  321. CREATE TABLE IF NOT EXISTS "remote_media_cache_thumbnails" ( media_origin TEXT, media_id TEXT, thumbnail_width INTEGER, thumbnail_height INTEGER, thumbnail_method TEXT, thumbnail_type TEXT, thumbnail_length INTEGER, filesystem_id TEXT, UNIQUE ( media_origin, media_id, thumbnail_width, thumbnail_height, thumbnail_type, thumbnail_method ) );
  322. CREATE TABLE ui_auth_sessions_ips(
  323. session_id TEXT NOT NULL,
  324. ip TEXT NOT NULL,
  325. user_agent TEXT NOT NULL,
  326. UNIQUE (session_id, ip, user_agent),
  327. FOREIGN KEY (session_id)
  328. REFERENCES ui_auth_sessions (session_id)
  329. );
  330. CREATE UNIQUE INDEX federation_stream_position_instance ON federation_stream_position(type, instance_name);
  331. CREATE TABLE dehydrated_devices(
  332. user_id TEXT NOT NULL PRIMARY KEY,
  333. device_id TEXT NOT NULL,
  334. device_data TEXT NOT NULL -- JSON-encoded client-defined data
  335. );
  336. CREATE TABLE e2e_fallback_keys_json (
  337. user_id TEXT NOT NULL, -- The user this fallback key is for.
  338. device_id TEXT NOT NULL, -- The device this fallback key is for.
  339. algorithm TEXT NOT NULL, -- Which algorithm this fallback key is for.
  340. key_id TEXT NOT NULL, -- An id for suppressing duplicate uploads.
  341. key_json TEXT NOT NULL, -- The key as a JSON blob.
  342. used BOOLEAN NOT NULL DEFAULT FALSE, -- Whether the key has been used or not.
  343. CONSTRAINT e2e_fallback_keys_json_uniqueness UNIQUE (user_id, device_id, algorithm)
  344. );
  345. CREATE TABLE destination_rooms (
  346. -- the destination in question.
  347. destination TEXT NOT NULL REFERENCES destinations (destination),
  348. -- the ID of the room in question
  349. room_id TEXT NOT NULL REFERENCES rooms (room_id),
  350. -- the stream_ordering of the event
  351. stream_ordering BIGINT NOT NULL,
  352. PRIMARY KEY (destination, room_id)
  353. -- We don't declare a foreign key on stream_ordering here because that'd mean
  354. -- we'd need to either maintain an index (expensive) or do a table scan of
  355. -- destination_rooms whenever we delete an event (also potentially expensive).
  356. -- In addition to that, a foreign key on stream_ordering would be redundant
  357. -- as this row doesn't need to refer to a specific event; if the event gets
  358. -- deleted then it doesn't affect the validity of the stream_ordering here.
  359. );
  360. CREATE INDEX destination_rooms_room_id
  361. ON destination_rooms (room_id);
  362. CREATE TABLE stream_positions (
  363. stream_name TEXT NOT NULL,
  364. instance_name TEXT NOT NULL,
  365. stream_id BIGINT NOT NULL
  366. );
  367. CREATE UNIQUE INDEX stream_positions_idx ON stream_positions(stream_name, instance_name);
  368. CREATE TABLE IF NOT EXISTS "access_tokens" (
  369. id BIGINT PRIMARY KEY,
  370. user_id TEXT NOT NULL,
  371. device_id TEXT,
  372. token TEXT NOT NULL,
  373. valid_until_ms BIGINT,
  374. puppets_user_id TEXT,
  375. last_validated BIGINT, refresh_token_id BIGINT REFERENCES refresh_tokens (id) ON DELETE CASCADE, used BOOLEAN,
  376. UNIQUE(token)
  377. );
  378. CREATE INDEX access_tokens_device_id ON access_tokens (user_id, device_id);
  379. CREATE TABLE IF NOT EXISTS "event_txn_id" (
  380. event_id TEXT NOT NULL,
  381. room_id TEXT NOT NULL,
  382. user_id TEXT NOT NULL,
  383. token_id BIGINT NOT NULL,
  384. txn_id TEXT NOT NULL,
  385. inserted_ts BIGINT NOT NULL,
  386. FOREIGN KEY (event_id)
  387. REFERENCES events (event_id) ON DELETE CASCADE,
  388. FOREIGN KEY (token_id)
  389. REFERENCES access_tokens (id) ON DELETE CASCADE
  390. );
  391. CREATE UNIQUE INDEX event_txn_id_event_id ON event_txn_id(event_id);
  392. CREATE UNIQUE INDEX event_txn_id_txn_id ON event_txn_id(room_id, user_id, token_id, txn_id);
  393. CREATE INDEX event_txn_id_ts ON event_txn_id(inserted_ts);
  394. CREATE TABLE ignored_users( ignorer_user_id TEXT NOT NULL, ignored_user_id TEXT NOT NULL );
  395. CREATE UNIQUE INDEX ignored_users_uniqueness ON ignored_users (ignorer_user_id, ignored_user_id);
  396. CREATE INDEX ignored_users_ignored_user_id ON ignored_users (ignored_user_id);
  397. CREATE TABLE event_auth_chains (
  398. event_id TEXT PRIMARY KEY,
  399. chain_id BIGINT NOT NULL,
  400. sequence_number BIGINT NOT NULL
  401. );
  402. CREATE UNIQUE INDEX event_auth_chains_c_seq_index ON event_auth_chains (chain_id, sequence_number);
  403. CREATE TABLE event_auth_chain_links (
  404. origin_chain_id BIGINT NOT NULL,
  405. origin_sequence_number BIGINT NOT NULL,
  406. target_chain_id BIGINT NOT NULL,
  407. target_sequence_number BIGINT NOT NULL
  408. );
  409. CREATE INDEX event_auth_chain_links_idx ON event_auth_chain_links (origin_chain_id, target_chain_id);
  410. CREATE TABLE event_auth_chain_to_calculate (
  411. event_id TEXT PRIMARY KEY,
  412. room_id TEXT NOT NULL,
  413. type TEXT NOT NULL,
  414. state_key TEXT NOT NULL
  415. );
  416. CREATE INDEX event_auth_chain_to_calculate_rm_id ON event_auth_chain_to_calculate(room_id);
  417. CREATE TABLE users_to_send_full_presence_to(
  418. -- The user ID to send full presence to.
  419. user_id TEXT PRIMARY KEY,
  420. -- A presence stream ID token - the current presence stream token when the row was last upserted.
  421. -- If a user calls /sync and this token is part of the update they're to receive, we also include
  422. -- full user presence in the response.
  423. -- This allows multiple devices for a user to receive full presence whenever they next call /sync.
  424. presence_stream_id BIGINT,
  425. FOREIGN KEY (user_id)
  426. REFERENCES users (name)
  427. );
  428. CREATE TABLE refresh_tokens (
  429. id BIGINT PRIMARY KEY,
  430. user_id TEXT NOT NULL,
  431. device_id TEXT NOT NULL,
  432. token TEXT NOT NULL,
  433. -- When consumed, a new refresh token is generated, which is tracked by
  434. -- this foreign key
  435. next_token_id BIGINT REFERENCES refresh_tokens (id) ON DELETE CASCADE, expiry_ts BIGINT DEFAULT NULL, ultimate_session_expiry_ts BIGINT DEFAULT NULL,
  436. UNIQUE(token)
  437. );
  438. CREATE TABLE worker_locks (
  439. lock_name TEXT NOT NULL,
  440. lock_key TEXT NOT NULL,
  441. -- We write the instance name to ease manual debugging, we don't ever read
  442. -- from it.
  443. -- Note: instance names aren't guarenteed to be unique.
  444. instance_name TEXT NOT NULL,
  445. -- A random string generated each time an instance takes out a lock. Used by
  446. -- the instance to tell whether the lock is still held by it (e.g. in the
  447. -- case where the process stalls for a long time the lock may time out and
  448. -- be taken out by another instance, at which point the original instance
  449. -- can tell it no longer holds the lock as the tokens no longer match).
  450. token TEXT NOT NULL,
  451. last_renewed_ts BIGINT NOT NULL
  452. );
  453. CREATE UNIQUE INDEX worker_locks_key ON worker_locks (lock_name, lock_key);
  454. CREATE TABLE federation_inbound_events_staging (
  455. origin TEXT NOT NULL,
  456. room_id TEXT NOT NULL,
  457. event_id TEXT NOT NULL,
  458. received_ts BIGINT NOT NULL,
  459. event_json TEXT NOT NULL,
  460. internal_metadata TEXT NOT NULL
  461. );
  462. CREATE INDEX federation_inbound_events_staging_room ON federation_inbound_events_staging(room_id, received_ts);
  463. CREATE UNIQUE INDEX federation_inbound_events_staging_instance_event ON federation_inbound_events_staging(origin, event_id);
  464. CREATE TABLE insertion_event_edges(
  465. event_id TEXT NOT NULL,
  466. room_id TEXT NOT NULL,
  467. insertion_prev_event_id TEXT NOT NULL
  468. );
  469. CREATE INDEX insertion_event_edges_insertion_room_id ON insertion_event_edges(room_id);
  470. CREATE INDEX insertion_event_edges_insertion_prev_event_id ON insertion_event_edges(insertion_prev_event_id);
  471. CREATE TABLE insertion_event_extremities(
  472. event_id TEXT NOT NULL,
  473. room_id TEXT NOT NULL
  474. );
  475. CREATE UNIQUE INDEX insertion_event_extremities_event_id ON insertion_event_extremities(event_id);
  476. CREATE INDEX insertion_event_extremities_room_id ON insertion_event_extremities(room_id);
  477. CREATE TABLE registration_tokens(
  478. token TEXT NOT NULL, -- The token that can be used for authentication.
  479. uses_allowed INT, -- The total number of times this token can be used. NULL if no limit.
  480. pending INT NOT NULL, -- The number of in progress registrations using this token.
  481. completed INT NOT NULL, -- The number of times this token has been used to complete a registration.
  482. expiry_time BIGINT, -- The latest time this token will be valid (epoch time in milliseconds). NULL if token doesn't expire.
  483. UNIQUE (token)
  484. );
  485. CREATE TABLE sessions(
  486. session_type TEXT NOT NULL, -- The unique key for this type of session.
  487. session_id TEXT NOT NULL, -- The session ID passed to the client.
  488. value TEXT NOT NULL, -- A JSON dictionary to persist.
  489. expiry_time_ms BIGINT NOT NULL, -- The time this session will expire (epoch time in milliseconds).
  490. UNIQUE (session_type, session_id)
  491. );
  492. CREATE TABLE insertion_events(
  493. event_id TEXT NOT NULL,
  494. room_id TEXT NOT NULL,
  495. next_batch_id TEXT NOT NULL
  496. );
  497. CREATE UNIQUE INDEX insertion_events_event_id ON insertion_events(event_id);
  498. CREATE INDEX insertion_events_next_batch_id ON insertion_events(next_batch_id);
  499. CREATE TABLE batch_events(
  500. event_id TEXT NOT NULL,
  501. room_id TEXT NOT NULL,
  502. batch_id TEXT NOT NULL
  503. );
  504. CREATE UNIQUE INDEX batch_events_event_id ON batch_events(event_id);
  505. CREATE INDEX batch_events_batch_id ON batch_events(batch_id);
  506. CREATE INDEX insertion_event_edges_event_id ON insertion_event_edges(event_id);
  507. CREATE TABLE device_auth_providers (
  508. user_id TEXT NOT NULL,
  509. device_id TEXT NOT NULL,
  510. auth_provider_id TEXT NOT NULL,
  511. auth_provider_session_id TEXT NOT NULL
  512. );
  513. CREATE INDEX device_auth_providers_devices
  514. ON device_auth_providers (user_id, device_id);
  515. CREATE INDEX device_auth_providers_sessions
  516. ON device_auth_providers (auth_provider_id, auth_provider_session_id);
  517. CREATE INDEX refresh_tokens_next_token_id
  518. ON refresh_tokens(next_token_id)
  519. WHERE next_token_id IS NOT NULL;
  520. CREATE TABLE partial_state_rooms (
  521. room_id TEXT PRIMARY KEY,
  522. FOREIGN KEY(room_id) REFERENCES rooms(room_id)
  523. );
  524. CREATE TABLE partial_state_rooms_servers (
  525. room_id TEXT NOT NULL REFERENCES partial_state_rooms(room_id),
  526. server_name TEXT NOT NULL,
  527. UNIQUE(room_id, server_name)
  528. );
  529. CREATE TABLE partial_state_events (
  530. -- the room_id is denormalised for efficient indexing (the canonical source is `events`)
  531. room_id TEXT NOT NULL REFERENCES partial_state_rooms(room_id),
  532. event_id TEXT NOT NULL REFERENCES events(event_id),
  533. UNIQUE(event_id)
  534. );
  535. CREATE INDEX partial_state_events_room_id_idx
  536. ON partial_state_events (room_id);
  537. CREATE TRIGGER partial_state_events_bad_room_id
  538. BEFORE INSERT ON partial_state_events
  539. FOR EACH ROW
  540. BEGIN
  541. SELECT RAISE(ABORT, 'Incorrect room_id in partial_state_events')
  542. WHERE EXISTS (
  543. SELECT 1 FROM events
  544. WHERE events.event_id = NEW.event_id
  545. AND events.room_id != NEW.room_id
  546. );
  547. END;
  548. CREATE TABLE device_lists_changes_in_room (
  549. user_id TEXT NOT NULL,
  550. device_id TEXT NOT NULL,
  551. room_id TEXT NOT NULL,
  552. -- This initially matches `device_lists_stream.stream_id`. Note that we
  553. -- delete older values from `device_lists_stream`, so we can't use a foreign
  554. -- constraint here.
  555. --
  556. -- The table will contain rows with the same `stream_id` but different
  557. -- `room_id`, as for each device update we store a row per room the user is
  558. -- joined to. Therefore `(stream_id, room_id)` gives a unique index.
  559. stream_id BIGINT NOT NULL,
  560. -- We have a background process which goes through this table and converts
  561. -- entries into rows in `device_lists_outbound_pokes`. Once we have processed
  562. -- a row, we mark it as such by setting `converted_to_destinations=TRUE`.
  563. converted_to_destinations BOOLEAN NOT NULL,
  564. opentracing_context TEXT
  565. );
  566. CREATE UNIQUE INDEX device_lists_changes_in_stream_id ON device_lists_changes_in_room(stream_id, room_id);
  567. CREATE INDEX device_lists_changes_in_stream_id_unconverted ON device_lists_changes_in_room(stream_id) WHERE NOT converted_to_destinations;
  568. CREATE TABLE IF NOT EXISTS "event_edges" (
  569. event_id TEXT NOT NULL,
  570. prev_event_id TEXT NOT NULL,
  571. room_id TEXT NULL,
  572. is_state BOOL NOT NULL DEFAULT 0,
  573. FOREIGN KEY(event_id) REFERENCES events(event_id)
  574. );
  575. CREATE UNIQUE INDEX event_edges_event_id_prev_event_id_idx
  576. ON event_edges (event_id, prev_event_id);
  577. CREATE INDEX ev_edges_prev_id ON event_edges (prev_event_id);
  578. CREATE TABLE event_push_summary_last_receipt_stream_id (
  579. Lock CHAR(1) NOT NULL DEFAULT 'X' UNIQUE, -- Makes sure this table only has one row.
  580. stream_id BIGINT NOT NULL,
  581. CHECK (Lock='X')
  582. );
  583. CREATE TABLE IF NOT EXISTS "application_services_state" (
  584. as_id TEXT PRIMARY KEY NOT NULL,
  585. state VARCHAR(5),
  586. read_receipt_stream_id BIGINT,
  587. presence_stream_id BIGINT,
  588. to_device_stream_id BIGINT,
  589. device_list_stream_id BIGINT
  590. );
  591. CREATE TABLE IF NOT EXISTS "receipts_linearized" (
  592. stream_id BIGINT NOT NULL,
  593. room_id TEXT NOT NULL,
  594. receipt_type TEXT NOT NULL,
  595. user_id TEXT NOT NULL,
  596. event_id TEXT NOT NULL,
  597. thread_id TEXT,
  598. event_stream_ordering BIGINT,
  599. data TEXT NOT NULL,
  600. CONSTRAINT receipts_linearized_uniqueness UNIQUE (room_id, receipt_type, user_id),
  601. CONSTRAINT receipts_linearized_uniqueness_thread UNIQUE (room_id, receipt_type, user_id, thread_id)
  602. );
  603. CREATE TABLE IF NOT EXISTS "receipts_graph" (
  604. room_id TEXT NOT NULL,
  605. receipt_type TEXT NOT NULL,
  606. user_id TEXT NOT NULL,
  607. event_ids TEXT NOT NULL,
  608. thread_id TEXT,
  609. data TEXT NOT NULL,
  610. CONSTRAINT receipts_graph_uniqueness UNIQUE (room_id, receipt_type, user_id),
  611. CONSTRAINT receipts_graph_uniqueness_thread UNIQUE (room_id, receipt_type, user_id, thread_id)
  612. );
  613. CREATE INDEX receipts_linearized_id ON receipts_linearized( stream_id );
  614. CREATE INDEX receipts_linearized_room_stream ON receipts_linearized( room_id, stream_id );
  615. CREATE INDEX receipts_linearized_user ON receipts_linearized( user_id );
  616. CREATE INDEX redactions_have_censored_ts ON redactions (received_ts) WHERE NOT have_censored;
  617. CREATE INDEX room_memberships_user_room_forgotten ON room_memberships (user_id, room_id) WHERE forgotten = 1;
  618. CREATE INDEX users_have_local_media ON local_media_repository (user_id, created_ts) ;
  619. CREATE UNIQUE INDEX e2e_cross_signing_keys_stream_idx ON e2e_cross_signing_keys (stream_id) ;
  620. CREATE INDEX user_external_ids_user_id_idx ON user_external_ids (user_id) ;
  621. CREATE INDEX presence_stream_state_not_offline_idx ON presence_stream (state) WHERE state != 'offline';
  622. CREATE UNIQUE INDEX event_push_summary_unique_index ON event_push_summary (user_id, room_id) ;
  623. CREATE UNIQUE INDEX event_push_summary_unique_index2 ON event_push_summary (user_id, room_id, thread_id) ;
  624. CREATE UNIQUE INDEX receipts_graph_unique_index ON receipts_graph (room_id, receipt_type, user_id) WHERE thread_id IS NULL;
  625. CREATE UNIQUE INDEX receipts_linearized_unique_index ON receipts_linearized (room_id, receipt_type, user_id) WHERE thread_id IS NULL;
  626. CREATE INDEX event_push_actions_stream_highlight_index ON event_push_actions (highlight, stream_ordering) WHERE highlight=0;
  627. CREATE INDEX current_state_events_member_index ON current_state_events (state_key) WHERE type='m.room.member';
  628. CREATE INDEX event_contains_url_index ON events (room_id, topological_ordering, stream_ordering) WHERE contains_url = true AND outlier = false;
  629. CREATE INDEX event_push_actions_highlights_index ON event_push_actions (user_id, room_id, topological_ordering, stream_ordering) WHERE highlight=1;
  630. CREATE INDEX local_media_repository_url_idx ON local_media_repository (created_ts) WHERE url_cache IS NOT NULL;
  631. INSERT INTO appservice_stream_position VALUES('X',0);
  632. INSERT INTO federation_stream_position VALUES('federation',-1,'master');
  633. INSERT INTO federation_stream_position VALUES('events',-1,'master');
  634. INSERT INTO event_push_summary_stream_ordering VALUES('X',0);
  635. INSERT INTO user_directory_stream_pos VALUES('X',1);
  636. INSERT INTO stats_incremental_position VALUES('X',1);
  637. INSERT INTO event_push_summary_last_receipt_stream_id VALUES('X',0);