From 48e09dbc2a0aeae3f16e8a9efbba829dfb7a6f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Thu, 24 Feb 2022 10:43:23 +0100 Subject: [PATCH] fix: add migration patch This patch will fix issues introduced in Unleash v3.13.0. It only affects users trying to upgrade from v3 to v4 and has been using v3.13.0 which contained an incorrect migration file. --- .../20210428103922-patch-role-table.js | 9 ++++ .../20210428103924-patch-admin-role-name.js | 14 +++++ .../20210428103924-patch-admin_role.js | 27 ++++++++++ .../20210428103924-patch-role_permissions.js | 53 +++++++++++++++++++ ...081422-remove-project-column-from-roles.js | 8 +++ 5 files changed, 111 insertions(+) create mode 100644 src/migrations/20210428103922-patch-role-table.js create mode 100644 src/migrations/20210428103924-patch-admin-role-name.js create mode 100644 src/migrations/20210428103924-patch-admin_role.js create mode 100644 src/migrations/20210428103924-patch-role_permissions.js create mode 100644 src/migrations/20220224081422-remove-project-column-from-roles.js diff --git a/src/migrations/20210428103922-patch-role-table.js b/src/migrations/20210428103922-patch-role-table.js new file mode 100644 index 00000000000..451b7fe8f8f --- /dev/null +++ b/src/migrations/20210428103922-patch-role-table.js @@ -0,0 +1,9 @@ +'use strict'; + +exports.up = function (db, cb) { + db.runSql('ALTER TABLE roles ADD COLUMN IF NOT EXISTS project text', cb); +}; + +exports.down = function (db, cb) { + cb(); +}; diff --git a/src/migrations/20210428103924-patch-admin-role-name.js b/src/migrations/20210428103924-patch-admin-role-name.js new file mode 100644 index 00000000000..8e4bf6eb058 --- /dev/null +++ b/src/migrations/20210428103924-patch-admin-role-name.js @@ -0,0 +1,14 @@ +'use strict'; + +exports.up = function (db, cb) { + db.runSql( + ` + UPDATE roles set name='Admin' where name='Super User'; + `, + cb, + ); +}; + +exports.down = function (db, cb) { + cb(); +}; diff --git a/src/migrations/20210428103924-patch-admin_role.js b/src/migrations/20210428103924-patch-admin_role.js new file mode 100644 index 00000000000..891e6f02ab2 --- /dev/null +++ b/src/migrations/20210428103924-patch-admin_role.js @@ -0,0 +1,27 @@ +'use strict'; + +exports.up = function (db, cb) { + db.runSql( + ` + DO $$ + declare + begin + WITH admin AS ( + SELECT * FROM roles WHERE name in ('Admin', 'Super User') LIMIT 1 + ) + INSERT into role_user(role_id, user_id) + VALUES + ((select id from admin), (select id FROM users where username='admin' LIMIT 1)); + + EXCEPTION WHEN OTHERS THEN + raise notice 'Ignored'; + end; + $$;`, + cb, + ); +}; + +exports.down = function (db, cb) { + // We can't just remove roles for users as we don't know if there has been any manual additions. + cb(); +}; diff --git a/src/migrations/20210428103924-patch-role_permissions.js b/src/migrations/20210428103924-patch-role_permissions.js new file mode 100644 index 00000000000..ecd74e719da --- /dev/null +++ b/src/migrations/20210428103924-patch-role_permissions.js @@ -0,0 +1,53 @@ +'use strict'; + +exports.up = function (db, cb) { + db.runSql( + ` + DO $$ + declare + begin + WITH editor AS ( + SELECT * FROM roles WHERE name in ('Editor', 'Regular') LIMIT 1 + ) + + INSERT INTO role_permission(role_id, project, permission) + VALUES + ((SELECT id from editor), '', 'CREATE_STRATEGY'), + ((SELECT id from editor), '', 'UPDATE_STRATEGY'), + ((SELECT id from editor), '', 'DELETE_STRATEGY'), + + ((SELECT id from editor), '', 'UPDATE_APPLICATION'), + + ((SELECT id from editor), '', 'CREATE_CONTEXT_FIELD'), + ((SELECT id from editor), '', 'UPDATE_CONTEXT_FIELD'), + ((SELECT id from editor), '', 'DELETE_CONTEXT_FIELD'), + + ((SELECT id from editor), '', 'CREATE_PROJECT'), + + ((SELECT id from editor), '', 'CREATE_ADDON'), + ((SELECT id from editor), '', 'UPDATE_ADDON'), + ((SELECT id from editor), '', 'DELETE_ADDON'), + + ((SELECT id from editor), 'default', 'UPDATE_PROJECT'), + ((SELECT id from editor), 'default', 'DELETE_PROJECT'), + ((SELECT id from editor), 'default', 'CREATE_FEATURE'), + ((SELECT id from editor), 'default', 'UPDATE_FEATURE'), + ((SELECT id from editor), 'default', 'DELETE_FEATURE'); + + -- Clean up duplicates + DELETE FROM role_permission p1 + USING role_permission p2 + WHERE p1.created_at < p2.created_at -- select the "older" ones + AND p1.project = p2.project -- list columns that define duplicates + AND p1.permission = p2.permission; + EXCEPTION WHEN OTHERS THEN + raise notice 'Ignored'; + end; + $$;`, + cb, + ); +}; + +exports.down = function (db, cb) { + cb(); +}; diff --git a/src/migrations/20220224081422-remove-project-column-from-roles.js b/src/migrations/20220224081422-remove-project-column-from-roles.js new file mode 100644 index 00000000000..840ea50de8e --- /dev/null +++ b/src/migrations/20220224081422-remove-project-column-from-roles.js @@ -0,0 +1,8 @@ +exports.up = function (db, cb) { + db.runSql('ALTER TABLE roles DROP COLUMN IF EXISTS project', cb); +}; + +exports.down = function (db, cb) { + // We can't just remove roles for users as we don't know if there has been any manual additions. + db.runSql('ALTER TABLE roles ADD COLUMN IF NOT EXISTS project text', cb); +};