- change all occurrences of slices.deleted to slices.is_deleted
[plcapi.git] / planetlab4.sql
index c68aecc..136e7e1 100644 (file)
@@ -9,7 +9,7 @@
 --
 -- Copyright (C) 2006 The Trustees of Princeton University
 --
--- $Id: planetlab4.sql,v 1.31 2006/11/10 17:00:21 thierry Exp $
+-- $Id: planetlab4.sql,v 1.40 2006/11/17 10:43:17 thierry Exp $
 --
 
 --------------------------------------------------------------------------------
@@ -25,6 +25,17 @@ CREATE AGGREGATE array_accum (
     initcond = '{}'
 );
 
+--------------------------------------------------------------------------------
+-- Version
+--------------------------------------------------------------------------------
+
+--version
+CREATE TABLE plc_db_version (
+       version integer NOT NULL 
+) WITH OIDS;
+
+INSERT INTO plc_db_version (version) VALUES (4);
+
 --------------------------------------------------------------------------------
 -- Accounts
 --------------------------------------------------------------------------------
@@ -297,13 +308,12 @@ GROUP BY site_id;
 
 -- Nodes - peers relationship
 CREATE TABLE peer_node (
-    peer_id integer REFERENCES peers NOT NULL, -- peer primary key
-    node_id integer REFERENCES nodes NOT NULL, -- node primary key
-    foreign_id integer NOT NULL,
-    PRIMARY KEY (peer_id, node_id)
+    peer_id integer REFERENCES peers NOT NULL, -- Peer identifier
+    node_id integer REFERENCES nodes NOT NULL, -- (Local) node identifier
+    PRIMARY KEY (peer_id, node_id),
+    UNIQUE (node_id) -- Nodes can only be at one peer
 ) WITH OIDS;
 CREATE INDEX peer_node_peer_id_idx ON peer_node (peer_id);
-CREATE INDEX peer_node_node_id_idx ON peer_node (node_id);
 
 -- Nodes at each peer
 CREATE VIEW peer_nodes AS
@@ -530,7 +540,8 @@ INSERT INTO slice_instantiations (instantiation) VALUES ('delegated'); -- Manual
 -- Slices
 CREATE TABLE slices (
     slice_id serial PRIMARY KEY, -- Slice identifier
-    site_id integer REFERENCES sites NOT NULL, -- Site identifier
+-- xxx temporarily remove the NOT NULL constraint
+    site_id integer REFERENCES sites, -- Site identifier
     name text NOT NULL, -- Slice name
     instantiation text REFERENCES slice_instantiations NOT NULL DEFAULT 'plc-instantiated', -- Slice state, e.g. plc-instantiated
     url text, -- Project URL
@@ -538,7 +549,8 @@ CREATE TABLE slices (
 
     max_nodes integer NOT NULL DEFAULT 100, -- Maximum number of nodes that can be assigned to this slice
 
-    creator_person_id integer REFERENCES persons NOT NULL, -- Creator
+-- xxx temporarily remove the NOT NULL constraint
+    creator_person_id integer REFERENCES persons, -- Creator
     created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, -- Creation date
     expires timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP + '2 weeks', -- Expiration date
 
@@ -579,17 +591,17 @@ CREATE VIEW site_slices AS
 SELECT site_id,
 array_accum(slice_id) AS slice_ids
 FROM slices
+WHERE is_deleted is false
 GROUP BY site_id;
 
 -- Slices - peer relationship
 CREATE TABLE peer_slice (
     peer_id integer REFERENCES peers NOT NULL, -- peer primary key
     slice_id integer REFERENCES slices NOT NULL, -- node primary key
-    foreign_id integer NOT NULL,
     PRIMARY KEY (peer_id, slice_id)
 ) WITH OIDS;
 CREATE INDEX peer_slice_peer_id_idx ON peer_slice (peer_id);
-CREATE INDEX peer_slice_slice_id_idx ON peer_slice (node_id);
+CREATE INDEX peer_slice_slice_id_idx ON peer_slice (slice_id);
 
 CREATE VIEW peer_slices AS
 SELECT peer_id,
@@ -682,6 +694,7 @@ CREATE TABLE node_session (
 
 CREATE TABLE messages (
     message_id text PRIMARY KEY, -- Message name
+    subject text, -- Message summary
     template text, -- Message template
     enabled bool NOT NULL DEFAULT true -- Whether message is enabled
 ) WITH OIDS;
@@ -712,6 +725,7 @@ INSERT INTO object_types (object_type) VALUES ('BootState');
 INSERT INTO object_types (object_type) VALUES ('ConfFile');
 INSERT INTO object_types (object_type) VALUES ('KeyType');
 INSERT INTO object_types (object_type) VALUES ('Key');
+INSERT INTO object_types (object_type) VALUES ('Message');
 INSERT INTO object_types (object_type) VALUES ('NetworkMethod');
 INSERT INTO object_types (object_type) VALUES ('NetworkType');
 INSERT INTO object_types (object_type) VALUES ('Network');
@@ -726,6 +740,7 @@ INSERT INTO object_types (object_type) VALUES ('Site');
 INSERT INTO object_types (object_type) VALUES ('SliceAttributeType');
 INSERT INTO object_types (object_type) VALUES ('SliceAttribute');
 INSERT INTO object_types (object_type) VALUES ('Slice');
+INSERT INTO object_types (object_type) VALUES ('SliceInstantiation');
 INSERT INTO object_types (object_type) VALUES ('Unknown');
 
 -- Events
@@ -969,7 +984,9 @@ LEFT JOIN peer_slice USING (slice_id)
 LEFT JOIN slice_nodes USING (slice_id)
 LEFT JOIN slice_persons USING (slice_id)
 LEFT JOIN slice_attributes USING (slice_id)
-WHERE peer_slice.peer_id IS NULL;
+WHERE peer_slice.peer_id IS NULL
+AND slices.site_id IS NOT NULL
+AND slices.creator_person_id IS NOT NULL;
 
 CREATE VIEW view_foreign_slices AS
 SELECT
@@ -982,9 +999,11 @@ slices.description,
 slices.max_nodes,
 slices.is_deleted,
 CAST(date_part('epoch', slices.created) AS bigint) AS created,
-CAST(date_part('epoch', slices.expires) AS bigint) AS expires
+CAST(date_part('epoch', slices.expires) AS bigint) AS expires,
+COALESCE(slice_nodes.node_ids, '{}') AS node_ids
 FROM slices
 LEFT JOIN peer_slice USING (slice_id)
+LEFT JOIN slice_nodes USING (slice_id)
 WHERE peer_slice.peer_id IS NOT NULL;
 
 --