store: refactor access hash and session tables

* Move sessions to user_login metadata, as that data rarely changes after login.
* Merge user and channel access hashes. Those IDs don't conflict.
* Split usernames into a new table to allow better `ON CONFLICT` updates
  (when a username moves to another entity, we want the old row to be replaced).
  Usernames also don't need to be scoped to a login.
This commit is contained in:
Tulir Asokan
2024-08-22 16:21:24 +03:00
parent e611c87342
commit b25c09fc53
14 changed files with 129 additions and 195 deletions
+15 -24
View File
@@ -1,12 +1,7 @@
-- v0 -> v3: Latest revision
CREATE TABLE telegram_session (
user_id BIGINT PRIMARY KEY,
session_data BYTEA NOT NULL
);
-- v0 -> v1: Latest revision
CREATE TABLE telegram_user_state (
user_id BIGINT PRIMARY KEY,
user_id BIGINT NOT NULL PRIMARY KEY,
pts BIGINT NOT NULL,
qts BIGINT NOT NULL,
date BIGINT NOT NULL,
@@ -14,39 +9,35 @@ CREATE TABLE telegram_user_state (
);
CREATE TABLE telegram_channel_state (
user_id BIGINT,
channel_id BIGINT,
user_id BIGINT NOT NULL,
channel_id BIGINT NOT NULL,
pts BIGINT NOT NULL,
PRIMARY KEY (user_id, channel_id)
);
CREATE INDEX idx_telegram_channel_state_user_id ON telegram_channel_state (user_id);
CREATE INDEX telegram_channel_state_user_id_idx ON telegram_channel_state (user_id);
CREATE TABLE telegram_channel_access_hashes (
user_id BIGINT,
channel_id BIGINT,
CREATE TABLE telegram_access_hash (
user_id BIGINT NOT NULL,
entity_id BIGINT NOT NULL,
access_hash BIGINT NOT NULL,
PRIMARY KEY (user_id, channel_id)
PRIMARY KEY (user_id, entity_id)
);
CREATE TABLE telegram_user_metadata (
receiver_id BIGINT,
user_id BIGINT,
CREATE TABLE telegram_username (
username TEXT NOT NULL,
entity_id BIGINT NOT NULL,
access_hash BIGINT NOT NULL,
username TEXT,
PRIMARY KEY (receiver_id, user_id)
PRIMARY KEY (username)
);
CREATE INDEX telegram_username_entity_idx ON telegram_username (entity_id);
CREATE TABLE telegram_file (
id TEXT PRIMARY KEY,
mxc TEXT NOT NULL,
mime_type TEXT,
size BIGINT
);
-- TODO this will be unnecessary once the queries switch to reading telegram_user_metadata
CREATE INDEX idx_ghost_username ON ghost ((metadata->>'username'));