From 3b198709adbb15ed81c55d4962d3ded95078e1ab Mon Sep 17 00:00:00 2001 From: FredrikOseberg Date: Fri, 29 Nov 2024 11:24:49 +0100 Subject: [PATCH] feat: add migration --- ...205-add-foreign-key-to-role-permissions.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/migrations/20241129102205-add-foreign-key-to-role-permissions.js diff --git a/src/migrations/20241129102205-add-foreign-key-to-role-permissions.js b/src/migrations/20241129102205-add-foreign-key-to-role-permissions.js new file mode 100644 index 0000000000..f2e44bc69f --- /dev/null +++ b/src/migrations/20241129102205-add-foreign-key-to-role-permissions.js @@ -0,0 +1,20 @@ +exports.up = function (db, cb) { + db.runSql( + ` + UPDATE role_permission SET environment = null where environment = ''; + DELETE FROM role_permission WHERE environment IS NOT NULL AND environment NOT IN (SELECT name FROM environments); + ALTER TABLE role_permission ADD CONSTRAINT fk_role_permission_environment FOREIGN KEY (environment) REFERENCES environments(name) ON DELETE CASCADE; + `, + cb, + ); +}; + +exports.down = function (db, cb) { + db.runSql( + ` + ALTER TABLE role_permission + DROP CONSTRAINT IF EXISTS fk_role_permission_environment; + `, + cb, + ); +};