-- Users

CREATE TABLE users (
  id                  BIGSERIAL PRIMARY KEY,
  org_id              BIGINT      NOT NULL REFERENCES orgs (id),
  name                VARCHAR     NOT NULL,
  email               VARCHAR,
  admin               BOOLEAN     NOT NULL DEFAULT FALSE,
  active              BOOLEAN     NOT NULL DEFAULT FALSE,

  password_hash       VARCHAR,
  password_changed_at TIMESTAMPTZ,

  created_at          TIMESTAMPTZ NOT NULL DEFAULT now(),
  logged_in_at        TIMESTAMPTZ
);

CREATE INDEX users_org_id_ix
  ON users (org_id);
CREATE UNIQUE INDEX users_email_uniq
  ON users (lower(email))
  WHERE email IS NOT NULL;
