sascha@979: BEGIN;
sascha@979: 
sascha@985: CREATE SEQUENCE USERS_ID_SEQ;
sascha@985: 
sascha@979: CREATE TABLE users (
sascha@985:     id  INT  PRIMARY KEY NOT NULL,
sascha@985:     gid UUID             NOT NULL UNIQUE
sascha@979: );
sascha@979: 
sascha@985: CREATE SEQUENCE COLLECTIONS_ID_SEQ;
sascha@985: 
sascha@979: CREATE TABLE collections (
sascha@1025:     id       INT  PRIMARY KEY NOT NULL,
sascha@1025:     gid      UUID             NOT NULL UNIQUE,
sascha@1025:     user_id  INT              NOT NULL REFERENCES users(id) ON DELETE CASCADE,
sascha@1025:     name     VARCHAR(256)     NOT NULL,
sascha@1025:     creation TIMESTAMP        NOT NULL
sascha@979: );
sascha@979: 
sascha@985: CREATE SEQUENCE ARTIFACTS_ID_SEQ;
sascha@985: 
sascha@979: CREATE TABLE artifacts (
sascha@1025:     id       INT  PRIMARY KEY NOT NULL,
sascha@1025:     gid      UUID             NOT NULL UNIQUE,
sascha@1025:     state    VARCHAR(256)     NOT NULL,
sascha@1025:     creation TIMESTAMP        NOT NULL
sascha@979: );
sascha@979: 
sascha@988: CREATE SEQUENCE COLLECTION_ITEMS_ID_SEQ;
sascha@985: 
sascha@979: CREATE TABLE collection_items (
sascha@985:     id            INT PRIMARY KEY NOT NULL,
sascha@985:     collection_id INT             NOT NULL REFERENCES collections(id) ON DELETE CASCADE,
sascha@985:     artifact_id   INT             NOT NULL REFERENCES artifacts(id)   ON DELETE CASCADE
sascha@979: );
sascha@979: 
sascha@985: CREATE SEQUENCE ARTIFACT_DATA_ID_SEQ;
sascha@985: 
sascha@979: CREATE TABLE artifact_data (
sascha@985:     id          INT PRIMARY KEY NOT NULL,
sascha@985:     artifact_id INT             NOT NULL REFERENCES artifacts(id) ON DELETE CASCADE,
sascha@990:     kind        VARCHAR(256)    NOT NULL,
sascha@985:     k           VARCHAR(256)    NOT NULL,
sascha@985:     v           VARCHAR(256),   -- Maybe too short
sascha@979:     UNIQUE (artifact_id, k)
sascha@979: );
sascha@979: 
sascha@985: CREATE SEQUENCE OUTS_ID_SEQ;
sascha@985: 
sascha@981: CREATE TABLE outs (
sascha@985:     id          INT PRIMARY KEY NOT NULL,
sascha@985:     artifact_id INT             NOT NULL REFERENCES artifacts(id) ON DELETE CASCADE,
sascha@985:     name        VARCHAR(256)    NOT NULL,
sascha@981:     description VARCHAR(256),
sascha@981:     out_type    VARCHAR(256)
sascha@981: );
sascha@981: 
sascha@985: CREATE SEQUENCE FACETS_ID_SEQ;
sascha@985: 
sascha@979: CREATE TABLE facets (
sascha@985:     id          INT PRIMARY KEY NOT NULL,
sascha@992:     out_id      INT             NOT NULL REFERENCES outs(id) ON DELETE CASCADE,
sascha@985:     name        VARCHAR(256)    NOT NULL,
sascha@992:     num         INT             NOT NULL,
sascha@992:     state       VARCHAR(256)    NOT NULL,
sascha@979:     description VARCHAR(256),
sascha@993:     UNIQUE (out_id, num, name)
sascha@979: );
sascha@979: 
sascha@1044: CREATE VIEW master_artifacts AS
sascha@1044:     SELECT a2.id             AS id,
sascha@1044:            a2.gid            AS gid,
sascha@1044:            a2.state          AS state,
sascha@1044:            a2.creation       AS creation,
sascha@1044:            ci2.collection_id AS collection_id
sascha@1044:     FROM   collection_items ci2 
sascha@1044:            JOIN artifacts a2 
sascha@1044:              ON ci2.artifact_id = a2.id 
sascha@1044:            JOIN (SELECT ci.collection_id AS c_id, 
sascha@1044:                         MIN(a.creation)  AS oldest_a 
sascha@1044:                  FROM   collection_items ci 
sascha@1044:                         JOIN artifacts a 
sascha@1044:                           ON ci.artifact_id = a.id 
sascha@1044:                  GROUP  BY ci.collection_id) o 
sascha@1044:              ON o.c_id = ci2.collection_id 
sascha@1044:     WHERE  a2.creation = o.oldest_a;
sascha@1044: 
sascha@1044: -- DROP VIEW master_artifacts;
sascha@985: -- DROP SEQUENCE USERS_ID_SEQ;
sascha@985: -- DROP SEQUENCE COLLECTIONS_ID_SEQ;
sascha@985: -- DROP SEQUENCE ARTIFACTS_ID_SEQ;
sascha@988: -- DROP SEQUENCE COLLECTION_ITEMS_ID_SEQ;
sascha@985: -- DROP SEQUENCE ARTIFACT_DATA_ID_SEQ;
sascha@985: -- DROP SEQUENCE OUTS_ID_SEQ;
sascha@985: -- DROP SEQUENCE FACETS_ID_SEQ;
sascha@980: -- DROP TABLE facets;
sascha@981: -- DROP TABLE outs;
sascha@980: -- DROP TABLE artifact_data;
sascha@980: -- DROP TABLE collection_items;
sascha@980: -- DROP TABLE collections;
sascha@980: -- DROP TABLE artifacts;
sascha@980: -- DROP TABLE users;
sascha@980: 
sascha@979: COMMIT;