Files
XHChat/server/internal/database/001_identity.sql
T

28 lines
985 B
SQL

CREATE TABLE users (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
username text UNIQUE NOT NULL CHECK (username ~ '^[a-z0-9_]{3,32}$'),
password_hash text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE devices (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
public_key bytea NOT NULL CHECK (octet_length(public_key) = 32),
name text NOT NULL,
platform text NOT NULL CHECK (platform IN ('web', 'windows')),
created_at timestamptz NOT NULL DEFAULT now(),
last_active_at timestamptz NOT NULL DEFAULT now(),
revoked_at timestamptz,
UNIQUE (user_id, public_key)
);
CREATE TABLE sessions (
token_hash bytea PRIMARY KEY CHECK (octet_length(token_hash) = 32),
device_id uuid NOT NULL REFERENCES devices(id) ON DELETE CASCADE,
expires_at timestamptz NOT NULL
);
CREATE INDEX sessions_device ON sessions(device_id);
CREATE INDEX sessions_expiry ON sessions(expires_at);