Skip to content

Commit 38fe53c

Browse files
committed
Added a SQL script for adding snapshot action permission to older concept sets (which were created before the lock feature was introduced with a new permission per concept set entity)
1 parent e1b10dd commit 38fe53c

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

src/main/resources/db/migration/postgresql/V2.15.0.20250220000001__webapi_snapshots_schema_init.sql

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,62 @@ CREATE TABLE ${ohdsiSchema}.INCLUDED_SOURCE_CODES_SNAPSHOTS (
8080
VALID_END_DATE DATE NOT NULL,
8181
INVALID_REASON VARCHAR(1),
8282
FOREIGN KEY (SNAPSHOT_METADATA_ID) REFERENCES ${ohdsiSchema}.CONCEPT_SET_SNAPSHOT_METADATA(ID)
83-
);
83+
);
84+
85+
-- This script inserts a new permission of type "conceptset:%s:snapshot:post" and maps it to the existing roles if such permission does not exist
86+
--for each concept set which has a "conceptset:%s:put" permission. This is made to allow making snapshot actions to old concept sets
87+
--which were created before the snapshot/lock feature was deployed
88+
89+
DO $$
90+
DECLARE
91+
permission RECORD;
92+
new_permission_id INTEGER;
93+
new_permission_value VARCHAR;
94+
new_role_permission_id INTEGER;
95+
BEGIN
96+
FOR permission IN
97+
SELECT p.id AS permission_id, p.value AS permission_value, rp.role_id AS role_id
98+
FROM ${ohdsiSchema}.sec_permission p
99+
INNER JOIN ${ohdsiSchema}.sec_role_permission rp
100+
ON p.id = rp.permission_id
101+
WHERE p.value ~ '^conceptset:[0-9]+:put$'
102+
LOOP
103+
new_permission_value := 'conceptset:' || split_part(permission.permission_value, ':', 2) || ':snapshot:post';
104+
105+
IF NOT EXISTS (
106+
SELECT 1
107+
FROM ${ohdsiSchema}.sec_permission
108+
WHERE "value" = new_permission_value
109+
)
110+
THEN
111+
new_permission_id := nextval('${ohdsiSchema}.sec_permission_id_seq');
112+
113+
INSERT INTO ${ohdsiSchema}.sec_permission (id, value, description)
114+
VALUES (new_permission_id, new_permission_value, 'Permission to create snapshot for concept set');
115+
116+
RAISE NOTICE 'Inserted New Permission: % (ID: %)', new_permission_value, new_permission_id;
117+
ELSE
118+
SELECT id INTO new_permission_id
119+
FROM ${ohdsiSchema}.sec_permission
120+
WHERE "value" = new_permission_value;
121+
122+
RAISE NOTICE 'Permission Already Exists: % (ID: %)', new_permission_value, new_permission_id;
123+
END IF;
124+
125+
IF NOT EXISTS (
126+
SELECT 1
127+
FROM ${ohdsiSchema}.sec_role_permission
128+
WHERE role_id = permission.role_id AND permission_id = new_permission_id
129+
)
130+
THEN
131+
new_role_permission_id := nextval('${ohdsiSchema}.sec_role_permission_sequence');
132+
133+
INSERT INTO ${ohdsiSchema}.sec_role_permission (id, role_id, permission_id)
134+
VALUES (new_role_permission_id, permission.role_id, new_permission_id);
135+
136+
RAISE NOTICE 'Mapped New Permission to Role: % (Role ID: %)', new_permission_value, permission.role_id;
137+
ELSE
138+
RAISE NOTICE 'Mapping Already Exists: Permission: % - Role ID: %', new_permission_value, permission.role_id;
139+
END IF;
140+
END LOOP;
141+
END $$;

0 commit comments

Comments
 (0)