-- Magic-link tokens for verifying an email address. `new_email` is null for a
-- plain "verify your current address" link, or the requested address for a
-- deferred email change (applied only when the link is opened).
CREATE TABLE IF NOT EXISTS email_verifications (
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id     INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
    token_hash  TEXT NOT NULL UNIQUE,
    new_email   TEXT NULL,
    expires_at  TEXT NOT NULL,
    consumed_at TEXT NULL,
    created_at  TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now'))
);

CREATE INDEX IF NOT EXISTS idx_email_verifications_user ON email_verifications (user_id);

-- When the last verification / change email was sent to this user, for the
-- once-per-minute resend throttle.
ALTER TABLE users ADD COLUMN verification_email_sent_at TEXT NULL;
