Peer object now has hrn_root
[plcapi.git] / migrations / v4-to-v5 / 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 -- nodes
112 ----------------------------------------
113 ALTER TABLE nodes ADD COLUMN node_type TEXT NOT NULL DEFAULT 'regular';
114
115 ----------------------------------------
116 -- tag types
117 ----------------------------------------
118 --- merge former slice attribute types and setting attribute types into tagtypes
119
120 ---------- slice attributes
121
122 --- the tag_types table is obtained from the former slice_attribute_types table 
123 ALTER TABLE tag_types RENAME COLUMN name TO tagname;
124 --- former slice_attribute_types had no 'category'
125 ALTER TABLE tag_types ADD COLUMN category TEXT NOT NULL DEFAULT 'slice/legacy';
126
127 --- append in tag_types the contents of former nodenetwork_setting_types
128 INSERT INTO tag_types (tagname,description,min_role_id,category) 
129        SELECT name,description,min_role_id,'interface/legacy' FROM interface_tag_types;
130
131 ---------- interface settings
132
133 --- former nodenetwork_setting_type_id are now renumbered, need to fix interface_tag accordingly
134
135 -- old_index -> new_index relation
136 CREATE OR REPLACE VIEW mgn_setting_renumber AS
137    SELECT 
138       interface_tag_types.interface_tag_type_id AS old_index,   
139       tag_types.tag_type_id AS new_index 
140    FROM 
141       interface_tag_types INNER JOIN tag_types  
142       ON interface_tag_types.name = tag_types.tagname;
143
144 -- need to temporarily drop constraint on interface_tag_type_id
145 ALTER TABLE interface_tag DROP CONSTRAINT interface_tag_interface_tag_type_id_fkey;
146
147 -- do the transcoding
148 UPDATE interface_tag 
149    SET interface_tag_type_id = 
150       (select new_index from mgn_setting_renumber where old_index=interface_tag_type_id);
151
152 -- alter column name to reflect change
153 ALTER TABLE interface_tag RENAME interface_tag_type_id TO tag_type_id;
154
155 -- add constraint again
156 ALTER TABLE interface_tag ADD CONSTRAINT interface_tag_tag_type_id_fkey 
157     FOREIGN KEY (tag_type_id) references tag_types(tag_type_id) ;
158
159 -- drop former interface_tag_types altogether
160 drop view mgn_setting_renumber;
161 drop table interface_tag_types;
162
163 ---------- node tags
164
165 CREATE TABLE node_tag (
166     node_tag_id serial PRIMARY KEY,                     -- ID
167     node_id integer REFERENCES nodes NOT NULL,          -- node id
168     tag_type_id integer REFERENCES tag_types,           -- tag type id
169     value text                                          -- value attached
170 ) WITH OIDS;
171
172
173 ----------------------------------------
174 -- ilinks
175 ----------------------------------------
176 CREATE TABLE ilink (
177        ilink_id serial PRIMARY KEY,                             -- id
178        tag_type_id integer REFERENCES tag_types,                -- id of the tag type
179        src_interface_id integer REFERENCES interfaces not NULL, -- id of src interface
180        dst_interface_id integer REFERENCES interfaces NOT NULL, -- id of dst interface
181        value text                                               -- optional value on the link
182 ) WITH OIDS;
183
184 ----------------------------------------
185 -- nodegroups
186 ----------------------------------------
187
188 ---------- nodegroups table - start
189 -- nodegroup_id is preserved for conf_files and other references
190 -- former nodegroups table was (nodegroup_id,name,description)
191 -- new table is now            (nodegroup_id, groupname, tag_type_id, value)
192
193 -- rename column
194 ALTER TABLE nodegroups RENAME name TO groupname;
195
196 ---------- create missing tag types
197 -- change default for the entries about to be created
198 ALTER TABLE tag_types ALTER COLUMN category SET DEFAULT 'nodegroup/migration';
199
200 -- do it
201 INSERT INTO tag_types (tagname)
202    SELECT DISTINCT tagname FROM mgn_site_nodegroup 
203       WHERE tagname NOT IN (SELECT tagname from tag_types);
204
205 -- xxx drop description in former nodegroups for now, 
206 -- but could have been attached to newly created tag types first
207 ALTER TABLE nodegroups DROP COLUMN description;
208
209 ---------- set the right tags so as to recover former nodegroups
210 INSERT INTO node_tag (node_id, tag_type_id, value)
211    SELECT node_id, tag_type_id, value FROM
212       nodegroup_node LEFT JOIN nodegroups USING (nodegroup_id) 
213          INNER JOIN mgn_site_nodegroup USING (groupname)
214             LEFT JOIN tag_types using (tagname); 
215
216 ---------- nodegroups table - conclusion
217 ALTER TABLE nodegroups ADD COLUMN tag_type_id INTEGER;
218 ALTER TABLE nodegroups ADD COLUMN value TEXT;
219
220 CREATE OR REPLACE VIEW mgn_nodegroups AS
221    SELECT groupname, tag_types.tag_type_id, mgn_site_nodegroup.value 
222       FROM nodegroups INNER JOIN mgn_site_nodegroup USING (groupname) 
223          INNER JOIN tag_types USING (tagname);
224
225 UPDATE nodegroups SET tag_type_id = (SELECT tag_type_id FROM mgn_nodegroups WHERE nodegroups.groupname=mgn_nodegroups.groupname);
226 UPDATE nodegroups SET value = (SELECT value FROM mgn_nodegroups WHERE nodegroups.groupname=mgn_nodegroups.groupname);
227
228 -- install corresponding constraints
229 ALTER TABLE nodegroups ADD CONSTRAINT nodegroups_tag_type_id_fkey
230    FOREIGN KEY (tag_type_id) REFERENCES tag_types (tag_type_id);  
231
232 --- change default now that the column is filled
233 ALTER TABLE tag_types ALTER COLUMN category SET DEFAULT 'general';
234
235 -- cleanup the nodegroup area
236 drop view mgn_nodegroups;
237 drop table mgn_site_nodegroup;
238 drop table nodegroup_node;
239
240 ----------------------------------------
241 -- boot states
242 ----------------------------------------
243 -- create new ones
244 INSERT INTO boot_states (boot_state) VALUES ('safeboot');
245 INSERT INTO boot_states (boot_state) VALUES ('failboot');
246 INSERT INTO boot_states (boot_state) VALUES ('disabled');
247 INSERT INTO boot_states (boot_state) VALUES ('install');
248 INSERT INTO boot_states (boot_state) VALUES ('reinstall');
249
250 -- map old ones
251 UPDATE nodes SET boot_state='failboot' WHERE boot_state='dbg';
252 UPDATE nodes SET boot_state='safeboot' WHERE boot_state='diag';
253 UPDATE nodes SET boot_state='disabled' WHERE boot_state='disable';
254 UPDATE nodes SET boot_state='install' WHERE boot_state='inst';
255 UPDATE nodes SET boot_state='reinstall' WHERE boot_state='rins';
256 UPDATE nodes SET boot_state='reinstall' WHERE boot_state='new';
257 UPDATE nodes SET boot_state='failboot' WHERE boot_state='rcnf';
258
259 -- delete old ones
260 DELETE FROM boot_states WHERE boot_state='dbg';
261 DELETE FROM boot_states WHERE boot_state='diag';
262 DELETE FROM boot_states WHERE boot_state='disable';
263 DELETE FROM boot_states WHERE boot_state='inst';
264 DELETE FROM boot_states WHERE boot_state='rins';
265 DELETE FROM boot_states WHERE boot_state='new';
266 DELETE FROM boot_states WHERE boot_state='rcnf';
267
268 -- ----------------------------------------
269 -- -- debug/information : display current constraints
270 -- ----------------------------------------
271 -- CREATE OR REPLACE VIEW mgn_all_constraints AS
272 --     SELECT * FROM pg_catalog.pg_constraint c
273 --        LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.connamespace
274 --        LEFT JOIN pg_catalog.pg_class t ON t.oid = c.conrelid
275 --           WHERE c.contype IN ('c','f','p','u') AND n.nspname in ('public')
276 --           AND pg_catalog.pg_table_is_visible(c.oid);
277 -- 
278 -- select * from mgn_all_constraints;
279 --
280 -- drop view mgn_all_constraints;
281
282 --- versioning (plc_db_version): ignore for now, so we keep both entries (v4 and v5)