--- channel-members


CREATE TABLE channel_members (
  id         BIGINT IDENTITY (1,1) PRIMARY KEY,
  channel_id BIGINT      NOT NULL REFERENCES channels (id),
  is_group   BIT     NOT NULL DEFAULT 0,
  deleted    BIT     NOT NULL DEFAULT 0,
  user_id    BIGINT REFERENCES users (id),
  group_id   BIGINT REFERENCES user_groups (id),
  created_at DATETIMEOFFSET NOT NULL DEFAULT SYSDATETIMEOFFSET(),
  deleted_at DATETIMEOFFSET
);

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;
