--- push-notifications

CREATE TABLE pushes (
  id           BIGINT PRIMARY KEY,
  state        VARCHAR(16) NOT NULL,
  channel_id   BIGINT      NOT NULL REFERENCES channels (id),
  client_id    BIGINT      NOT NULL REFERENCES clients (id),
  token_id     BIGINT      NOT NULL REFERENCES push_tokens (id),
  message_id   BIGINT      NOT NULL REFERENCES chat_messages (id),
  created_at   TIMESTAMPTZ NOT NULL DEFAULT now(),
  enqueued_at  TIMESTAMPTZ,
  sent_at      TIMESTAMPTZ,
  cancelled_at TIMESTAMPTZ
);

CREATE SEQUENCE pushes_id_seq;
CREATE INDEX pushes_channel_id_state_created_at_ix
  ON pushes (channel_id, state, created_at);
CREATE INDEX pushes_client_id_state_created_at_ix
  ON pushes (client_id, state, created_at);
