fixed: encodes boolean and double type values for the requests
[plcapi.git] / migrations / 104-up-noderole.sql
1 -- changing the permission model on tags
2 -- we replace the single 'min_role_id' field attached to tag_types
3 -- with a set of roles
4
5
6 -- create a separate table to keep the tag-type x role relationship
7 CREATE TABLE tag_type_role (
8     tag_type_id integer REFERENCES tag_types NOT NULL,  -- tag_type ID
9     role_id integer REFERENCES roles NOT NULL,          -- role ID
10     PRIMARY KEY (tag_type_id, role_id)
11 );
12 CREATE INDEX tag_type_role_tag_type_id_idx ON tag_type_role (tag_type_id);
13 CREATE INDEX tag_type_role_role_id_idx ON tag_type_role (role_id);
14
15 -- fill this from the former min_role_id field in the tag_types table
16 -- add all roles lower or equal to the min_role_id
17 INSERT INTO tag_type_role ("tag_type_id","role_id") SELECT tag_type_id,role_id FROM tag_types,roles where role_id<=min_role_id;
18
19 -- we can now drop the min_role_id column
20 ALTER TABLE tag_types DROP COLUMN min_role_id CASCADE;
21
22 -- create views to expose roles
23 CREATE OR REPLACE VIEW tag_type_roles AS
24 SELECT tag_type_id,
25 array_accum(role_id) AS role_ids,
26 array_accum(roles.name) AS roles
27 FROM tag_type_role 
28 LEFT JOIN roles USING (role_id)
29 GROUP BY tag_type_id;
30
31 CREATE OR REPLACE VIEW view_tag_types AS
32 SELECT 
33 tag_types.tag_type_id,
34 tag_types.tagname,
35 tag_types.description,
36 tag_types.category,
37 COALESCE((SELECT role_ids FROM tag_type_roles WHERE tag_type_roles.tag_type_id = tag_types.tag_type_id), '{}') AS role_ids,
38 COALESCE((SELECT roles FROM tag_type_roles WHERE tag_type_roles.tag_type_id = tag_types.tag_type_id), '{}') AS roles
39 FROM tag_types; 
40
41
42 -- remove min_role_id from the object views
43 CREATE OR REPLACE VIEW view_person_tags AS
44 SELECT
45 person_tag.person_tag_id,
46 person_tag.person_id,
47 persons.email,
48 tag_types.tag_type_id,
49 tag_types.tagname,
50 tag_types.description,
51 tag_types.category,
52 person_tag.value
53 FROM person_tag 
54 INNER JOIN tag_types USING (tag_type_id)
55 INNER JOIN persons USING (person_id);
56
57 CREATE OR REPLACE VIEW view_site_tags AS
58 SELECT
59 site_tag.site_tag_id,
60 site_tag.site_id,
61 sites.login_base,
62 tag_types.tag_type_id,
63 tag_types.tagname,
64 tag_types.description,
65 tag_types.category,
66 site_tag.value
67 FROM site_tag 
68 INNER JOIN tag_types USING (tag_type_id)
69 INNER JOIN sites USING (site_id);
70
71 CREATE OR REPLACE VIEW view_interface_tags AS
72 SELECT
73 interface_tag.interface_tag_id,
74 interface_tag.interface_id,
75 interfaces.ip,
76 tag_types.tag_type_id,
77 tag_types.tagname,
78 tag_types.description,
79 tag_types.category,
80 interface_tag.value
81 FROM interface_tag
82 INNER JOIN tag_types USING (tag_type_id)
83 INNER JOIN interfaces USING (interface_id);
84
85 CREATE OR REPLACE VIEW view_node_tags AS
86 SELECT
87 node_tag.node_tag_id,
88 node_tag.node_id,
89 nodes.hostname,
90 tag_types.tag_type_id,
91 tag_types.tagname,
92 tag_types.description,
93 tag_types.category,
94 node_tag.value
95 FROM node_tag 
96 INNER JOIN tag_types USING (tag_type_id)
97 INNER JOIN nodes USING (node_id);
98
99 CREATE OR REPLACE VIEW view_slice_tags AS
100 SELECT
101 slice_tag.slice_tag_id,
102 slice_tag.slice_id,
103 slice_tag.node_id,
104 slice_tag.nodegroup_id,
105 tag_types.tag_type_id,
106 tag_types.tagname,
107 tag_types.description,
108 tag_types.category,
109 slice_tag.value,
110 slices.name
111 FROM slice_tag
112 INNER JOIN tag_types USING (tag_type_id)
113 INNER JOIN slices USING (slice_id);
114
115 -- same for ilinks
116 CREATE OR REPLACE VIEW view_ilinks AS
117 SELECT * FROM tag_types 
118 INNER JOIN ilink USING (tag_type_id);
119
120 -- use this to allow nodes to set slice tags
121 INSERT INTO roles (role_id, name) VALUES (50, 'node');
122
123 --------------------
124 UPDATE plc_db_version SET subversion = 104;