remove extrainfo field from db. should use 'tags' instead.
[plcapi.git] / planetlab5.sql
1 --
2 -- PlanetLab Central database schema
3 -- Version 5, PostgreSQL
4 --
5 -- Aaron Klingaman <alk@cs.princeton.edu>
6 -- Reid Moran <rmoran@cs.princeton.edu>
7 -- Mark Huang <mlhuang@cs.princeton.edu>
8 -- Tony Mack <tmack@cs.princeton.edu>
9 -- Thierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
10 --
11 -- Copyright (C) 2006 The Trustees of Princeton University
12 --
13 -- NOTE: this file was first created for version 4.3, the filename might be confusing
14 --
15 -- $Id$
16 --
17
18 SET client_encoding = 'UNICODE';
19
20 --------------------------------------------------------------------------------
21 -- Version
22 --------------------------------------------------------------------------------
23
24 -- Database version
25 CREATE TABLE plc_db_version (
26     version integer NOT NULL,
27     subversion integer NOT NULL DEFAULT 0
28 ) WITH OIDS;
29
30 INSERT INTO plc_db_version (version, subversion) VALUES (5, 0);
31
32 --------------------------------------------------------------------------------
33 -- Aggregates and store procedures
34 --------------------------------------------------------------------------------
35
36 -- Like MySQL GROUP_CONCAT(), this function aggregates values into a
37 -- PostgreSQL array.
38 CREATE AGGREGATE array_accum (
39     sfunc = array_append,
40     basetype = anyelement,
41     stype = anyarray,
42     initcond = '{}'
43 );
44
45 --------------------------------------------------------------------------------
46 -- Accounts
47 --------------------------------------------------------------------------------
48
49 -- Accounts
50 CREATE TABLE persons (
51     -- Mandatory
52     person_id serial PRIMARY KEY,                       -- Account identifier
53     email text NOT NULL,                                -- E-mail address
54     first_name text NOT NULL,                           -- First name
55     last_name text NOT NULL,                            -- Last name
56     deleted boolean NOT NULL DEFAULT false,             -- Has been deleted
57     enabled boolean NOT NULL DEFAULT false,             -- Has been disabled
58
59     password text NOT NULL DEFAULT 'nopass',            -- Password (md5crypted)
60     verification_key text,                              -- Reset password key
61     verification_expires timestamp without time zone,
62
63     -- Optional
64     title text,                                         -- Honorific
65     phone text,                                         -- Telephone number
66     url text,                                           -- Home page
67     bio text,                                           -- Biography
68
69     -- Timestamps
70     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
71     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
72 ) WITH OIDS;
73 CREATE INDEX persons_email_idx ON persons (email);
74
75 --------------------------------------------------------------------------------
76 -- Sites
77 --------------------------------------------------------------------------------
78
79 -- Sites
80 CREATE TABLE sites (
81     -- Mandatory
82     site_id serial PRIMARY KEY,                         -- Site identifier
83     login_base text NOT NULL,                           -- Site slice prefix
84     name text NOT NULL,                                 -- Site name
85     abbreviated_name text NOT NULL,                     -- Site abbreviated name
86     enabled boolean NOT NULL Default true,              -- Is this site enabled
87     deleted boolean NOT NULL DEFAULT false,             -- Has been deleted
88     is_public boolean NOT NULL DEFAULT true,            -- Shows up in public lists
89     max_slices integer NOT NULL DEFAULT 0,              -- Maximum number of slices
90     max_slivers integer NOT NULL DEFAULT 1000,          -- Maximum number of instantiated slivers
91
92     -- Optional
93     latitude real,
94     longitude real,
95     url text,
96     ext_consortium_id integer,                          -- external consortium id
97
98     -- Timestamps
99     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
100     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
101 ) WITH OIDS;
102 CREATE INDEX sites_login_base_idx ON sites (login_base);
103
104 -- Account site membership
105 CREATE TABLE person_site (
106     person_id integer REFERENCES persons NOT NULL,      -- Account identifier
107     site_id integer REFERENCES sites NOT NULL,          -- Site identifier
108     is_primary boolean NOT NULL DEFAULT false,          -- Is the primary site for this account
109     PRIMARY KEY (person_id, site_id)
110 );
111 CREATE INDEX person_site_person_id_idx ON person_site (person_id);
112 CREATE INDEX person_site_site_id_idx ON person_site (site_id);
113
114 -- Ordered by primary site first
115 CREATE OR REPLACE VIEW person_site_ordered AS
116 SELECT person_id, site_id
117 FROM person_site
118 ORDER BY is_primary DESC;
119
120 -- Sites that each person is a member of
121 CREATE OR REPLACE VIEW person_sites AS
122 SELECT person_id,
123 array_accum(site_id) AS site_ids
124 FROM person_site_ordered
125 GROUP BY person_id;
126
127 -- Accounts at each site
128 CREATE OR REPLACE VIEW site_persons AS
129 SELECT site_id,
130 array_accum(person_id) AS person_ids
131 FROM person_site
132 GROUP BY site_id;
133
134 --------------------------------------------------------------------------------
135 -- Mailing Addresses
136 --------------------------------------------------------------------------------
137
138 CREATE TABLE address_types (
139     address_type_id serial PRIMARY KEY,                 -- Address type identifier
140     name text UNIQUE NOT NULL,                          -- Address type
141     description text                                    -- Address type description
142 ) WITH OIDS;
143
144 -- Multi-rows insertion "insert .. values (row1), (row2)" is not supported by pgsql-8.1
145 -- 'Billing' Used to be 'Site'
146 INSERT INTO address_types (name) VALUES ('Personal');
147 INSERT INTO address_types (name) VALUES ('Shipping');
148 INSERT INTO address_types (name) VALUES ('Billing');
149
150 -- Mailing addresses
151 CREATE TABLE addresses (
152     address_id serial PRIMARY KEY,                      -- Address identifier
153     line1 text NOT NULL,                                -- Address line 1
154     line2 text,                                         -- Address line 2
155     line3 text,                                         -- Address line 3
156     city text NOT NULL,                                 -- City
157     state text NOT NULL,                                -- State or province
158     postalcode text NOT NULL,                           -- Postal code
159     country text NOT NULL                               -- Country
160 ) WITH OIDS;
161
162 -- Each mailing address can be one of several types
163 CREATE TABLE address_address_type (
164     address_id integer REFERENCES addresses NOT NULL,           -- Address identifier
165     address_type_id integer REFERENCES address_types NOT NULL,  -- Address type
166     PRIMARY KEY (address_id, address_type_id)
167 ) WITH OIDS;
168 CREATE INDEX address_address_type_address_id_idx ON address_address_type (address_id);
169 CREATE INDEX address_address_type_address_type_id_idx ON address_address_type (address_type_id);
170
171 CREATE OR REPLACE VIEW address_address_types AS
172 SELECT address_id,
173 array_accum(address_type_id) AS address_type_ids,
174 array_accum(address_types.name) AS address_types
175 FROM address_address_type
176 LEFT JOIN address_types USING (address_type_id)
177 GROUP BY address_id;
178
179 CREATE TABLE site_address (
180     site_id integer REFERENCES sites NOT NULL,          -- Site identifier
181     address_id integer REFERENCES addresses NOT NULL,   -- Address identifier
182     PRIMARY KEY (site_id, address_id)
183 ) WITH OIDS;
184 CREATE INDEX site_address_site_id_idx ON site_address (site_id);
185 CREATE INDEX site_address_address_id_idx ON site_address (address_id);
186
187 CREATE OR REPLACE VIEW site_addresses AS
188 SELECT site_id,
189 array_accum(address_id) AS address_ids
190 FROM site_address
191 GROUP BY site_id;
192
193 --------------------------------------------------------------------------------
194 -- Authentication Keys
195 --------------------------------------------------------------------------------
196
197 -- Valid key types
198 CREATE TABLE key_types (
199     key_type text PRIMARY KEY                           -- Key type
200 ) WITH OIDS;
201 INSERT INTO key_types (key_type) VALUES ('ssh');
202
203 -- Authentication keys
204 CREATE TABLE keys (
205     key_id serial PRIMARY KEY,                          -- Key identifier
206     key_type text REFERENCES key_types NOT NULL,        -- Key type
207     key text NOT NULL, -- Key material
208     is_blacklisted boolean NOT NULL DEFAULT false       -- Has been blacklisted
209 ) WITH OIDS;
210
211 -- Account authentication key(s)
212 CREATE TABLE person_key (
213     key_id integer REFERENCES keys PRIMARY KEY,         -- Key identifier
214     person_id integer REFERENCES persons NOT NULL       -- Account identifier
215 ) WITH OIDS;
216 CREATE INDEX person_key_person_id_idx ON person_key (person_id);
217
218 CREATE OR REPLACE VIEW person_keys AS
219 SELECT person_id,
220 array_accum(key_id) AS key_ids
221 FROM person_key
222 GROUP BY person_id;
223
224 --------------------------------------------------------------------------------
225 -- Account roles
226 --------------------------------------------------------------------------------
227
228 -- Valid account roles
229 CREATE TABLE roles (
230     role_id integer PRIMARY KEY,                        -- Role identifier
231     name text UNIQUE NOT NULL                           -- Role symbolic name
232 ) WITH OIDS;
233 INSERT INTO roles (role_id, name) VALUES (10, 'admin');
234 INSERT INTO roles (role_id, name) VALUES (20, 'pi');
235 INSERT INTO roles (role_id, name) VALUES (30, 'user');
236 INSERT INTO roles (role_id, name) VALUES (40, 'tech');
237
238 CREATE TABLE person_role (
239     person_id integer REFERENCES persons NOT NULL,      -- Account identifier
240     role_id integer REFERENCES roles NOT NULL,          -- Role identifier
241     PRIMARY KEY (person_id, role_id)
242 ) WITH OIDS;
243 CREATE INDEX person_role_person_id_idx ON person_role (person_id);
244
245 -- Account roles
246 CREATE OR REPLACE VIEW person_roles AS
247 SELECT person_id,
248 array_accum(role_id) AS role_ids,
249 array_accum(roles.name) AS roles
250 FROM person_role
251 LEFT JOIN roles USING (role_id)
252 GROUP BY person_id;
253
254 --------------------------------------------------------------------------------
255 -- Nodes
256 --------------------------------------------------------------------------------
257
258 -- Valid node boot states (Nodes.py expect max length to be 20)
259 CREATE TABLE boot_states (
260     boot_state text PRIMARY KEY
261 ) WITH OIDS;
262 INSERT INTO boot_states (boot_state) VALUES ('boot');
263 INSERT INTO boot_states (boot_state) VALUES ('safeboot');
264 INSERT INTO boot_states (boot_state) VALUES ('reinstall');
265 INSERT INTO boot_states (boot_state) VALUES ('disabled');
266
267 CREATE TABLE run_levels  (
268     run_level text PRIMARY KEY
269 ) WITH OIDS;
270 INSERT INTO run_levels  (run_level) VALUES ('boot');
271 INSERT INTO run_levels  (run_level) VALUES ('safeboot');
272 INSERT INTO run_levels  (run_level) VALUES ('failboot');
273 INSERT INTO run_levels  (run_level) VALUES ('reinstall');
274
275 -- Known node types (Nodes.py expect max length to be 20)
276 CREATE TABLE node_types (
277     node_type text PRIMARY KEY
278 ) WITH OIDS;
279 INSERT INTO node_types (node_type) VALUES ('regular');
280 INSERT INTO node_types (node_type) VALUES ('dummynet');
281
282 -- Nodes
283 CREATE TABLE nodes (
284     -- Mandatory
285     node_id serial PRIMARY KEY,                         -- Node identifier
286     node_type text REFERENCES node_types                -- node type
287                DEFAULT 'regular',
288
289     hostname text NOT NULL,                             -- Node hostname
290     site_id integer REFERENCES sites NOT NULL,          -- At which site 
291     boot_state text REFERENCES boot_states NOT NULL     -- Node boot state
292                DEFAULT 'reinstall', 
293     run_level  text REFERENCES run_levels DEFAULT NULL, -- Node Run Level
294     deleted boolean NOT NULL DEFAULT false,             -- Is deleted
295
296     -- Optional
297     model text,                                         -- Hardware make and model
298     boot_nonce text,                                    -- Random nonce updated by Boot Manager
299     version text,                                       -- Boot CD version string updated by Boot Manager
300     ssh_rsa_key text,                                   -- SSH host key updated by Boot Manager
301     key text,                                           -- Node key generated when boot file is downloaded
302         verified boolean NOT NULL DEFAULT false,                -- whether or not the node & pcu are verified
303
304     -- Timestamps
305     date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
306     last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
307     last_contact timestamp without time zone    
308 ) WITH OIDS;
309 CREATE INDEX nodes_hostname_idx ON nodes (hostname);
310 CREATE INDEX nodes_site_id_idx ON nodes (site_id);
311
312 -- Nodes at each site
313 CREATE OR REPLACE VIEW site_nodes AS
314 SELECT site_id,
315 array_accum(node_id) AS node_ids
316 FROM nodes
317 WHERE deleted IS false
318 GROUP BY site_id;
319
320 --------------------------------------------------------------------------------
321 -- node tags
322 --------------------------------------------------------------------------------
323 CREATE TABLE tag_types (
324
325     tag_type_id serial PRIMARY KEY,                     -- ID
326     tagname text UNIQUE NOT NULL,                       -- Tag Name
327     description text,                                   -- Optional Description
328     min_role_id integer REFERENCES roles DEFAULT 10,    -- set minimal role required
329     category text NOT NULL DEFAULT 'general'            -- Free text for grouping tags together
330 ) WITH OIDS;
331
332 CREATE TABLE node_tag (
333     node_tag_id serial PRIMARY KEY,                     -- ID
334     node_id integer REFERENCES nodes NOT NULL,          -- node id
335     tag_type_id integer REFERENCES tag_types,           -- tag type id
336     value text                                          -- value attached
337 ) WITH OIDS;
338
339 --------------------------------------------------------------------------------
340 -- (network) interfaces
341 --------------------------------------------------------------------------------
342
343 -- Valid network addressing schemes
344 CREATE TABLE network_types (
345     type text PRIMARY KEY -- Addressing scheme
346 ) WITH OIDS;
347 INSERT INTO network_types (type) VALUES ('ipv4');
348
349 -- Valid network configuration methods
350 CREATE TABLE network_methods (
351     method text PRIMARY KEY -- Configuration method
352 ) WITH OIDS;
353
354 INSERT INTO network_methods (method) VALUES ('static');
355 INSERT INTO network_methods (method) VALUES ('dhcp');
356 INSERT INTO network_methods (method) VALUES ('proxy');
357 INSERT INTO network_methods (method) VALUES ('tap');
358 INSERT INTO network_methods (method) VALUES ('ipmi');
359 INSERT INTO network_methods (method) VALUES ('unknown');
360
361 -- Network interfaces
362 CREATE TABLE interfaces (
363     -- Mandatory
364     interface_id serial PRIMARY KEY,                    -- Network interface identifier
365     node_id integer REFERENCES nodes NOT NULL,          -- Which node
366     is_primary boolean NOT NULL DEFAULT false,          -- Is the primary interface for this node
367     type text REFERENCES network_types NOT NULL,        -- Addressing scheme
368     method text REFERENCES network_methods NOT NULL,    -- Configuration method
369
370     -- Optional, depending on type and method
371     ip text,                                            -- IP address
372     mac text,                                           -- MAC address
373     gateway text,                                       -- Default gateway address
374     network text,                                       -- Network address
375     broadcast text,                                     -- Network broadcast address
376     netmask text,                                       -- Network mask
377     dns1 text,                                          -- Primary DNS server
378     dns2 text,                                          -- Secondary DNS server
379     bwlimit integer,                                    -- Bandwidth limit in bps
380     hostname text                                       -- Hostname of this interface
381 ) WITH OIDS;
382 CREATE INDEX interfaces_node_id_idx ON interfaces (node_id);
383
384 -- Ordered by primary interface first
385 CREATE OR REPLACE VIEW interfaces_ordered AS
386 SELECT node_id, interface_id
387 FROM interfaces
388 ORDER BY is_primary DESC;
389
390 -- Network interfaces on each node
391 CREATE OR REPLACE VIEW node_interfaces AS
392 SELECT node_id,
393 array_accum(interface_id) AS interface_ids
394 FROM interfaces_ordered
395 GROUP BY node_id;
396
397 --------------------------------------------------------------------------------
398 -- Interface tags (formerly known as interface settings)
399 --------------------------------------------------------------------------------
400
401 CREATE TABLE interface_tag (
402     interface_tag_id serial PRIMARY KEY,                -- Interface Setting Identifier
403     interface_id integer REFERENCES interfaces NOT NULL,-- the interface this applies to
404     tag_type_id integer REFERENCES tag_types NOT NULL,  -- the setting type
405     value text                                          -- value attached
406 ) WITH OIDS;
407
408 CREATE OR REPLACE VIEW interface_tags AS 
409 SELECT interface_id,
410 array_accum(interface_tag_id) AS interface_tag_ids
411 FROM interface_tag
412 GROUP BY interface_id;
413
414 CREATE OR REPLACE VIEW view_interface_tags AS
415 SELECT
416 interface_tag.interface_tag_id,
417 interface_tag.interface_id,
418 interfaces.ip,
419 tag_types.tag_type_id,
420 tag_types.tagname,
421 tag_types.description,
422 tag_types.category,
423 tag_types.min_role_id,
424 interface_tag.value
425 FROM interface_tag
426 INNER JOIN tag_types USING (tag_type_id)
427 INNER JOIN interfaces USING (interface_id);
428
429 CREATE OR REPLACE VIEW view_interfaces AS
430 SELECT
431 interfaces.interface_id,
432 interfaces.node_id,
433 interfaces.is_primary,
434 interfaces.type,
435 interfaces.method,
436 interfaces.ip,
437 interfaces.mac,
438 interfaces.gateway,
439 interfaces.network,
440 interfaces.broadcast,
441 interfaces.netmask,
442 interfaces.dns1,
443 interfaces.dns2,
444 interfaces.bwlimit,
445 interfaces.hostname,
446 COALESCE((SELECT interface_tag_ids FROM interface_tags WHERE interface_tags.interface_id = interfaces.interface_id), '{}') AS interface_tag_ids
447 FROM interfaces;
448
449 --------------------------------------------------------------------------------
450 -- ilinks : links between interfaces
451 --------------------------------------------------------------------------------
452 CREATE TABLE ilink (
453        ilink_id serial PRIMARY KEY,                             -- id
454        tag_type_id integer REFERENCES tag_types,                -- id of the tag type
455        src_interface_id integer REFERENCES interfaces not NULL, -- id of src interface
456        dst_interface_id integer REFERENCES interfaces NOT NULL, -- id of dst interface
457        value text                                               -- optional value on the link
458 ) WITH OIDS;
459
460 CREATE OR REPLACE VIEW view_ilinks AS
461 SELECT * FROM tag_types 
462 INNER JOIN ilink USING (tag_type_id);
463
464 -- xxx TODO : expose to view_interfaces the set of ilinks a given interface is part of
465 -- this is needed for properly deleting these ilinks when an interface gets deleted
466 -- as this is not done yet, it prevents DeleteInterface, thus DeleteNode, thus DeleteSite
467 -- from working correctly when an iLink is set
468
469 --------------------------------------------------------------------------------
470 -- Node groups
471 --------------------------------------------------------------------------------
472
473 -- Node groups
474 CREATE TABLE nodegroups (
475     nodegroup_id serial PRIMARY KEY,            -- Group identifier
476     groupname text UNIQUE NOT NULL,             -- Group name 
477     tag_type_id integer REFERENCES tag_types,   -- node is in nodegroup if it has this tag defined
478     -- can be null, make management faster & easier
479     value text                                  -- with this value attached
480 ) WITH OIDS;
481
482 -- xxx - first rough implem. similar to former semantics but might be slow
483 CREATE OR REPLACE VIEW nodegroup_node AS
484 SELECT nodegroup_id, node_id 
485 FROM tag_types 
486 JOIN node_tag 
487 USING (tag_type_id) 
488 JOIN nodegroups 
489 USING (tag_type_id,value);
490
491 CREATE OR REPLACE VIEW nodegroup_nodes AS
492 SELECT nodegroup_id,
493 array_accum(node_id) AS node_ids
494 FROM nodegroup_node
495 GROUP BY nodegroup_id;
496
497 -- Node groups that each node is a member of
498 CREATE OR REPLACE VIEW node_nodegroups AS
499 SELECT node_id,
500 array_accum(nodegroup_id) AS nodegroup_ids
501 FROM nodegroup_node
502 GROUP BY node_id;
503
504 --------------------------------------------------------------------------------
505 -- Node configuration files
506 --------------------------------------------------------------------------------
507
508 CREATE TABLE conf_files (
509     conf_file_id serial PRIMARY KEY,                    -- Configuration file identifier
510     enabled bool NOT NULL DEFAULT true,                 -- Configuration file is active
511     source text NOT NULL,                               -- Relative path on the boot server
512                                                         -- where file can be downloaded
513     dest text NOT NULL,                                 -- Absolute path where file should be installed
514     file_permissions text NOT NULL DEFAULT '0644',      -- chmod(1) permissions
515     file_owner text NOT NULL DEFAULT 'root',            -- chown(1) owner
516     file_group text NOT NULL DEFAULT 'root',            -- chgrp(1) owner
517     preinstall_cmd text,                                -- Shell command to execute prior to installing
518     postinstall_cmd text,                               -- Shell command to execute after installing
519     error_cmd text,                                     -- Shell command to execute if any error occurs
520     ignore_cmd_errors bool NOT NULL DEFAULT false,      -- Install file anyway even if an error occurs
521     always_update bool NOT NULL DEFAULT false           -- Always attempt to install file even if unchanged
522 ) WITH OIDS;
523
524 CREATE TABLE conf_file_node (
525     conf_file_id integer REFERENCES conf_files NOT NULL,        -- Configuration file identifier
526     node_id integer REFERENCES nodes NOT NULL,                  -- Node identifier
527     PRIMARY KEY (conf_file_id, node_id)
528 );
529 CREATE INDEX conf_file_node_conf_file_id_idx ON conf_file_node (conf_file_id);
530 CREATE INDEX conf_file_node_node_id_idx ON conf_file_node (node_id);
531
532 -- Nodes linked to each configuration file
533 CREATE OR REPLACE VIEW conf_file_nodes AS
534 SELECT conf_file_id,
535 array_accum(node_id) AS node_ids
536 FROM conf_file_node
537 GROUP BY conf_file_id;
538
539 -- Configuration files linked to each node
540 CREATE OR REPLACE VIEW node_conf_files AS
541 SELECT node_id,
542 array_accum(conf_file_id) AS conf_file_ids
543 FROM conf_file_node
544 GROUP BY node_id;
545
546 CREATE TABLE conf_file_nodegroup (
547     conf_file_id integer REFERENCES conf_files NOT NULL,        -- Configuration file identifier
548     nodegroup_id integer REFERENCES nodegroups NOT NULL,        -- Node group identifier
549     PRIMARY KEY (conf_file_id, nodegroup_id)
550 );
551 CREATE INDEX conf_file_nodegroup_conf_file_id_idx ON conf_file_nodegroup (conf_file_id);
552 CREATE INDEX conf_file_nodegroup_nodegroup_id_idx ON conf_file_nodegroup (nodegroup_id);
553
554 -- Node groups linked to each configuration file
555 CREATE OR REPLACE VIEW conf_file_nodegroups AS
556 SELECT conf_file_id,
557 array_accum(nodegroup_id) AS nodegroup_ids
558 FROM conf_file_nodegroup
559 GROUP BY conf_file_id;
560
561 -- Configuration files linked to each node group
562 CREATE OR REPLACE VIEW nodegroup_conf_files AS
563 SELECT nodegroup_id,
564 array_accum(conf_file_id) AS conf_file_ids
565 FROM conf_file_nodegroup
566 GROUP BY nodegroup_id;
567
568 --------------------------------------------------------------------------------
569 -- Power control units (PCUs)
570 --------------------------------------------------------------------------------
571
572 CREATE TABLE pcus (
573     -- Mandatory
574     pcu_id serial PRIMARY KEY,                          -- PCU identifier
575     site_id integer REFERENCES sites NOT NULL,          -- Site identifier
576     hostname text,                                      -- Hostname, not necessarily unique 
577                                                         -- (multiple logical sites could use the same PCU)
578     ip text NOT NULL,                                   -- IP, not necessarily unique
579
580     -- Optional
581     protocol text,                                      -- Protocol, e.g. ssh or https or telnet
582     username text,                                      -- Username, if applicable
583     "password" text,                                    -- Password, if applicable
584     model text,                                         -- Model, e.g. BayTech or iPal
585     notes text                                          -- Random notes
586 ) WITH OIDS;
587 CREATE INDEX pcus_site_id_idx ON pcus (site_id);
588
589 CREATE OR REPLACE VIEW site_pcus AS
590 SELECT site_id,
591 array_accum(pcu_id) AS pcu_ids
592 FROM pcus
593 GROUP BY site_id;
594
595 CREATE TABLE pcu_node (
596     pcu_id integer REFERENCES pcus NOT NULL,            -- PCU identifier
597     node_id integer REFERENCES nodes NOT NULL,          -- Node identifier
598     port integer NOT NULL,                              -- Port number
599     PRIMARY KEY (pcu_id, node_id),                      -- The same node cannot be controlled by different ports
600     UNIQUE (pcu_id, port)                               -- The same port cannot control multiple nodes
601 );
602 CREATE INDEX pcu_node_pcu_id_idx ON pcu_node (pcu_id);
603 CREATE INDEX pcu_node_node_id_idx ON pcu_node (node_id);
604
605 CREATE OR REPLACE VIEW node_pcus AS
606 SELECT node_id,
607 array_accum(pcu_id) AS pcu_ids,
608 array_accum(port) AS ports
609 FROM pcu_node
610 GROUP BY node_id;
611
612 CREATE OR REPLACE VIEW pcu_nodes AS
613 SELECT pcu_id,
614 array_accum(node_id) AS node_ids,
615 array_accum(port) AS ports
616 FROM pcu_node
617 GROUP BY pcu_id;
618
619 --------------------------------------------------------------------------------
620 -- Slices
621 --------------------------------------------------------------------------------
622
623 CREATE TABLE slice_instantiations (
624     instantiation text PRIMARY KEY
625 ) WITH OIDS;
626 INSERT INTO slice_instantiations (instantiation) VALUES ('not-instantiated');   -- Placeholder slice
627 INSERT INTO slice_instantiations (instantiation) VALUES ('plc-instantiated');   -- Instantiated by Node Manager
628 INSERT INTO slice_instantiations (instantiation) VALUES ('delegated');          -- Manually instantiated
629 INSERT INTO slice_instantiations (instantiation) VALUES ('nm-controller');      -- NM Controller
630
631 -- Slices
632 CREATE TABLE slices (
633     slice_id serial PRIMARY KEY,                        -- Slice identifier
634     site_id integer REFERENCES sites NOT NULL,          -- Site identifier
635
636     name text NOT NULL,                                 -- Slice name
637     instantiation text REFERENCES slice_instantiations  -- Slice state, e.g. plc-instantiated
638                   NOT NULL DEFAULT 'plc-instantiated',                  
639     url text,                                           -- Project URL
640     description text,                                   -- Project description
641
642     max_nodes integer NOT NULL DEFAULT 100,             -- Maximum number of nodes that can be assigned to this slice
643
644     creator_person_id integer REFERENCES persons,       -- Creator
645     created timestamp without time zone NOT NULL        -- Creation date
646         DEFAULT CURRENT_TIMESTAMP, 
647     expires timestamp without time zone NOT NULL        -- Expiration date
648         DEFAULT CURRENT_TIMESTAMP + '2 weeks', 
649
650     is_deleted boolean NOT NULL DEFAULT false
651 ) WITH OIDS;
652 CREATE INDEX slices_site_id_idx ON slices (site_id);
653 CREATE INDEX slices_name_idx ON slices (name);
654
655 -- Slivers
656 CREATE TABLE slice_node (
657     slice_id integer REFERENCES slices NOT NULL,        -- Slice identifier
658     node_id integer REFERENCES nodes NOT NULL,          -- Node identifier
659     PRIMARY KEY (slice_id, node_id)
660 ) WITH OIDS;
661 CREATE INDEX slice_node_slice_id_idx ON slice_node (slice_id);
662 CREATE INDEX slice_node_node_id_idx ON slice_node (node_id);
663
664 -- Synonym for slice_node
665 CREATE OR REPLACE VIEW slivers AS
666 SELECT * FROM slice_node;
667
668 -- Nodes in each slice
669 CREATE OR REPLACE VIEW slice_nodes AS
670 SELECT slice_id,
671 array_accum(node_id) AS node_ids
672 FROM slice_node
673 GROUP BY slice_id;
674
675 -- Slices on each node
676 CREATE OR REPLACE VIEW node_slices AS
677 SELECT node_id,
678 array_accum(slice_id) AS slice_ids
679 FROM slice_node
680 GROUP BY node_id;
681
682 -- Slices at each site
683 CREATE OR REPLACE VIEW site_slices AS
684 SELECT site_id,
685 array_accum(slice_id) AS slice_ids
686 FROM slices
687 WHERE is_deleted is false
688 GROUP BY site_id;
689
690 -- Slice membership
691 CREATE TABLE slice_person (
692     slice_id integer REFERENCES slices NOT NULL,        -- Slice identifier
693     person_id integer REFERENCES persons NOT NULL,      -- Account identifier
694     PRIMARY KEY (slice_id, person_id)
695 ) WITH OIDS;
696 CREATE INDEX slice_person_slice_id_idx ON slice_person (slice_id);
697 CREATE INDEX slice_person_person_id_idx ON slice_person (person_id);
698
699 -- Members of the slice
700 CREATE OR REPLACE VIEW slice_persons AS
701 SELECT slice_id,
702 array_accum(person_id) AS person_ids
703 FROM slice_person
704 GROUP BY slice_id;
705
706 -- Slices of which each person is a member
707 CREATE OR REPLACE VIEW person_slices AS
708 SELECT person_id,
709 array_accum(slice_id) AS slice_ids
710 FROM slice_person
711 GROUP BY person_id;
712
713 --------------------------------------------------------------------------------
714 -- Slice whitelist
715 --------------------------------------------------------------------------------
716 -- slice whitelist on nodes
717 CREATE TABLE node_slice_whitelist (
718     node_id integer REFERENCES nodes NOT NULL,          -- Node id of whitelist
719     slice_id integer REFERENCES slices NOT NULL,        -- Slice id thats allowd on this node
720     PRIMARY KEY (node_id, slice_id)
721 ) WITH OIDS;
722 CREATE INDEX node_slice_whitelist_node_id_idx ON node_slice_whitelist (node_id);
723 CREATE INDEX node_slice_whitelist_slice_id_idx ON node_slice_whitelist (slice_id);
724
725 -- Slices on each node
726 CREATE OR REPLACE VIEW node_slices_whitelist AS
727 SELECT node_id,
728 array_accum(slice_id) AS slice_ids_whitelist
729 FROM node_slice_whitelist
730 GROUP BY node_id;
731
732 --------------------------------------------------------------------------------
733 -- Slice tags (formerly known as slice attributes)
734 --------------------------------------------------------------------------------
735
736 -- Slice/sliver attributes
737 CREATE TABLE slice_tag (
738     slice_tag_id serial PRIMARY KEY,            -- Slice attribute identifier
739     slice_id integer REFERENCES slices NOT NULL,        -- Slice identifier
740     node_id integer REFERENCES nodes,                   -- Sliver attribute if set
741     nodegroup_id integer REFERENCES nodegroups,         -- Node group attribute if set
742     tag_type_id integer REFERENCES tag_types NOT NULL,  -- Attribute type identifier
743     value text
744 ) WITH OIDS;
745 CREATE INDEX slice_tag_slice_id_idx ON slice_tag (slice_id);
746 CREATE INDEX slice_tag_node_id_idx ON slice_tag (node_id);
747 CREATE INDEX slice_tag_nodegroup_id_idx ON slice_tag (nodegroup_id);
748
749 --------------------------------------------------------------------------------
750 -- Initscripts
751 --------------------------------------------------------------------------------
752
753 -- Initscripts
754 CREATE TABLE initscripts (
755     initscript_id serial PRIMARY KEY,                   -- Initscript identifier
756     name text NOT NULL,                                 -- Initscript name
757     enabled bool NOT NULL DEFAULT true,                 -- Initscript is active
758     script text NOT NULL,                               -- Initscript body
759     UNIQUE (name)
760 ) WITH OIDS;
761 CREATE INDEX initscripts_name_idx ON initscripts (name);
762
763
764 --------------------------------------------------------------------------------
765 -- Peers
766 --------------------------------------------------------------------------------
767
768 -- Peers
769 CREATE TABLE peers (
770     peer_id serial PRIMARY KEY,                         -- Peer identifier
771     peername text UNIQUE NOT NULL,                      -- Peer name
772     peer_url text NOT NULL,                             -- (HTTPS) URL of the peer PLCAPI interface
773     cacert text,                                        -- (SSL) Public certificate of peer API server
774     key text,                                           -- (GPG) Public key used for authentication
775     shortname text,                                     -- abbreviated name for displaying foreign objects
776     hrn_root text,                                              -- root for this peer domain
777     deleted boolean NOT NULL DEFAULT false
778 ) WITH OIDS;
779 CREATE INDEX peers_peername_idx ON peers (peername) WHERE deleted IS false;
780 CREATE INDEX peers_shortname_idx ON peers (shortname) WHERE deleted IS false;
781
782 -- Objects at each peer
783 CREATE TABLE peer_site (
784     site_id integer REFERENCES sites PRIMARY KEY,       -- Local site identifier
785     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
786     peer_site_id integer NOT NULL,                      -- Foreign site identifier at peer
787     UNIQUE (peer_id, peer_site_id)                      -- The same foreign site should not be cached twice
788 ) WITH OIDS;
789 CREATE INDEX peer_site_peer_id_idx ON peers (peer_id);
790
791 CREATE OR REPLACE VIEW peer_sites AS
792 SELECT peer_id,
793 array_accum(site_id) AS site_ids,
794 array_accum(peer_site_id) AS peer_site_ids
795 FROM peer_site
796 GROUP BY peer_id;
797
798 CREATE TABLE peer_person (
799     person_id integer REFERENCES persons PRIMARY KEY,   -- Local user identifier
800     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
801     peer_person_id integer NOT NULL,                    -- Foreign user identifier at peer
802     UNIQUE (peer_id, peer_person_id)                    -- The same foreign user should not be cached twice
803 ) WITH OIDS;
804 CREATE INDEX peer_person_peer_id_idx ON peer_person (peer_id);
805
806 CREATE OR REPLACE VIEW peer_persons AS
807 SELECT peer_id,
808 array_accum(person_id) AS person_ids,
809 array_accum(peer_person_id) AS peer_person_ids
810 FROM peer_person
811 GROUP BY peer_id;
812
813 CREATE TABLE peer_key (
814     key_id integer REFERENCES keys PRIMARY KEY,         -- Local key identifier
815     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
816     peer_key_id integer NOT NULL,                       -- Foreign key identifier at peer
817     UNIQUE (peer_id, peer_key_id)                       -- The same foreign key should not be cached twice
818 ) WITH OIDS;
819 CREATE INDEX peer_key_peer_id_idx ON peer_key (peer_id);
820
821 CREATE OR REPLACE VIEW peer_keys AS
822 SELECT peer_id,
823 array_accum(key_id) AS key_ids,
824 array_accum(peer_key_id) AS peer_key_ids
825 FROM peer_key
826 GROUP BY peer_id;
827
828 CREATE TABLE peer_node (
829     node_id integer REFERENCES nodes PRIMARY KEY,       -- Local node identifier
830     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
831     peer_node_id integer NOT NULL,                      -- Foreign node identifier
832     UNIQUE (peer_id, peer_node_id)                      -- The same foreign node should not be cached twice
833 ) WITH OIDS;
834 CREATE INDEX peer_node_peer_id_idx ON peer_node (peer_id);
835
836 CREATE OR REPLACE VIEW peer_nodes AS
837 SELECT peer_id,
838 array_accum(node_id) AS node_ids,
839 array_accum(peer_node_id) AS peer_node_ids
840 FROM peer_node
841 GROUP BY peer_id;
842
843 CREATE TABLE peer_slice (
844     slice_id integer REFERENCES slices PRIMARY KEY,     -- Local slice identifier
845     peer_id integer REFERENCES peers NOT NULL,          -- Peer identifier
846     peer_slice_id integer NOT NULL,                     -- Slice identifier at peer
847     UNIQUE (peer_id, peer_slice_id)                     -- The same foreign slice should not be cached twice
848 ) WITH OIDS;
849 CREATE INDEX peer_slice_peer_id_idx ON peer_slice (peer_id);
850
851 CREATE OR REPLACE VIEW peer_slices AS
852 SELECT peer_id,
853 array_accum(slice_id) AS slice_ids,
854 array_accum(peer_slice_id) AS peer_slice_ids
855 FROM peer_slice
856 GROUP BY peer_id;
857
858 --------------------------------------------------------------------------------
859 -- Authenticated sessions
860 --------------------------------------------------------------------------------
861
862 -- Authenticated sessions
863 CREATE TABLE sessions (
864     session_id text PRIMARY KEY,                        -- Session identifier
865     expires timestamp without time zone
866 ) WITH OIDS;
867
868 -- People can have multiple sessions
869 CREATE TABLE person_session (
870     person_id integer REFERENCES persons NOT NULL,      -- Account identifier
871     session_id text REFERENCES sessions NOT NULL,       -- Session identifier
872     PRIMARY KEY (person_id, session_id),
873     UNIQUE (session_id)                                 -- Sessions are unique
874 ) WITH OIDS;
875 CREATE INDEX person_session_person_id_idx ON person_session (person_id);
876
877 -- Nodes can have only one session
878 CREATE TABLE node_session (
879     node_id integer REFERENCES nodes NOT NULL,          -- Node identifier
880     session_id text REFERENCES sessions NOT NULL,       -- Session identifier
881     UNIQUE (node_id),                                   -- Nodes can have only one session
882     UNIQUE (session_id)                                 -- Sessions are unique
883 ) WITH OIDS;
884
885 -------------------------------------------------------------------------------
886 -- PCU Types
887 ------------------------------------------------------------------------------
888 CREATE TABLE pcu_types (
889     pcu_type_id serial PRIMARY KEY,
890     model text NOT NULL ,                               -- PCU model name
891     name text                                           -- Full PCU model name
892 ) WITH OIDS;
893 CREATE INDEX pcu_types_model_idx ON pcu_types (model);
894
895 CREATE TABLE pcu_protocol_type (
896     pcu_protocol_type_id serial PRIMARY KEY,
897     pcu_type_id integer REFERENCES pcu_types NOT NULL,  -- PCU type identifier
898     port integer NOT NULL,                              -- PCU port
899     protocol text NOT NULL,                             -- Protocol
900     supported boolean NOT NULL DEFAULT True             -- Does PLC support
901 ) WITH OIDS;
902 CREATE INDEX pcu_protocol_type_pcu_type_id ON pcu_protocol_type (pcu_type_id);
903
904
905 CREATE OR REPLACE VIEW pcu_protocol_types AS
906 SELECT pcu_type_id,
907 array_accum(pcu_protocol_type_id) as pcu_protocol_type_ids
908 FROM pcu_protocol_type
909 GROUP BY pcu_type_id;
910
911 --------------------------------------------------------------------------------
912 -- Message templates
913 --------------------------------------------------------------------------------
914
915 CREATE TABLE messages (
916     message_id text PRIMARY KEY,                        -- Message name
917     subject text,                                       -- Message summary
918     template text,                                      -- Message template
919     enabled bool NOT NULL DEFAULT true                  -- Whether message is enabled
920 ) WITH OIDS;
921
922 --------------------------------------------------------------------------------
923 -- Events
924 --------------------------------------------------------------------------------
925
926 -- Events
927 CREATE TABLE events (
928     event_id serial PRIMARY KEY,                        -- Event identifier
929     person_id integer REFERENCES persons,               -- Person responsible for event, if any
930     node_id integer REFERENCES nodes,                   -- Node responsible for event, if any
931     auth_type text,                                     -- Type of auth used. i.e. AuthMethod
932     fault_code integer NOT NULL DEFAULT 0,              -- Did this event result in error
933     call_name text NOT NULL,                            -- Call responsible for this event
934     call text NOT NULL,                                 -- Call responsible for this event, including parameters
935     message text,                                       -- High level description of this event
936     runtime float DEFAULT 0,                            -- Event run time
937     time timestamp without time zone NOT NULL           -- Event timestamp
938         DEFAULT CURRENT_TIMESTAMP
939 ) WITH OIDS;
940
941 -- Database object(s) that may have been affected by a particular event
942 CREATE TABLE event_object (
943     event_id integer REFERENCES events NOT NULL,        -- Event identifier
944     object_id integer NOT NULL,                         -- Object identifier
945     object_type text NOT NULL Default 'Unknown'         -- What type of object is this event affecting
946 ) WITH OIDS;
947 CREATE INDEX event_object_event_id_idx ON event_object (event_id);
948 CREATE INDEX event_object_object_id_idx ON event_object (object_id);
949 CREATE INDEX event_object_object_type_idx ON event_object (object_type);
950
951 CREATE OR REPLACE VIEW event_objects AS
952 SELECT event_id,
953 array_accum(object_id) AS object_ids,
954 array_accum(object_type) AS object_types
955 FROM event_object
956 GROUP BY event_id;
957
958 --------------------------------------------------------------------------------
959 -- Useful views
960 --------------------------------------------------------------------------------
961 CREATE OR REPLACE VIEW view_pcu_types AS
962 SELECT
963 pcu_types.pcu_type_id,
964 pcu_types.model,
965 pcu_types.name,
966 COALESCE((SELECT pcu_protocol_type_ids FROM pcu_protocol_types
967                  WHERE pcu_protocol_types.pcu_type_id = pcu_types.pcu_type_id), '{}') 
968 AS pcu_protocol_type_ids
969 FROM pcu_types;
970
971 --------------------------------------------------------------------------------
972 CREATE OR REPLACE VIEW view_events AS
973 SELECT
974 events.event_id,
975 events.person_id,
976 events.node_id,
977 events.auth_type,
978 events.fault_code,
979 events.call_name,
980 events.call,
981 events.message,
982 events.runtime,
983 CAST(date_part('epoch', events.time) AS bigint) AS time,
984 COALESCE((SELECT object_ids FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_ids,
985 COALESCE((SELECT object_types FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_types
986 FROM events;
987
988 CREATE OR REPLACE VIEW view_event_objects AS 
989 SELECT
990 events.event_id,
991 events.person_id,
992 events.node_id,
993 events.fault_code,
994 events.call_name,
995 events.call,
996 events.message,
997 events.runtime,
998 CAST(date_part('epoch', events.time) AS bigint) AS time,
999 event_object.object_id,
1000 event_object.object_type
1001 FROM events LEFT JOIN event_object USING (event_id);
1002
1003 --------------------------------------------------------------------------------
1004 CREATE OR REPLACE VIEW view_persons AS
1005 SELECT
1006 persons.person_id,
1007 persons.email,
1008 persons.first_name,
1009 persons.last_name,
1010 persons.deleted,
1011 persons.enabled,
1012 persons.password,
1013 persons.verification_key,
1014 CAST(date_part('epoch', persons.verification_expires) AS bigint) AS verification_expires,
1015 persons.title,
1016 persons.phone,
1017 persons.url,
1018 persons.bio,
1019 CAST(date_part('epoch', persons.date_created) AS bigint) AS date_created,
1020 CAST(date_part('epoch', persons.last_updated) AS bigint) AS last_updated,
1021 peer_person.peer_id,
1022 peer_person.peer_person_id,
1023 COALESCE((SELECT role_ids FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS role_ids,
1024 COALESCE((SELECT roles FROM person_roles WHERE person_roles.person_id = persons.person_id), '{}') AS roles,
1025 COALESCE((SELECT site_ids FROM person_sites WHERE person_sites.person_id = persons.person_id), '{}') AS site_ids,
1026 COALESCE((SELECT key_ids FROM person_keys WHERE person_keys.person_id = persons.person_id), '{}') AS key_ids,
1027 COALESCE((SELECT slice_ids FROM person_slices WHERE person_slices.person_id = persons.person_id), '{}') AS slice_ids
1028 FROM persons
1029 LEFT JOIN peer_person USING (person_id);
1030
1031 --------------------------------------------------------------------------------
1032 CREATE OR REPLACE VIEW view_peers AS
1033 SELECT 
1034 peers.*, 
1035 COALESCE((SELECT site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS site_ids,
1036 COALESCE((SELECT peer_site_ids FROM peer_sites WHERE peer_sites.peer_id = peers.peer_id), '{}') AS peer_site_ids,
1037 COALESCE((SELECT person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS person_ids,
1038 COALESCE((SELECT peer_person_ids FROM peer_persons WHERE peer_persons.peer_id = peers.peer_id), '{}') AS peer_person_ids,
1039 COALESCE((SELECT key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS key_ids,
1040 COALESCE((SELECT peer_key_ids FROM peer_keys WHERE peer_keys.peer_id = peers.peer_id), '{}') AS peer_key_ids,
1041 COALESCE((SELECT node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS node_ids,
1042 COALESCE((SELECT peer_node_ids FROM peer_nodes WHERE peer_nodes.peer_id = peers.peer_id), '{}') AS peer_node_ids,
1043 COALESCE((SELECT slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS slice_ids,
1044 COALESCE((SELECT peer_slice_ids FROM peer_slices WHERE peer_slices.peer_id = peers.peer_id), '{}') AS peer_slice_ids
1045 FROM peers;
1046
1047 --------------------------------------------------------------------------------
1048 CREATE OR REPLACE VIEW node_tags AS
1049 SELECT node_id,
1050 array_accum(node_tag_id) AS node_tag_ids
1051 FROM node_tag
1052 GROUP BY node_id;
1053
1054 CREATE OR REPLACE VIEW view_node_tags AS
1055 SELECT
1056 node_tag.node_tag_id,
1057 node_tag.node_id,
1058 nodes.hostname,
1059 tag_types.tag_type_id,
1060 tag_types.tagname,
1061 tag_types.description,
1062 tag_types.category,
1063 tag_types.min_role_id,
1064 node_tag.value
1065 FROM node_tag 
1066 INNER JOIN tag_types USING (tag_type_id)
1067 INNER JOIN nodes USING (node_id);
1068
1069 CREATE OR REPLACE VIEW view_nodes AS
1070 SELECT
1071 nodes.node_id,
1072 nodes.node_type,
1073 nodes.hostname,
1074 nodes.site_id,
1075 nodes.boot_state,
1076 nodes.run_level,
1077 nodes.deleted,
1078 nodes.model,
1079 nodes.boot_nonce,
1080 nodes.version,
1081 nodes.verified,
1082 nodes.ssh_rsa_key,
1083 nodes.key,
1084 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
1085 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
1086 CAST(date_part('epoch', nodes.last_contact) AS bigint) AS last_contact,  
1087 peer_node.peer_id,
1088 peer_node.peer_node_id,
1089 COALESCE((SELECT interface_ids FROM node_interfaces 
1090                  WHERE node_interfaces.node_id = nodes.node_id), '{}') 
1091 AS interface_ids,
1092 COALESCE((SELECT nodegroup_ids FROM node_nodegroups 
1093                  WHERE node_nodegroups.node_id = nodes.node_id), '{}') 
1094 AS nodegroup_ids,
1095 COALESCE((SELECT slice_ids FROM node_slices 
1096                  WHERE node_slices.node_id = nodes.node_id), '{}') 
1097 AS slice_ids,
1098 COALESCE((SELECT slice_ids_whitelist FROM node_slices_whitelist 
1099                  WHERE node_slices_whitelist.node_id = nodes.node_id), '{}') 
1100 AS slice_ids_whitelist,
1101 COALESCE((SELECT pcu_ids FROM node_pcus 
1102                  WHERE node_pcus.node_id = nodes.node_id), '{}') 
1103 AS pcu_ids,
1104 COALESCE((SELECT ports FROM node_pcus
1105                  WHERE node_pcus.node_id = nodes.node_id), '{}') 
1106 AS ports,
1107 COALESCE((SELECT conf_file_ids FROM node_conf_files
1108                  WHERE node_conf_files.node_id = nodes.node_id), '{}') 
1109 AS conf_file_ids,
1110 COALESCE((SELECT node_tag_ids FROM node_tags 
1111                  WHERE node_tags.node_id = nodes.node_id), '{}') 
1112 AS node_tag_ids,
1113 node_session.session_id AS session
1114 FROM nodes
1115 LEFT JOIN peer_node USING (node_id)
1116 LEFT JOIN node_session USING (node_id);
1117
1118 --------------------------------------------------------------------------------
1119 CREATE OR REPLACE VIEW view_nodegroups AS
1120 SELECT
1121 nodegroups.*,
1122 tag_types.tagname,
1123 COALESCE((SELECT conf_file_ids FROM nodegroup_conf_files 
1124                  WHERE nodegroup_conf_files.nodegroup_id = nodegroups.nodegroup_id), '{}') 
1125 AS conf_file_ids,
1126 COALESCE((SELECT node_ids FROM nodegroup_nodes 
1127                  WHERE nodegroup_nodes.nodegroup_id = nodegroups.nodegroup_id), '{}') 
1128 AS node_ids
1129 FROM nodegroups INNER JOIN tag_types USING (tag_type_id);
1130
1131 --------------------------------------------------------------------------------
1132 CREATE OR REPLACE VIEW view_conf_files AS
1133 SELECT
1134 conf_files.*,
1135 COALESCE((SELECT node_ids FROM conf_file_nodes 
1136                  WHERE conf_file_nodes.conf_file_id = conf_files.conf_file_id), '{}') 
1137 AS node_ids,
1138 COALESCE((SELECT nodegroup_ids FROM conf_file_nodegroups 
1139                  WHERE conf_file_nodegroups.conf_file_id = conf_files.conf_file_id), '{}') 
1140 AS nodegroup_ids
1141 FROM conf_files;
1142
1143 --------------------------------------------------------------------------------
1144 CREATE OR REPLACE VIEW view_pcus AS
1145 SELECT
1146 pcus.*,
1147 COALESCE((SELECT node_ids FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS node_ids,
1148 COALESCE((SELECT ports FROM pcu_nodes WHERE pcu_nodes.pcu_id = pcus.pcu_id), '{}') AS ports
1149 FROM pcus;
1150
1151 --------------------------------------------------------------------------------
1152 CREATE OR REPLACE VIEW view_sites AS
1153 SELECT
1154 sites.site_id,
1155 sites.login_base,
1156 sites.name,
1157 sites.abbreviated_name,
1158 sites.deleted,
1159 sites.enabled,
1160 sites.is_public,
1161 sites.max_slices,
1162 sites.max_slivers,
1163 sites.latitude,
1164 sites.longitude,
1165 sites.url,
1166 sites.ext_consortium_id,
1167 CAST(date_part('epoch', sites.date_created) AS bigint) AS date_created,
1168 CAST(date_part('epoch', sites.last_updated) AS bigint) AS last_updated,
1169 peer_site.peer_id,
1170 peer_site.peer_site_id,
1171 COALESCE((SELECT person_ids FROM site_persons WHERE site_persons.site_id = sites.site_id), '{}') AS person_ids,
1172 COALESCE((SELECT node_ids FROM site_nodes WHERE site_nodes.site_id = sites.site_id), '{}') AS node_ids,
1173 COALESCE((SELECT address_ids FROM site_addresses WHERE site_addresses.site_id = sites.site_id), '{}') AS address_ids,
1174 COALESCE((SELECT slice_ids FROM site_slices WHERE site_slices.site_id = sites.site_id), '{}') AS slice_ids,
1175 COALESCE((SELECT pcu_ids FROM site_pcus WHERE site_pcus.site_id = sites.site_id), '{}') AS pcu_ids
1176 FROM sites
1177 LEFT JOIN peer_site USING (site_id);
1178
1179 --------------------------------------------------------------------------------
1180 CREATE OR REPLACE VIEW view_addresses AS
1181 SELECT
1182 addresses.*,
1183 COALESCE((SELECT address_type_ids FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_type_ids,
1184 COALESCE((SELECT address_types FROM address_address_types WHERE address_address_types.address_id = addresses.address_id), '{}') AS address_types
1185 FROM addresses;
1186
1187 --------------------------------------------------------------------------------
1188 CREATE OR REPLACE VIEW view_keys AS
1189 SELECT
1190 keys.*,
1191 person_key.person_id,
1192 peer_key.peer_id,
1193 peer_key.peer_key_id
1194 FROM keys
1195 LEFT JOIN person_key USING (key_id)
1196 LEFT JOIN peer_key USING (key_id);
1197
1198 --------------------------------------------------------------------------------
1199 CREATE OR REPLACE VIEW slice_tags AS
1200 SELECT slice_id,
1201 array_accum(slice_tag_id) AS slice_tag_ids
1202 FROM slice_tag
1203 GROUP BY slice_id;
1204
1205 CREATE OR REPLACE VIEW view_slices AS
1206 SELECT
1207 slices.slice_id,
1208 slices.site_id,
1209 slices.name,
1210 slices.instantiation,
1211 slices.url,
1212 slices.description,
1213 slices.max_nodes,
1214 slices.creator_person_id,
1215 slices.is_deleted,
1216 CAST(date_part('epoch', slices.created) AS bigint) AS created,
1217 CAST(date_part('epoch', slices.expires) AS bigint) AS expires,
1218 peer_slice.peer_id,
1219 peer_slice.peer_slice_id,
1220 COALESCE((SELECT node_ids FROM slice_nodes WHERE slice_nodes.slice_id = slices.slice_id), '{}') AS node_ids,
1221 COALESCE((SELECT person_ids FROM slice_persons WHERE slice_persons.slice_id = slices.slice_id), '{}') AS person_ids,
1222 COALESCE((SELECT slice_tag_ids FROM slice_tags WHERE slice_tags.slice_id = slices.slice_id), '{}') AS slice_tag_ids
1223 FROM slices
1224 LEFT JOIN peer_slice USING (slice_id);
1225
1226 CREATE OR REPLACE VIEW view_slice_tags AS
1227 SELECT
1228 slice_tag.slice_tag_id,
1229 slice_tag.slice_id,
1230 slice_tag.node_id,
1231 slice_tag.nodegroup_id,
1232 tag_types.tag_type_id,
1233 tag_types.tagname,
1234 tag_types.description,
1235 tag_types.category,
1236 tag_types.min_role_id,
1237 slice_tag.value,
1238 slices.name
1239 FROM slice_tag
1240 INNER JOIN tag_types USING (tag_type_id)
1241 INNER JOIN slices USING (slice_id);
1242
1243 --------------------------------------------------------------------------------
1244 CREATE OR REPLACE VIEW view_sessions AS
1245 SELECT
1246 sessions.session_id,
1247 CAST(date_part('epoch', sessions.expires) AS bigint) AS expires,
1248 person_session.person_id,
1249 node_session.node_id
1250 FROM sessions
1251 LEFT JOIN person_session USING (session_id)
1252 LEFT JOIN node_session USING (session_id);
1253
1254 --------------------------------------------------------------------------------
1255 -- Built-in maintenance account and default site
1256 --------------------------------------------------------------------------------
1257
1258 INSERT INTO persons (first_name, last_name, email, password, enabled)
1259 VALUES              ('Maintenance', 'Account', 'maint@localhost.localdomain', 'nopass', true);
1260
1261 INSERT INTO person_role (person_id, role_id) VALUES (1, 10);
1262 INSERT INTO person_role (person_id, role_id) VALUES (1, 20);
1263 INSERT INTO person_role (person_id, role_id) VALUES (1, 30);
1264 INSERT INTO person_role (person_id, role_id) VALUES (1, 40);
1265
1266 INSERT INTO sites (login_base, name, abbreviated_name, max_slices)
1267 VALUES ('pl', 'PlanetLab Central', 'PLC', 100);