--- channel-thread-members

CREATE TABLE channel_thread_members (
  id         BIGINT PRIMARY KEY,
  thread_id  BIGINT      NOT NULL REFERENCES channel_threads (id),
  user_id    BIGINT      NOT NULL REFERENCES users (id),
  inviter_id BIGINT REFERENCES users (id),
  state      VARCHAR     NOT NULL,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ
);

CREATE SEQUENCE channel_thread_members_id_seq;

CREATE UNIQUE INDEX channel_thread_members_thread_id_user_id_uniq
  ON channel_thread_members (thread_id, user_id);

-- channel-threads

ALTER TABLE channel_threads
  ADD total_members INTEGER NOT NULL DEFAULT 0;
