-- Admin flag and rotating refresh tokens. ALTER TABLE users ADD COLUMN is_admin BOOLEAN NOT NULL DEFAULT FALSE; -- Accounts that already exist: the oldest one becomes the admin. UPDATE users SET is_admin = TRUE WHERE id = (SELECT MIN(id) FROM users); -- Only a hash of the token is stored, never the token itself. CREATE TABLE refresh_tokens ( id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, token_hash TEXT NOT NULL UNIQUE, -- SHA-256 of the token, hex created_at BIGINT NOT NULL, -- unix seconds expires_at BIGINT NOT NULL, -- unix seconds revoked_at BIGINT -- unix seconds, NULL while active ); CREATE INDEX idx_refresh_tokens_user ON refresh_tokens(user_id);