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