DROP TABLE IF EXISTS message;

-- DROP SEQUENCE IF EXISTS message_seq;
-- CREATE SEQUENCE message_seq;

-- CREATE TABLE IF NOT EXISTS message (
CREATE TABLE message (
  "message_id" int PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, -- SERIAL PRIMARY KEY
  "type" varchar(20) NOT NULL DEFAULT 'message',
  "data" text NOT NULL,
  "meta" text NOT NULL,
  "status" smallint NOT NULL DEFAULT 0, -- unread = 0, read = 1
  "created_at" timestamp(0) NOT NULL DEFAULT now(),
  "updated_at" timestamp(0) NOT NULL DEFAULT now()
  -- PRIMARY KEY("message_id")
);


DROP INDEX IF EXISTS "message_type_status_date";

CREATE INDEX "message_type_status_date" ON message ("type","status","created_at");
-- SELECT setval('message_message_id_seq', 0, true); -- last inserted id by sample data
