-- -*- sql -*-

CREATE LANGUAGE plpgsql;

-- table for list of sMAP sources we should be subscribed to
CREATE TABLE subscription (
       id INT PRIMARY KEY,
       uuid VARCHAR(36),
       url VARCHAR(512) NOT NULL,
       resource VARCHAR(512) NOT NULL DEFAULT '/+',
       key VARCHAR(36),
       public BOOLEAN DEFAULT true,
       description VARCHAR(256),
       owner_id integer DEFAULT 1
);
CREATE UNIQUE INDEX subscription_key_ind ON subscription(key);
CREATE SEQUENCE subscription_id_seq;
ALTER TABLE subscription
  ALTER COLUMN id
     SET DEFAULT NEXTVAL('subscription_id_seq');

-- list of streams associated with a sMAP source
CREATE TABLE stream (
       id INT PRIMARY KEY,
       subscription_id INT NOT NULL,
       uuid VARCHAR(36) UNIQUE,

       FOREIGN KEY (subscription_id) REFERENCES subscription(id)
         ON DELETE CASCADE
);
CREATE UNIQUE INDEX uuid_ind ON stream(uuid);
CREATE INDEX subscription_int ON stream(subscription_id);
CREATE SEQUENCE stream_id_seq;
ALTER TABLE stream
  ALTER COLUMN id
    SET DEFAULT NEXTVAL('stream_id_seq');

-- table for sMAP stream metadata
CREATE TABLE metadata2 (
       id INT PRIMARY KEY,
       stream_id INT,

       tagname VARCHAR(64) NOT NULL,
       tagval TEXT NOT NULL,

       FOREIGN KEY (stream_id) REFERENCES stream(id)
         ON DELETE CASCADE
);
CREATE INDEX stream_id_ind ON metadata2(stream_id);
CREATE UNIQUE INDEX uuid_anchor_ind ON metadata2(stream_id, tagname);
CREATE SEQUENCE metadata2_id_seq;
ALTER TABLE metadata2
  ALTER COLUMN id
    SET DEFAULT NEXTVAL('metadata2_id_seq');


