Files
sorsfonalak/crates/db/migrations/0013_invitations.sql
T

44 lines
1.8 KiB
SQL
Raw Normal View History

-- Invitations to the closed site. While SIGNUP_MODE=invite, a new account
-- (Google or password) needs an open invitation for its address, or the
-- invitation link itself (then any address will do).
CREATE TABLE invitations (
id uuid PRIMARY KEY,
email text NOT NULL,
message text CHECK (char_length(message) <= 300),
locale text NOT NULL,
invited_by uuid REFERENCES users (id) ON DELETE SET NULL,
created_at timestamptz NOT NULL DEFAULT now(),
expires_at timestamptz NOT NULL,
sent_count integer NOT NULL DEFAULT 1,
last_sent_at timestamptz NOT NULL DEFAULT now(),
accepted_at timestamptz,
accepted_by uuid REFERENCES users (id) ON DELETE SET NULL,
revoked_at timestamptz
);
-- At most one open invitation per address.
CREATE UNIQUE INDEX invitations_open_email_idx ON invitations (email)
WHERE accepted_at IS NULL AND revoked_at IS NULL;
CREATE INDEX invitations_created_idx ON invitations (created_at DESC);
-- Link tokens (only their hashes). Every sending issues a new one, and the
-- earlier links keep working while the invitation is open.
CREATE TABLE invitation_tokens (
token_hash bytea PRIMARY KEY,
invitation_id uuid NOT NULL REFERENCES invitations (id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX invitation_tokens_invitation_idx ON invitation_tokens (invitation_id);
-- Addresses that asked for no more invitations (only the SHA-256 of the address).
CREATE TABLE email_optouts (
email_hash bytea PRIMARY KEY,
created_at timestamptz NOT NULL DEFAULT now()
);
-- "Tell me when it opens": people without an invitation.
CREATE TABLE waitlist (
email text PRIMARY KEY,
locale text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);