--- channel-events

CREATE TABLE channel_events (
  id          BIGINT PRIMARY KEY,
  type        VARCHAR     NOT NULL,
  channel_id  BIGINT      NOT NULL REFERENCES channels (id),
  member_id   BIGINT REFERENCES channel_members (id),

  actor_id    BIGINT      NOT NULL REFERENCES users (id),
  actor_ip    VARCHAR,
  actor_ua_id BIGINT      NOT NULL REFERENCES user_agents (id),

  created_at  TIMESTAMPTZ NOT NULL  DEFAULT now()
);

CREATE SEQUENCE channel_events_id_seq;

CREATE INDEX channel_events_channel_id_ix
  ON channel_events (channel_id);


ALTER TABLE channels
  DROP COLUMN event_id;

ALTER TABLE channels
  ADD event_id BIGINT REFERENCES channel_events (id);
ALTER TABLE channels
  ADD total_members INTEGER NOT NULL DEFAULT 0;
