7ec440975a28e748ce29a3e53436bd7a324a5a6f
[plcapi.git] / migrations / v42-to-v43 / migrate.sql
1 -- Thierry Parmentelat - INRIA
2 -- 
3 -- $Id$
4 --
5 -- this is part of the script to migrate from 4.2 to 5.0
6 -- 
7 -- most of the renamings have taken place already when this script is invoked
8 --
9
10 ----------------------------------------
11 -- views
12 ----------------------------------------
13 -- we want the views to get out of our way, i.e. to drop all views; 
14 -- the views will be reinstantiated later upon loading of planetlab5.sql
15
16 -- this lists all views
17 CREATE OR REPLACE VIEW mgn_all_views AS
18     SELECT c.relname FROM pg_catalog.pg_class c
19        LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
20           WHERE c.relkind IN ('v','') AND n.nspname in ('public')
21              AND pg_catalog.pg_table_is_visible(c.oid);
22
23 -- shows in logfile
24 select * from mgn_all_views;
25
26 -- this one version almost works, but somehow does not, could not figure why
27 CREATE OR REPLACE FUNCTION mgn_drop_all_views () RETURNS INTEGER AS $$
28     DECLARE 
29         row mgn_all_views%ROWTYPE;
30     BEGIN
31         FOR row IN SELECT * FROM mgn_all_views where relname != 'mgn_all_views' LOOP
32            RAISE NOTICE 'Dropping %',row.relname;
33            EXECUTE 'DROP VIEW ' || row.relname || ' CASCADE' ;
34         END LOOP;
35         RETURN 0;       
36     END;
37 $$ LANGUAGE 'plpgsql';
38
39 -- SELECT mgn_drop_all_views();
40
41 -- so let's have it the boring way
42 DROP VIEW address_address_types CASCADE;
43 DROP VIEW conf_file_nodegroups CASCADE;
44 DROP VIEW conf_file_nodes CASCADE;
45 DROP VIEW dummybox_nodes CASCADE;
46 DROP VIEW event_objects CASCADE;
47 DROP VIEW node_conf_files CASCADE;
48 DROP VIEW node_nodegroups CASCADE;
49 DROP VIEW interfaces_ordered CASCADE;
50 -- caught by some previous cascade -- DROP VIEW node_interfaces CASCADE;
51 DROP VIEW node_pcus CASCADE;
52 DROP VIEW node_slices CASCADE;
53 DROP VIEW node_slices_whitelist CASCADE;
54 DROP VIEW nodegroup_conf_files CASCADE;
55 DROP VIEW nodegroup_nodes CASCADE;
56 DROP VIEW interface_tags CASCADE;
57 DROP VIEW pcu_nodes CASCADE;
58 DROP VIEW pcu_protocol_types CASCADE;
59 DROP VIEW peer_keys CASCADE;
60 DROP VIEW peer_nodes CASCADE;
61 DROP VIEW peer_persons CASCADE;
62 DROP VIEW peer_sites CASCADE;
63 DROP VIEW peer_slices CASCADE;
64 DROP VIEW person_keys CASCADE;
65 DROP VIEW person_roles CASCADE;
66 DROP VIEW person_site_ordered CASCADE;
67 -- caught by some previous cascade -- DROP VIEW person_sites CASCADE;
68 DROP VIEW person_slices CASCADE;
69 DROP VIEW site_addresses CASCADE;
70 DROP VIEW site_nodes CASCADE;
71 DROP VIEW site_pcus CASCADE;
72 DROP VIEW site_persons CASCADE;
73 DROP VIEW site_slices CASCADE;
74 DROP VIEW slice_tags CASCADE;
75 DROP VIEW slice_nodes CASCADE;
76 DROP VIEW slice_persons CASCADE;
77 DROP VIEW slivers CASCADE;
78 -- caught by some previous cascade -- DROP VIEW view_addresses CASCADE;
79 -- caught by some previous cascade -- DROP VIEW view_conf_files CASCADE;
80 -- caught by some previous cascade -- DROP VIEW view_dummyboxes CASCADE;
81 DROP VIEW view_event_objects CASCADE;
82 -- caught by some previous cascade -- DROP VIEW view_events CASCADE;
83 DROP VIEW view_keys CASCADE;
84 -- caught by some previous cascade -- DROP VIEW view_nodegroups CASCADE;
85 DROP VIEW view_interface_tags CASCADE;
86 -- caught by some previous cascade -- DROP VIEW view_interfaces CASCADE;
87 -- caught by some previous cascade -- DROP VIEW view_nodes CASCADE;
88 -- caught by some previous cascade -- DROP VIEW view_pcu_types CASCADE;
89 -- caught by some previous cascade -- DROP VIEW view_pcus CASCADE;
90 -- caught by some previous cascade -- DROP VIEW view_peers CASCADE;
91 -- caught by some previous cascade -- DROP VIEW view_persons CASCADE;
92 DROP VIEW view_sessions CASCADE;
93 -- caught by some previous cascade -- DROP VIEW view_sites CASCADE;
94 DROP VIEW view_slice_tags CASCADE;
95 -- caught by some previous cascade -- DROP VIEW view_slices CASCADE;
96
97 -- shows in logfile
98 select * from mgn_all_views;
99
100 -- cleanup migration utilities
101 drop view mgn_all_views;
102 drop function mgn_drop_all_views ();
103
104 ----------------------------------------
105 -- peers
106 ----------------------------------------
107 ALTER TABLE peers ADD COLUMN shortname TEXT;
108 ALTER TABLE peers ADD COLUMN hrn_root TEXT;
109
110
111 ----------------------------------------
112 -- run levels
113 ----------------------------------------
114 CREATE TABLE run_levels  (
115     run_level text PRIMARY KEY
116 ) WITH OIDS;
117 INSERT INTO run_levels  (run_level) VALUES ('boot');
118 INSERT INTO run_levels  (run_level) VALUES ('safeboot');
119 INSERT INTO run_levels  (run_level) VALUES ('failboot');
120 INSERT INTO run_levels  (run_level) VALUES ('reinstall');
121
122
123 ----------------------------------------
124 -- node types
125 ----------------------------------------
126 CREATE TABLE node_types (
127     node_type text PRIMARY KEY
128 ) WITH OIDS;
129 INSERT INTO node_types (node_type) VALUES ('regular');
130 INSERT INTO node_types (node_type) VALUES ('dummynet');
131
132 ----------------------------------------
133 -- nodes
134 ----------------------------------------
135 ALTER TABLE nodes ADD COLUMN node_type TEXT REFERENCES node_types  DEFAULT 'regular'; -- node types
136 ALTER TABLE nodes ALTER COLUMN boot_state SET DEFAULT 'reinstall'; -- boot_states changed in planetlab5.sql
137 ALTER TABLE nodes ADD COLUMN verified boolean NOT NULL DEFAULT false;   -- whether or not the node & pcu are verified
138 ALTER TABLE nodes ADD COLUMN run_level TEXT REFERENCES run_levels DEFAULT NULL; -- Node Run Level
139
140 ----------------------------------------
141 -- tag types
142 ----------------------------------------
143 --- merge former slice attribute types and setting attribute types into tagtypes
144
145 ---------- slice attributes
146
147 --- the tag_types table is obtained from the former slice_attribute_types table 
148 ALTER TABLE tag_types RENAME COLUMN name TO tagname;
149 --- former slice_attribute_types had no 'category'
150 ALTER TABLE tag_types ADD COLUMN category TEXT NOT NULL DEFAULT 'slice/legacy';
151
152 --- append in tag_types the contents of former nodenetwork_setting_types
153 INSERT INTO tag_types (tagname,description,min_role_id,category) 
154        SELECT name,description,min_role_id,'interface/legacy' FROM interface_tag_types;
155
156 ---------- interface settings
157
158 --- former nodenetwork_setting_type_id are now renumbered, need to fix interface_tag accordingly
159
160 -- old_index -> new_index relation
161 CREATE OR REPLACE VIEW mgn_setting_renumber AS
162    SELECT 
163       interface_tag_types.interface_tag_type_id AS old_index,   
164       tag_types.tag_type_id AS new_index 
165    FROM 
166       interface_tag_types INNER JOIN tag_types  
167       ON interface_tag_types.name = tag_types.tagname;
168
169 -- need to temporarily drop constraint on interface_tag_type_id
170 ALTER TABLE interface_tag DROP CONSTRAINT interface_tag_interface_tag_type_id_fkey;
171
172 -- do the transcoding
173 UPDATE interface_tag 
174    SET interface_tag_type_id = 
175       (select new_index from mgn_setting_renumber where old_index=interface_tag_type_id);
176
177 -- alter column name to reflect change
178 ALTER TABLE interface_tag RENAME interface_tag_type_id TO tag_type_id;
179
180 -- add constraint again
181 ALTER TABLE interface_tag ADD CONSTRAINT interface_tag_tag_type_id_fkey 
182     FOREIGN KEY (tag_type_id) references tag_types(tag_type_id) ;
183
184 -- drop former interface_tag_types altogether
185 drop view mgn_setting_renumber;
186 drop table interface_tag_types;
187
188 ---------- node tags
189
190 CREATE TABLE node_tag (
191     node_tag_id serial PRIMARY KEY,                     -- ID
192     node_id integer REFERENCES nodes NOT NULL,          -- node id
193     tag_type_id integer REFERENCES tag_types,           -- tag type id
194     value text                                          -- value attached
195 ) WITH OIDS;
196
197
198 ----------------------------------------
199 -- ilinks
200 ----------------------------------------
201 CREATE TABLE ilink (
202        ilink_id serial PRIMARY KEY,                             -- id
203        tag_type_id integer REFERENCES tag_types,                -- id of the tag type
204        src_interface_id integer REFERENCES interfaces not NULL, -- id of src interface
205        dst_interface_id integer REFERENCES interfaces NOT NULL, -- id of dst interface
206        value text                                               -- optional value on the link
207 ) WITH OIDS;
208
209 ----------------------------------------
210 -- nodegroups
211 ----------------------------------------
212
213 ---------- nodegroups table - start
214 -- nodegroup_id is preserved for conf_files and other references
215 -- former nodegroups table was (nodegroup_id,name,description)
216 -- new table is now            (nodegroup_id, groupname, tag_type_id, value)
217
218 -- rename column
219 ALTER TABLE nodegroups RENAME name TO groupname;
220
221 ---------- create missing tag types
222 -- change default for the entries about to be created
223 ALTER TABLE tag_types ALTER COLUMN category SET DEFAULT 'nodegroup/migration';
224
225 -- do it
226 INSERT INTO tag_types (tagname)
227    SELECT DISTINCT tagname FROM mgn_site_nodegroup 
228       WHERE tagname NOT IN (SELECT tagname from tag_types);
229
230 -- xxx drop description in former nodegroups for now, 
231 -- but could have been attached to newly created tag types first
232 ALTER TABLE nodegroups DROP COLUMN description;
233
234 ---------- set the right tags so as to recover former nodegroups
235 INSERT INTO node_tag (node_id, tag_type_id, value)
236    SELECT node_id, tag_type_id, value FROM
237       nodegroup_node LEFT JOIN nodegroups USING (nodegroup_id) 
238          INNER JOIN mgn_site_nodegroup USING (groupname)
239             LEFT JOIN tag_types using (tagname); 
240
241 ---------- nodegroups table - conclusion
242 ALTER TABLE nodegroups ADD COLUMN tag_type_id INTEGER;
243 ALTER TABLE nodegroups ADD COLUMN value TEXT;
244
245 CREATE OR REPLACE VIEW mgn_nodegroups AS
246    SELECT groupname, tag_types.tag_type_id, mgn_site_nodegroup.value 
247       FROM nodegroups INNER JOIN mgn_site_nodegroup USING (groupname) 
248          INNER JOIN tag_types USING (tagname);
249
250 UPDATE nodegroups SET tag_type_id = (SELECT tag_type_id FROM mgn_nodegroups WHERE nodegroups.groupname=mgn_nodegroups.groupname);
251 UPDATE nodegroups SET value = (SELECT value FROM mgn_nodegroups WHERE nodegroups.groupname=mgn_nodegroups.groupname);
252
253 -- install corresponding constraints
254 ALTER TABLE nodegroups ADD CONSTRAINT nodegroups_tag_type_id_fkey
255    FOREIGN KEY (tag_type_id) REFERENCES tag_types (tag_type_id);  
256
257 --- change default now that the column is filled
258 ALTER TABLE tag_types ALTER COLUMN category SET DEFAULT 'general';
259
260 -- cleanup the nodegroup area
261 drop view mgn_nodegroups;
262 drop table mgn_site_nodegroup;
263 drop table nodegroup_node;
264
265
266 ----------------------------------------
267 -- boot states
268 ----------------------------------------
269 -- create new ones
270 INSERT INTO boot_states (boot_state) VALUES ('safeboot');
271 INSERT INTO boot_states (boot_state) VALUES ('failboot');
272 INSERT INTO boot_states (boot_state) VALUES ('disabled');
273 INSERT INTO boot_states (boot_state) VALUES ('reinstall');
274
275 -- map old ones
276 -- b/c boot_states are declared by users not reported by node.
277 UPDATE nodes SET boot_state='safeboot' WHERE boot_state='dbg';  
278 UPDATE nodes SET boot_state='safeboot' WHERE boot_state='diag';
279 UPDATE nodes SET boot_state='disabled' WHERE boot_state='disable';
280 UPDATE nodes SET boot_state='reinstall' WHERE boot_state='inst';
281 UPDATE nodes SET boot_state='reinstall' WHERE boot_state='rins';
282 UPDATE nodes SET boot_state='reinstall' WHERE boot_state='new';
283 UPDATE nodes SET boot_state='reinstall' WHERE boot_state='rcnf';
284
285 -- delete old ones
286 DELETE FROM boot_states WHERE boot_state='dbg';
287 DELETE FROM boot_states WHERE boot_state='diag';
288 DELETE FROM boot_states WHERE boot_state='disable';
289 DELETE FROM boot_states WHERE boot_state='install';
290 DELETE FROM boot_states WHERE boot_state='inst';
291 DELETE FROM boot_states WHERE boot_state='rins';
292 DELETE FROM boot_states WHERE boot_state='new';
293 DELETE FROM boot_states WHERE boot_state='rcnf';
294
295 -- ----------------------------------------
296 -- -- debug/information : display current constraints
297 -- ----------------------------------------
298 -- CREATE OR REPLACE VIEW mgn_all_constraints AS
299 --     SELECT * FROM pg_catalog.pg_constraint c
300 --        LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.connamespace
301 --        LEFT JOIN pg_catalog.pg_class t ON t.oid = c.conrelid
302 --           WHERE c.contype IN ('c','f','p','u') AND n.nspname in ('public')
303 --           AND pg_catalog.pg_table_is_visible(c.oid);
304 -- 
305 -- select * from mgn_all_constraints;
306 --
307 -- drop view mgn_all_constraints;
308
309 --- versioning (plc_db_version): ignore for now, so we keep both entries (v4 and v5)