75a68865ddf0a1232e4bbeffb7a76ce8e3990b55
[plcapi.git] / migrations / migrate-v4-to-v5.sql
1 --
2 -- $Id$
3 --
4 -- this is part of the script to migrate from 4.2 to 5.0
5 --
6
7 -------------------- VIEWS :
8 -- we want the views to get out of our way, i.e. to drop all views; 
9 -- the views will be reinstantiated later upon loading of planetlab5.sql
10
11 -- this lists all views
12 CREATE OR REPLACE VIEW all_views AS
13     SELECT c.relname FROM pg_catalog.pg_class c
14        LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
15           WHERE c.relkind IN ('v','') AND n.nspname in ('public')
16              AND pg_catalog.pg_table_is_visible(c.oid);
17
18 -- shows in logfile
19 select * from all_views;
20
21 -- this one version almost works, but somehow does not, could not figure why
22 CREATE OR REPLACE FUNCTION drop_all_views () RETURNS INTEGER AS $$
23     DECLARE 
24         row all_views%ROWTYPE;
25     BEGIN
26         FOR row IN SELECT * FROM all_views where relname != 'all_views' LOOP
27            RAISE NOTICE 'Dropping %',row.relname;
28            EXECUTE 'DROP VIEW ' || row.relname || ' CASCADE' ;
29         END LOOP;
30         RETURN 0;       
31     END;
32 $$ LANGUAGE 'plpgsql';
33
34 -- SELECT drop_all_views();
35
36 -- so let's have it the boring way
37 DROP VIEW address_address_types CASCADE;
38 DROP VIEW conf_file_nodegroups CASCADE;
39 DROP VIEW conf_file_nodes CASCADE;
40 DROP VIEW dummybox_nodes CASCADE;
41 DROP VIEW event_objects CASCADE;
42 DROP VIEW node_conf_files CASCADE;
43 DROP VIEW node_nodegroups CASCADE;
44 DROP VIEW interfaces_ordered CASCADE;
45 -- caught by some previous cascade -- DROP VIEW node_interfaces CASCADE;
46 DROP VIEW node_pcus CASCADE;
47 DROP VIEW node_slices CASCADE;
48 DROP VIEW node_slices_whitelist CASCADE;
49 DROP VIEW nodegroup_conf_files CASCADE;
50 DROP VIEW nodegroup_nodes CASCADE;
51 DROP VIEW interface_settings CASCADE;
52 DROP VIEW pcu_nodes CASCADE;
53 DROP VIEW pcu_protocol_types CASCADE;
54 DROP VIEW peer_keys CASCADE;
55 DROP VIEW peer_nodes CASCADE;
56 DROP VIEW peer_persons CASCADE;
57 DROP VIEW peer_sites CASCADE;
58 DROP VIEW peer_slices CASCADE;
59 DROP VIEW person_keys CASCADE;
60 DROP VIEW person_roles CASCADE;
61 DROP VIEW person_site_ordered CASCADE;
62 -- caught by some previous cascade -- DROP VIEW person_sites CASCADE;
63 DROP VIEW person_slices CASCADE;
64 DROP VIEW site_addresses CASCADE;
65 DROP VIEW site_nodes CASCADE;
66 DROP VIEW site_pcus CASCADE;
67 DROP VIEW site_persons CASCADE;
68 DROP VIEW site_slices CASCADE;
69 DROP VIEW slice_attributes CASCADE;
70 DROP VIEW slice_nodes CASCADE;
71 DROP VIEW slice_persons CASCADE;
72 DROP VIEW slivers CASCADE;
73 -- caught by some previous cascade -- DROP VIEW view_addresses CASCADE;
74 -- caught by some previous cascade -- DROP VIEW view_conf_files CASCADE;
75 -- caught by some previous cascade -- DROP VIEW view_dummyboxes CASCADE;
76 DROP VIEW view_event_objects CASCADE;
77 -- caught by some previous cascade -- DROP VIEW view_events CASCADE;
78 DROP VIEW view_keys CASCADE;
79 -- caught by some previous cascade -- DROP VIEW view_nodegroups CASCADE;
80 DROP VIEW view_interface_settings CASCADE;
81 -- caught by some previous cascade -- DROP VIEW view_interfaces CASCADE;
82 -- caught by some previous cascade -- DROP VIEW view_nodes CASCADE;
83 -- caught by some previous cascade -- DROP VIEW view_pcu_types CASCADE;
84 -- caught by some previous cascade -- DROP VIEW view_pcus CASCADE;
85 -- caught by some previous cascade -- DROP VIEW view_peers CASCADE;
86 -- caught by some previous cascade -- DROP VIEW view_persons CASCADE;
87 DROP VIEW view_sessions CASCADE;
88 -- caught by some previous cascade -- DROP VIEW view_sites CASCADE;
89 DROP VIEW view_slice_attributes CASCADE;
90 -- caught by some previous cascade -- DROP VIEW view_slices CASCADE;
91
92 -- shows in logfile
93 select * from all_views;
94
95
96 -------------------- TAG TYPES
97 --- merge former slice attribute types and setting attribute types into tagtypes
98
99 --- the tag_types table is obtained from the former slice_attribute_types table 
100 ALTER TABLE tag_types RENAME COLUMN name TO tagname;
101 --- former slice_attribute_types had no 'category'
102 ALTER TABLE tag_types ADD COLUMN category TEXT NOT NULL DEFAULT 'slice/legacy';
103 --- change default now that the column is filled
104 ALTER TABLE tag_types ALTER COLUMN category SET DEFAULT 'general';
105
106 --- append in tag_types the contents of nodenetwork_setting_types
107 insert into tag_types (tagname,description,min_role_id,category) select name,description,min_role_id,'interface/legacy' from interface_setting_types;
108
109 --- former nodenetwork_setting_type_id are now renumbered, need to fix interface_setting accordingly
110
111 -- old_index -> new_index relation
112 CREATE OR REPLACE VIEW index_renumber AS
113    SELECT 
114       interface_setting_types.interface_setting_type_id AS old_index,   
115       tag_types.tag_type_id AS new_index 
116    FROM 
117       interface_setting_types INNER JOIN tag_types  
118       ON interface_setting_types.name = tag_types.tagname;
119
120 -- need to temporarily drop constraint on interface_setting_type_id
121 ALTER TABLE interface_setting DROP CONSTRAINT interface_setting_interface_setting_type_id_fkey;
122
123 -- do the transcoding
124 UPDATE interface_setting 
125    SET interface_setting_type_id = 
126       (select new_index from index_renumber where old_index=interface_setting_type_id);
127
128 -- alter column nam to reflect change
129 ALTER TABLE interface_setting RENAME interface_setting_type_id TO tag_type_id;
130
131 -- add contraint again
132 ALTER TABLE interface_setting ADD CONSTRAINT interface_setting_tag_type_id_fkey 
133     FOREIGN KEY (tag_type_id) references tag_types(tag_type_id) ;
134
135 --- cleanup
136 drop view index_renumber;
137
138 -- drop former interface_setting_types altogether
139 drop table interface_setting_types;
140
141 -------------------- NEW STUFF
142
143 CREATE TABLE ilink (
144        ilink_id serial PRIMARY KEY,                             -- id
145        tag_type_id integer REFERENCES tag_types,                -- id of the tag type
146        src_interface_id integer REFERENCES interfaces not NULL, -- id of src interface
147        dst_interface_id integer REFERENCES interfaces NOT NULL, -- id of dst interface
148        value text                                               -- optional value on the link
149 ) WITH OIDS;
150
151
152 CREATE TABLE node_tag (
153     node_tag_id serial PRIMARY KEY,                     -- ID
154     node_id integer REFERENCES nodes NOT NULL,          -- node id
155     tag_type_id integer REFERENCES tag_types,           -- tag type id
156     tagvalue text                                       -- value attached
157 ) WITH OIDS;
158
159 ----------------------------------------
160 -- nodegroups
161 ----------------------------------------
162 --- xxx - need to capture this first
163 --- xxx would dump some python script to capture current nodegroups...
164
165 --- xxx would maybe like to preserve it in nodegroups_v4 or something
166 DROP TABLE IF EXISTS nodegroups CASCADE;
167 DROP TABLE IF EXISTS nodegroup_node CASCADE;
168
169 CREATE TABLE nodegroups (
170     nodegroup_id serial PRIMARY KEY,            -- Group identifier
171     groupname text UNIQUE NOT NULL,             -- Group name 
172     tag_type_id integer REFERENCES tag_types,   -- node is in nodegroup if it has this tag defined
173     tagvalue text NOT NULL                      -- with this value attached
174 ) WITH OIDS;
175
176 -------------
177 -- display constraints
178
179 CREATE OR REPLACE VIEW all_constraints AS
180     SELECT * FROM pg_catalog.pg_constraint c
181        LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.connamespace
182        LEFT JOIN pg_catalog.pg_class t ON t.oid = c.conrelid
183           WHERE c.contype IN ('c','f','p','u') AND n.nspname in ('public')
184              AND pg_catalog.pg_table_is_visible(c.oid);
185
186 -- cleanup
187 drop view all_views;
188
189 --- versioning (plc_db_version): ignore for now, so we keep both entries (v4 and v5)