customer/migrations/0001_accounts.sql

28 lines
931 B
SQL

CREATE TABLE organizations (
id uuid PRIMARY KEY,
name text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE users (
id uuid PRIMARY KEY,
email text NOT NULL,
password_hash text NOT NULL,
role text NOT NULL CHECK (role IN ('buyer', 'org_admin', 'operator')),
org_id uuid REFERENCES organizations (id) ON DELETE RESTRICT,
created_at timestamptz NOT NULL DEFAULT now(),
CHECK ((role = 'buyer') = (org_id IS NULL))
);
CREATE UNIQUE INDEX users_email_lower_idx ON users (lower(email));
CREATE INDEX users_org_id_idx ON users (org_id);
CREATE TABLE sessions (
token_hash bytea PRIMARY KEY,
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
csrf_token text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
expires_at timestamptz NOT NULL
);
CREATE INDEX sessions_user_id_idx ON sessions (user_id);
CREATE INDEX sessions_expires_at_idx ON sessions (expires_at);