owpengram-server/deploy/migrations/0001_init.up.sql
2026-06-04 01:37:39 +08:00

48 lines
2.7 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 0001_init: telesrv 第一阶段持久化 schemaauth_key / user / authorization
--
-- 协议产物auth_keys与业务产物authorizations分表遵循协议/业务隔离边界。
-- MTProto auth key密钥交换产物。server 重启后据此解密客户端加密包,避免客户端重建密钥。
CREATE TABLE IF NOT EXISTS auth_keys (
auth_key_id BIGINT PRIMARY KEY, -- SHA1(auth_key) 低 64 位,小端解释为 int64
body BYTEA NOT NULL, -- 256 字节 auth key 原文
server_salt BIGINT NOT NULL, -- 密钥交换产出的初始 server salt
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- 用户第一阶段仅登录链路必须字段。access_hash 为任何 InputUser 校验所必须,不可省。
CREATE TABLE IF NOT EXISTS users (
-- 普通用户 ID 从 2026-06-01 00:00:00 Asia/Shanghai 的 Unix 秒级时间戳开始。
-- 777000 等系统兼容账号显式插入,低于该区间。
id BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1780243200) PRIMARY KEY,
access_hash BIGINT NOT NULL,
phone VARCHAR(32) NOT NULL,
first_name VARCHAR(64) NOT NULL DEFAULT '',
last_name VARCHAR(64) NOT NULL DEFAULT '',
username VARCHAR(64) NOT NULL DEFAULT '',
country_code VARCHAR(8) NOT NULL DEFAULT '',
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT users_phone_key UNIQUE (phone)
);
-- 授权auth_key ↔ user 绑定 + initConnection 带来的设备信息。
-- 这是 auth.signIn 后绑定关系与 account.getAuthorizations 的权威来源;
-- 也是 rpc.ClientInfodevice/app/layer的持久化归宿。
CREATE TABLE IF NOT EXISTS authorizations (
auth_key_id BIGINT PRIMARY KEY REFERENCES auth_keys(auth_key_id) ON DELETE CASCADE,
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
hash BIGINT NOT NULL DEFAULT 0, -- getAuthorizations 列表项 hash
layer INT NOT NULL DEFAULT 0,
device_model VARCHAR(128) NOT NULL DEFAULT '',
platform VARCHAR(64) NOT NULL DEFAULT '',
system_version VARCHAR(64) NOT NULL DEFAULT '',
api_id INT NOT NULL DEFAULT 0,
app_version VARCHAR(64) NOT NULL DEFAULT '',
ip VARCHAR(64) NOT NULL DEFAULT '',
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
active_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- 按 user 反查授权PushToUser / getAuthorizations
CREATE INDEX IF NOT EXISTS authorizations_user_id_idx ON authorizations (user_id);