Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/conversion/sakai_25_1-25_2_mysql_conversion.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- SAK-51713 --
-- Permission added might not be present
INSERT IGNORE INTO SAKAI_REALM_FUNCTION (FUNCTION_NAME) VALUES ('assessment.all.groups');
-- Add this for every role able to create and manage conditions on a site, you'll need to add the tool too
INSERT INTO SAKAI_REALM_RL_FN VALUES((select REALM_KEY from SAKAI_REALM where REALM_ID = '!site.template'), (select ROLE_KEY from SAKAI_REALM_ROLE where ROLE_NAME = 'maintain'), (select FUNCTION_KEY from SAKAI_REALM_FUNCTION where FUNCTION_NAME = 'assessment.all.groups'));
INSERT INTO SAKAI_REALM_RL_FN VALUES((select REALM_KEY from SAKAI_REALM where REALM_ID = '!site.template.course'), (select ROLE_KEY from SAKAI_REALM_ROLE where ROLE_NAME = 'Instructor'), (select FUNCTION_KEY from SAKAI_REALM_FUNCTION where FUNCTION_NAME = 'assessment.all.groups'));
Comment on lines +5 to +6
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Template inserts lack idempotency and may fail on re-run.

These direct INSERT statements will fail with a duplicate key error if the script runs a second time (or if the permission already exists). Additionally, if any subquery returns NULL (e.g., role or realm not found), the insert will either fail or insert invalid data.

Consider using INSERT IGNORE or wrapping with a NOT EXISTS check to match the idempotent pattern used in the bulk insert below.

🔎 Suggested fix using INSERT IGNORE:
 -- Add this for every role able to create and manage conditions on a site, you'll need to add the tool too
-INSERT INTO SAKAI_REALM_RL_FN VALUES((select REALM_KEY from SAKAI_REALM where REALM_ID = '!site.template'), (select ROLE_KEY from SAKAI_REALM_ROLE where ROLE_NAME = 'maintain'), (select FUNCTION_KEY from SAKAI_REALM_FUNCTION where FUNCTION_NAME = 'assessment.all.groups'));
-INSERT INTO SAKAI_REALM_RL_FN VALUES((select REALM_KEY from SAKAI_REALM where REALM_ID = '!site.template.course'), (select ROLE_KEY from SAKAI_REALM_ROLE where ROLE_NAME = 'Instructor'), (select FUNCTION_KEY from SAKAI_REALM_FUNCTION where FUNCTION_NAME = 'assessment.all.groups'));
+INSERT IGNORE INTO SAKAI_REALM_RL_FN VALUES((select REALM_KEY from SAKAI_REALM where REALM_ID = '!site.template'), (select ROLE_KEY from SAKAI_REALM_ROLE where ROLE_NAME = 'maintain'), (select FUNCTION_KEY from SAKAI_REALM_FUNCTION where FUNCTION_NAME = 'assessment.all.groups'));
+INSERT IGNORE INTO SAKAI_REALM_RL_FN VALUES((select REALM_KEY from SAKAI_REALM where REALM_ID = '!site.template.course'), (select ROLE_KEY from SAKAI_REALM_ROLE where ROLE_NAME = 'Instructor'), (select FUNCTION_KEY from SAKAI_REALM_FUNCTION where FUNCTION_NAME = 'assessment.all.groups'));
🤖 Prompt for AI Agents
In docs/conversion/sakai_25_1-25_2_mysql_conversion.sql around lines 5 to 6, the
two direct INSERT statements are not idempotent and will error on duplicate keys
or insert NULLs if the subqueries return no rows; change them to perform
conditional inserts (e.g., INSERT IGNORE ... SELECT ... or INSERT ... SELECT ...
WHERE NOT EXISTS (...) ) and ensure the SELECTs only run when the subquery
values are non-NULL (add WHERE clauses checking existence of the realm, role and
function keys or join to those tables instead of scalar subqueries) so
re-running the script is safe and no invalid rows are inserted.

-- Add this to populate existing sites with the permission
CREATE TABLE PERMISSIONS_SRC_TEMP (ROLE_NAME VARCHAR(99), FUNCTION_NAME VARCHAR(99));
INSERT INTO PERMISSIONS_SRC_TEMP values ('maintain','assessment.all.groups');
INSERT INTO PERMISSIONS_SRC_TEMP values ('Instructor','assessment.all.groups');

CREATE TABLE PERMISSIONS_TEMP (ROLE_KEY INTEGER, FUNCTION_KEY INTEGER);
INSERT INTO PERMISSIONS_TEMP (ROLE_KEY, FUNCTION_KEY)
SELECT SRR.ROLE_KEY, SRF.FUNCTION_KEY
from PERMISSIONS_SRC_TEMP TMPSRC
JOIN SAKAI_REALM_ROLE SRR ON (TMPSRC.ROLE_NAME = SRR.ROLE_NAME)
JOIN SAKAI_REALM_FUNCTION SRF ON (TMPSRC.FUNCTION_NAME = SRF.FUNCTION_NAME);

INSERT INTO SAKAI_REALM_RL_FN (REALM_KEY, ROLE_KEY, FUNCTION_KEY)
SELECT SRRFD.REALM_KEY, SRRFD.ROLE_KEY, TMP.FUNCTION_KEY
FROM
(SELECT DISTINCT SRRF.REALM_KEY, SRRF.ROLE_KEY FROM SAKAI_REALM_RL_FN SRRF) SRRFD
JOIN PERMISSIONS_TEMP TMP ON (SRRFD.ROLE_KEY = TMP.ROLE_KEY)
JOIN SAKAI_REALM SR ON (SRRFD.REALM_KEY = SR.REALM_KEY)
WHERE SR.REALM_ID != '!site.helper' AND SR.REALM_ID NOT LIKE '!user.template%'
AND NOT EXISTS (
SELECT 1
FROM SAKAI_REALM_RL_FN SRRFI
WHERE SRRFI.REALM_KEY=SRRFD.REALM_KEY AND SRRFI.ROLE_KEY=SRRFD.ROLE_KEY AND SRRFI.FUNCTION_KEY=TMP.FUNCTION_KEY
);

DROP TABLE PERMISSIONS_TEMP;
DROP TABLE PERMISSIONS_SRC_TEMP;
-- END SAK-51713 --
43 changes: 43 additions & 0 deletions docs/conversion/sakai_25_1-25_2_oracle_conversion.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
-- SAK-51713 --
-- Permission added might not be present
MERGE INTO SAKAI_REALM_FUNCTION srf
USING (
SELECT -123 as function_key,
'assessment.all.groups' as function_name
FROM dual
) t on (srf.function_name = t.function_name)
WHEN NOT MATCHED THEN
INSERT (function_key, function_name)
VALUES (SAKAI_REALM_FUNCTION_SEQ.NEXTVAL, t.function_name);
-- Add this for every role able to create and manage conditions on a site, you'll need to add the tool too
INSERT INTO SAKAI_REALM_RL_FN VALUES((select REALM_KEY from SAKAI_REALM where REALM_ID = '!site.template'), (select ROLE_KEY from SAKAI_REALM_ROLE where ROLE_NAME = 'maintain'), (select FUNCTION_KEY from SAKAI_REALM_FUNCTION where FUNCTION_NAME = 'assessment.all.groups'));
INSERT INTO SAKAI_REALM_RL_FN VALUES((select REALM_KEY from SAKAI_REALM where REALM_ID = '!site.template.course'), (select ROLE_KEY from SAKAI_REALM_ROLE where ROLE_NAME = 'Instructor'), (select FUNCTION_KEY from SAKAI_REALM_FUNCTION where FUNCTION_NAME = 'assessment.all.groups'));
Comment on lines +13 to +14
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Template inserts lack idempotency and may fail on re-run.

Same issue as the MySQL version: these will fail with a constraint violation if run twice. Oracle doesn't have INSERT IGNORE, so consider using MERGE or wrapping each insert with a NOT EXISTS check.

🔎 Suggested fix using MERGE:
 -- Add this for every role able to create and manage conditions on a site, you'll need to add the tool too
-INSERT INTO SAKAI_REALM_RL_FN VALUES((select REALM_KEY from SAKAI_REALM where REALM_ID = '!site.template'), (select ROLE_KEY from SAKAI_REALM_ROLE where ROLE_NAME = 'maintain'), (select FUNCTION_KEY from SAKAI_REALM_FUNCTION where FUNCTION_NAME = 'assessment.all.groups'));
-INSERT INTO SAKAI_REALM_RL_FN VALUES((select REALM_KEY from SAKAI_REALM where REALM_ID = '!site.template.course'), (select ROLE_KEY from SAKAI_REALM_ROLE where ROLE_NAME = 'Instructor'), (select FUNCTION_KEY from SAKAI_REALM_FUNCTION where FUNCTION_NAME = 'assessment.all.groups'));
+MERGE INTO SAKAI_REALM_RL_FN t
+USING (
+    SELECT 
+        (SELECT REALM_KEY FROM SAKAI_REALM WHERE REALM_ID = '!site.template') AS REALM_KEY,
+        (SELECT ROLE_KEY FROM SAKAI_REALM_ROLE WHERE ROLE_NAME = 'maintain') AS ROLE_KEY,
+        (SELECT FUNCTION_KEY FROM SAKAI_REALM_FUNCTION WHERE FUNCTION_NAME = 'assessment.all.groups') AS FUNCTION_KEY
+    FROM dual
+) s ON (t.REALM_KEY = s.REALM_KEY AND t.ROLE_KEY = s.ROLE_KEY AND t.FUNCTION_KEY = s.FUNCTION_KEY)
+WHEN NOT MATCHED THEN INSERT (REALM_KEY, ROLE_KEY, FUNCTION_KEY) VALUES (s.REALM_KEY, s.ROLE_KEY, s.FUNCTION_KEY);
+
+MERGE INTO SAKAI_REALM_RL_FN t
+USING (
+    SELECT 
+        (SELECT REALM_KEY FROM SAKAI_REALM WHERE REALM_ID = '!site.template.course') AS REALM_KEY,
+        (SELECT ROLE_KEY FROM SAKAI_REALM_ROLE WHERE ROLE_NAME = 'Instructor') AS ROLE_KEY,
+        (SELECT FUNCTION_KEY FROM SAKAI_REALM_FUNCTION WHERE FUNCTION_NAME = 'assessment.all.groups') AS FUNCTION_KEY
+    FROM dual
+) s ON (t.REALM_KEY = s.REALM_KEY AND t.ROLE_KEY = s.ROLE_KEY AND t.FUNCTION_KEY = s.FUNCTION_KEY)
+WHEN NOT MATCHED THEN INSERT (REALM_KEY, ROLE_KEY, FUNCTION_KEY) VALUES (s.REALM_KEY, s.ROLE_KEY, s.FUNCTION_KEY);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
INSERT INTO SAKAI_REALM_RL_FN VALUES((select REALM_KEY from SAKAI_REALM where REALM_ID = '!site.template'), (select ROLE_KEY from SAKAI_REALM_ROLE where ROLE_NAME = 'maintain'), (select FUNCTION_KEY from SAKAI_REALM_FUNCTION where FUNCTION_NAME = 'assessment.all.groups'));
INSERT INTO SAKAI_REALM_RL_FN VALUES((select REALM_KEY from SAKAI_REALM where REALM_ID = '!site.template.course'), (select ROLE_KEY from SAKAI_REALM_ROLE where ROLE_NAME = 'Instructor'), (select FUNCTION_KEY from SAKAI_REALM_FUNCTION where FUNCTION_NAME = 'assessment.all.groups'));
MERGE INTO SAKAI_REALM_RL_FN t
USING (
SELECT
(SELECT REALM_KEY FROM SAKAI_REALM WHERE REALM_ID = '!site.template') AS REALM_KEY,
(SELECT ROLE_KEY FROM SAKAI_REALM_ROLE WHERE ROLE_NAME = 'maintain') AS ROLE_KEY,
(SELECT FUNCTION_KEY FROM SAKAI_REALM_FUNCTION WHERE FUNCTION_NAME = 'assessment.all.groups') AS FUNCTION_KEY
FROM dual
) s ON (t.REALM_KEY = s.REALM_KEY AND t.ROLE_KEY = s.ROLE_KEY AND t.FUNCTION_KEY = s.FUNCTION_KEY)
WHEN NOT MATCHED THEN INSERT (REALM_KEY, ROLE_KEY, FUNCTION_KEY) VALUES (s.REALM_KEY, s.ROLE_KEY, s.FUNCTION_KEY);
MERGE INTO SAKAI_REALM_RL_FN t
USING (
SELECT
(SELECT REALM_KEY FROM SAKAI_REALM WHERE REALM_ID = '!site.template.course') AS REALM_KEY,
(SELECT ROLE_KEY FROM SAKAI_REALM_ROLE WHERE ROLE_NAME = 'Instructor') AS ROLE_KEY,
(SELECT FUNCTION_KEY FROM SAKAI_REALM_FUNCTION WHERE FUNCTION_NAME = 'assessment.all.groups') AS FUNCTION_KEY
FROM dual
) s ON (t.REALM_KEY = s.REALM_KEY AND t.ROLE_KEY = s.ROLE_KEY AND t.FUNCTION_KEY = s.FUNCTION_KEY)
WHEN NOT MATCHED THEN INSERT (REALM_KEY, ROLE_KEY, FUNCTION_KEY) VALUES (s.REALM_KEY, s.ROLE_KEY, s.FUNCTION_KEY);
🤖 Prompt for AI Agents
In docs/conversion/sakai_25_1-25_2_oracle_conversion.sql around lines 13 to 14,
the two INSERT statements for SAKAI_REALM_RL_FN are not idempotent and will fail
on duplicate key constraint on re-run; replace each INSERT with an
Oracle-friendly idempotent operation (e.g., use a MERGE that matches on the
composite key columns or wrap the insert in a conditional INSERT ... SELECT that
only inserts when a corresponding row does not exist using NOT EXISTS), ensuring
you compute the same subselect values for realm/role/function and use them as
the MERGE key predicates or in the NOT EXISTS check so repeated runs do nothing
if the mapping already exists.

-- Add this to populate existing sites with the permission
CREATE TABLE PERMISSIONS_SRC_TEMP (ROLE_NAME VARCHAR(99), FUNCTION_NAME VARCHAR(99));
INSERT INTO PERMISSIONS_SRC_TEMP VALUES ('maintain','assessment.all.groups');
INSERT INTO PERMISSIONS_SRC_TEMP VALUES ('Instructor','assessment.all.groups');

CREATE TABLE PERMISSIONS_TEMP (ROLE_KEY INTEGER, FUNCTION_KEY INTEGER);
INSERT INTO PERMISSIONS_TEMP (ROLE_KEY, FUNCTION_KEY)
SELECT SRR.ROLE_KEY, SRF.FUNCTION_KEY
FROM PERMISSIONS_SRC_TEMP TMPSRC
JOIN SAKAI_REALM_ROLE SRR ON (TMPSRC.ROLE_NAME = SRR.ROLE_NAME)
JOIN SAKAI_REALM_FUNCTION SRF ON (TMPSRC.FUNCTION_NAME = SRF.FUNCTION_NAME);

INSERT INTO SAKAI_REALM_RL_FN (REALM_KEY, ROLE_KEY, FUNCTION_KEY)
SELECT
SRRFD.REALM_KEY, SRRFD.ROLE_KEY, TMP.FUNCTION_KEY
FROM
(SELECT DISTINCT SRRF.REALM_KEY, SRRF.ROLE_KEY FROM SAKAI_REALM_RL_FN SRRF) SRRFD
JOIN PERMISSIONS_TEMP TMP ON (SRRFD.ROLE_KEY = TMP.ROLE_KEY)
JOIN SAKAI_REALM SR ON (SRRFD.REALM_KEY = SR.REALM_KEY)
WHERE SR.REALM_ID != '!site.helper' AND SR.REALM_ID NOT LIKE '!user.template%'
AND NOT EXISTS (
SELECT 1
FROM SAKAI_REALM_RL_FN SRRFI
WHERE SRRFI.REALM_KEY=SRRFD.REALM_KEY AND SRRFI.ROLE_KEY=SRRFD.ROLE_KEY AND SRRFI.FUNCTION_KEY=TMP.FUNCTION_KEY
);

DROP TABLE PERMISSIONS_TEMP;
DROP TABLE PERMISSIONS_SRC_TEMP;
-- END SAK-51713 --