--- channel-members


CREATE TABLE channel_members (
  id         BIGINT PRIMARY KEY,
  channel_id BIGINT      NOT NULL REFERENCES channels (id),
  is_group   BOOLEAN     NOT NULL DEFAULT FALSE,
  deleted    BOOLEAN     NOT NULL DEFAULT FALSE,
  user_id    BIGINT REFERENCES users (id),
  group_id   BIGINT REFERENCES user_groups (id),
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  deleted_at TIMESTAMPTZ
);

CREATE SEQUENCE channel_members_id_seq;

CREATE UNIQUE INDEX channel_members_user_id_uniq
  ON channel_members (user_id)
  WHERE user_id IS NOT NULL;

CREATE UNIQUE INDEX channel_members_group_id_uniq
  ON channel_members (group_id)
  WHERE group_id IS NOT NULL;
