Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / migrations / 005-up-import-apr-2007.sql
1 --
2 -- to apply changes from import done in april 2007 from the
3 -- planetlab-4_0-branch tag
4 --
5 -- this is a rather complex example, so for next times, make sure that you
6 -- * first add new columns and new tables
7 -- * then create or replace views
8 -- * and only finally drop columns
9 -- otherwise the columns may refuse to get dropped if they are still used by views
10 --
11
12 ---------- creations 
13
14 ALTER TABLE sites ADD ext_consortium_id integer;
15
16 ALTER TABLE nodes ADD last_contact timestamp without time zone;
17
18 -- Initscripts
19 CREATE TABLE initscripts (
20     initscript_id serial PRIMARY KEY, -- Initscript identifier
21     name text NOT NULL, -- Initscript name
22     enabled bool NOT NULL DEFAULT true, -- Initscript is active
23     script text NOT NULL, -- Initscript
24     UNIQUE (name)
25 ) WITH OIDS;
26 CREATE INDEX initscripts_name_idx ON initscripts (name);
27
28 -- rather drop the tables altogether, 
29 -- ALTER TABLE events ADD auth_type text;
30 -- ALTER TABLE event_object ADD COLUMN object_type text NOT NULL Default 'Unknown';
31 -- CREATE INDEX event_object_object_type_idx ON event_object (object_type);
32
33 -- for some reason these views require to be dropped first
34 DROP VIEW view_events;
35 DROP VIEW event_objects;
36 DROP VIEW view_nodes;
37 DROP VIEW view_sites;
38
39 ----dropping tables must be preceded by dropping views using those tables
40 ----otherwise  dependency problems
41 DROP TABLE event_object;
42 DROP TABLE events;
43
44 CREATE TABLE events (
45     event_id serial PRIMARY KEY,  -- Event identifier
46     person_id integer REFERENCES persons, -- Person responsible for event, if any
47     node_id integer REFERENCES nodes, -- Node responsible for event, if any
48     auth_type text, -- Type of auth used. i.e. AuthMethod
49     fault_code integer NOT NULL DEFAULT 0, -- Did this event result in error
50     call_name text NOT NULL, -- Call responsible for this event
51     call text NOT NULL, -- Call responsible for this event, including parameters
52     message text, -- High level description of this event
53     runtime float DEFAULT 0, -- Event run time
54     time timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP -- Event timestamp
55 ) WITH OIDS;
56
57 -- Database object(s) that may have been affected by a particular event
58 CREATE TABLE event_object (
59     event_id integer REFERENCES events NOT NULL, -- Event identifier
60     object_id integer NOT NULL, -- Object identifier
61     object_type text NOT NULL Default 'Unknown' -- What type of object is this event affecting
62 ) WITH OIDS;
63 CREATE INDEX event_object_event_id_idx ON event_object (event_id);
64 CREATE INDEX event_object_object_id_idx ON event_object (object_id);
65 CREATE INDEX event_object_object_type_idx ON event_object (object_type);
66
67 ---------- view changes
68
69 CREATE OR REPLACE VIEW event_objects AS
70 SELECT event_id,
71 array_accum(object_id) AS object_ids,
72 array_accum(object_type) AS object_types
73 FROM event_object
74 GROUP BY event_id;
75
76 CREATE OR REPLACE VIEW view_events AS
77 SELECT
78 events.event_id,
79 events.person_id,
80 events.node_id,
81 events.auth_type,
82 events.fault_code,
83 events.call_name,
84 events.call,
85 events.message,
86 events.runtime,
87 CAST(date_part('epoch', events.time) AS bigint) AS time,
88 COALESCE((SELECT object_ids FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_ids,
89 COALESCE((SELECT object_types FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_types
90 FROM events;
91
92 CREATE OR REPLACE VIEW view_nodes AS
93 SELECT
94 nodes.node_id,
95 nodes.hostname,
96 nodes.site_id,
97 nodes.boot_state,
98 nodes.deleted,
99 nodes.model,
100 nodes.boot_nonce,
101 nodes.version,
102 nodes.ssh_rsa_key,
103 nodes.key,
104 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
105 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
106 CAST(date_part('epoch', nodes.last_contact) AS bigint) AS last_contact,  
107 peer_node.peer_id,
108 peer_node.peer_node_id,
109 COALESCE((SELECT nodenetwork_ids FROM node_nodenetworks WHERE node_nodenetworks.node_id = nodes.node_id), '{}') AS nodenetwork_ids,
110 COALESCE((SELECT nodegroup_ids FROM node_nodegroups WHERE node_nodegroups.node_id = nodes.node_id), '{}') AS nodegroup_ids,
111 COALESCE((SELECT slice_ids FROM node_slices WHERE node_slices.node_id = nodes.node_id), '{}') AS slice_ids,
112 COALESCE((SELECT pcu_ids FROM node_pcus WHERE node_pcus.node_id = nodes.node_id), '{}') AS pcu_ids,
113 COALESCE((SELECT ports FROM node_pcus WHERE node_pcus.node_id = nodes.node_id), '{}') AS ports,
114 COALESCE((SELECT conf_file_ids FROM node_conf_files WHERE node_conf_files.node_id = nodes.node_id), '{}') AS conf_file_ids,
115 node_session.session_id AS session
116 FROM nodes
117 LEFT JOIN peer_node USING (node_id)
118 LEFT JOIN node_session USING (node_id);
119
120 CREATE OR REPLACE VIEW view_sites AS
121 SELECT
122 sites.site_id,
123 sites.login_base,
124 sites.name,
125 sites.abbreviated_name,
126 sites.deleted,
127 sites.enabled,
128 sites.is_public,
129 sites.max_slices,
130 sites.max_slivers,
131 sites.latitude,
132 sites.longitude,
133 sites.url,
134 sites.ext_consortium_id,
135 CAST(date_part('epoch', sites.date_created) AS bigint) AS date_created,
136 CAST(date_part('epoch', sites.last_updated) AS bigint) AS last_updated,
137 peer_site.peer_id,
138 peer_site.peer_site_id,
139 COALESCE((SELECT person_ids FROM site_persons WHERE site_persons.site_id = sites.site_id), '{}') AS person_ids,
140 COALESCE((SELECT node_ids FROM site_nodes WHERE site_nodes.site_id = sites.site_id), '{}') AS node_ids,
141 COALESCE((SELECT address_ids FROM site_addresses WHERE site_addresses.site_id = sites.site_id), '{}') AS address_ids,
142 COALESCE((SELECT slice_ids FROM site_slices WHERE site_slices.site_id = sites.site_id), '{}') AS slice_ids,
143 COALESCE((SELECT pcu_ids FROM site_pcus WHERE site_pcus.site_id = sites.site_id), '{}') AS pcu_ids
144 FROM sites
145 LEFT JOIN peer_site USING (site_id);
146
147 ---------- deletions
148 --dont need to drop this colum it doesn't exit anymore 
149 -----ALTER TABLE events DROP COLUMN object_type;
150
151 ---------- bump subversion
152
153 UPDATE plc_db_version SET subversion = 5;
154 SELECT subversion from plc_db_version;