-- 0005_system_login_messages: official system account and first message persistence. -- -- 777000 官方账号与登录消息推送;表结构按 telesrv domain/store 边界建模。 ALTER TABLE users ADD COLUMN IF NOT EXISTS verified BOOLEAN NOT NULL DEFAULT false, ADD COLUMN IF NOT EXISTS support BOOLEAN NOT NULL DEFAULT false; INSERT INTO users (id, access_hash, phone, first_name, last_name, username, country_code, verified, support) VALUES (777000, 6599886787491911851, '42777', 'Telegram', '', 'telegram', '', true, true) ON CONFLICT (id) DO UPDATE SET access_hash = EXCLUDED.access_hash, phone = EXCLUDED.phone, first_name = EXCLUDED.first_name, last_name = EXCLUDED.last_name, username = EXCLUDED.username, verified = EXCLUDED.verified, support = EXCLUDED.support, updated_at = now(); ALTER TABLE dialogs ADD COLUMN IF NOT EXISTS top_message_date INT NOT NULL DEFAULT 0; CREATE INDEX IF NOT EXISTS dialogs_user_top_message_idx ON dialogs (user_id, pinned DESC, top_message_date DESC, top_message_id DESC, peer_id DESC); CREATE TABLE IF NOT EXISTS messages ( id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, owner_user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, peer_type VARCHAR(16) NOT NULL, peer_id BIGINT NOT NULL, from_user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE RESTRICT, message_date INT NOT NULL, outgoing BOOLEAN NOT NULL DEFAULT false, body TEXT NOT NULL DEFAULT '', entities JSONB NOT NULL DEFAULT '[]'::jsonb, created_at TIMESTAMPTZ NOT NULL DEFAULT now(), CONSTRAINT messages_peer_type_check CHECK (peer_type IN ('user')) ); CREATE INDEX IF NOT EXISTS messages_owner_dialog_idx ON messages (owner_user_id, peer_type, peer_id, id DESC); CREATE INDEX IF NOT EXISTS messages_owner_date_idx ON messages (owner_user_id, message_date DESC, id DESC);