Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / migrations / 005-down-import-apr-2007.sql
1 -- revert migration 005
2 -- 
3 -- this is a rather complex example, so for next times, make sure that you
4 -- * first restore old columns or tables
5 -- * then create or replace views
6 -- * and only finally drop new columns and tables
7 -- otherwise the columns may refuse to get dropped if they are still used by views
8 --
9
10 ---------- creations 
11
12 ALTER TABLE events ADD object_type text NOT NULL Default 'Unknown';
13
14 ---------- view changes
15
16 -- for some reason these views require to be dropped first
17 DROP VIEW view_events;
18 DROP VIEW event_objects;
19 DROP VIEW view_nodes;
20 DROP VIEW view_sites;
21
22 CREATE OR REPLACE VIEW event_objects AS
23 SELECT event_id,
24 array_accum(object_id) AS object_ids
25 FROM event_object
26 GROUP BY event_id;
27
28 CREATE OR REPLACE VIEW view_events AS
29 SELECT
30 events.event_id,
31 events.person_id,
32 events.node_id,
33 events.fault_code,
34 events.call_name,
35 events.call,
36 events.object_type,
37 events.message,
38 events.runtime,
39 CAST(date_part('epoch', events.time) AS bigint) AS time,
40 COALESCE((SELECT object_ids FROM event_objects WHERE event_objects.event_id = events.event_id), '{}') AS object_ids
41 FROM events;
42
43 CREATE OR REPLACE VIEW view_nodes AS
44 SELECT
45 nodes.node_id,
46 nodes.hostname,
47 nodes.site_id,
48 nodes.boot_state,
49 nodes.deleted,
50 nodes.model,
51 nodes.boot_nonce,
52 nodes.version,
53 nodes.ssh_rsa_key,
54 nodes.key,
55 CAST(date_part('epoch', nodes.date_created) AS bigint) AS date_created,
56 CAST(date_part('epoch', nodes.last_updated) AS bigint) AS last_updated,
57 peer_node.peer_id,
58 peer_node.peer_node_id,
59 COALESCE((SELECT nodenetwork_ids FROM node_nodenetworks WHERE node_nodenetworks.node_id = nodes.node_id), '{}') AS nodenetwork_ids,
60 COALESCE((SELECT nodegroup_ids FROM node_nodegroups WHERE node_nodegroups.node_id = nodes.node_id), '{}') AS nodegroup_ids,
61 COALESCE((SELECT slice_ids FROM node_slices WHERE node_slices.node_id = nodes.node_id), '{}') AS slice_ids,
62 COALESCE((SELECT pcu_ids FROM node_pcus WHERE node_pcus.node_id = nodes.node_id), '{}') AS pcu_ids,
63 COALESCE((SELECT ports FROM node_pcus WHERE node_pcus.node_id = nodes.node_id), '{}') AS ports,
64 COALESCE((SELECT conf_file_ids FROM node_conf_files WHERE node_conf_files.node_id = nodes.node_id), '{}') AS conf_file_ids,
65 node_session.session_id AS session
66 FROM nodes
67 LEFT JOIN peer_node USING (node_id)
68 LEFT JOIN node_session USING (node_id);
69
70 CREATE OR REPLACE VIEW view_sites AS
71 SELECT
72 sites.site_id,
73 sites.login_base,
74 sites.name,
75 sites.abbreviated_name,
76 sites.deleted,
77 sites.enabled,
78 sites.is_public,
79 sites.max_slices,
80 sites.max_slivers,
81 sites.latitude,
82 sites.longitude,
83 sites.url,
84 CAST(date_part('epoch', sites.date_created) AS bigint) AS date_created,
85 CAST(date_part('epoch', sites.last_updated) AS bigint) AS last_updated,
86 peer_site.peer_id,
87 peer_site.peer_site_id,
88 COALESCE((SELECT person_ids FROM site_persons WHERE site_persons.site_id = sites.site_id), '{}') AS person_ids,
89 COALESCE((SELECT node_ids FROM site_nodes WHERE site_nodes.site_id = sites.site_id), '{}') AS node_ids,
90 COALESCE((SELECT address_ids FROM site_addresses WHERE site_addresses.site_id = sites.site_id), '{}') AS address_ids,
91 COALESCE((SELECT slice_ids FROM site_slices WHERE site_slices.site_id = sites.site_id), '{}') AS slice_ids,
92 COALESCE((SELECT pcu_ids FROM site_pcus WHERE site_pcus.site_id = sites.site_id), '{}') AS pcu_ids
93 FROM sites
94 LEFT JOIN peer_site USING (site_id);
95
96 ---------- deletions
97
98 ALTER TABLE sites DROP COLUMN ext_consortium_id;
99
100 ALTER TABLE nodes DROP COLUMN last_contact;
101
102 DROP INDEX initscripts_name_idx;
103 DROP TABLE initscripts;
104
105 ALTER TABLE events DROP COLUMN auth_type;
106
107 ALTER TABLE event_object DROP COLUMN object_type;
108
109 ---------- revert subversion
110
111 UPDATE plc_db_version SET subversion = 4;
112 SELECT subversion from plc_db_version;