From a81b3853cd9c83a60fd8a356460f82192a01c1be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20G=C3=B3is?= Date: Thu, 23 Nov 2023 11:40:14 +0000 Subject: [PATCH] fix: add down migration, remaining fixes --- src/lib/db/access-store.ts | 8 ++-- src/lib/services/access-service.test.ts | 10 ++--- .../20231122122634-drop-permissions-id.js | 39 +++++++++++++++++-- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/lib/db/access-store.ts b/src/lib/db/access-store.ts index e26e8da535..d661e8cbe5 100644 --- a/src/lib/db/access-store.ts +++ b/src/lib/db/access-store.ts @@ -123,7 +123,7 @@ export class AccessStore implements IAccessStore { let userPermissionQuery = this.db .select( 'project', - 'permission', + 'rp.permission', 'environment', 'type', 'ur.role_id', @@ -136,7 +136,7 @@ export class AccessStore implements IAccessStore { userPermissionQuery = userPermissionQuery.union((db) => { db.select( 'project', - 'permission', + 'rp.permission', 'environment', 'p.type', 'gr.role_id', @@ -152,7 +152,7 @@ export class AccessStore implements IAccessStore { userPermissionQuery = userPermissionQuery.union((db) => { db.select( this.db.raw("'default' as project"), - 'permission', + 'rp.permission', 'environment', 'p.type', 'g.root_role_id as role_id', @@ -198,7 +198,7 @@ export class AccessStore implements IAccessStore { const stopTimer = this.timer('getPermissionsForRole'); const rows = await this.db .select( - 'p.permission', + 'rp.permission', 'rp.environment', 'p.display_name', 'p.type', diff --git a/src/lib/services/access-service.test.ts b/src/lib/services/access-service.test.ts index 211896ef94..9e4774028a 100644 --- a/src/lib/services/access-service.test.ts +++ b/src/lib/services/access-service.test.ts @@ -90,7 +90,7 @@ test('should complete environment field of permissions when not present', async description: 'description', permissions: [ { - id: 1, + name: 'name', }, ], }; @@ -101,7 +101,7 @@ test('should complete environment field of permissions when not present', async description: 'description', permissions: [ { - id: 1, + name: 'name', environment: '', }, ], @@ -116,7 +116,7 @@ test('should return the same object when all fields are valid and present', asyn description: 'description', permissions: [ { - id: 1, + name: 'name', environment: 'development', }, ], @@ -126,7 +126,7 @@ test('should return the same object when all fields are valid and present', asyn description: 'description', permissions: [ { - id: 1, + name: 'name', environment: 'development', }, ], @@ -141,7 +141,6 @@ test('should be able to validate and cleanup with additional properties', async additional: 'property', permissions: [ { - id: 1, environment: 'development', name: 'name', displayName: 'displayName', @@ -155,7 +154,6 @@ test('should be able to validate and cleanup with additional properties', async description: 'description', permissions: [ { - id: 1, name: 'name', environment: 'development', }, diff --git a/src/migrations/20231122122634-drop-permissions-id.js b/src/migrations/20231122122634-drop-permissions-id.js index dcd5efbd8c..65867d4083 100644 --- a/src/migrations/20231122122634-drop-permissions-id.js +++ b/src/migrations/20231122122634-drop-permissions-id.js @@ -79,7 +79,7 @@ SELECT * FROM (VALUES ('UPDATE_FRONTEND_API_TOKEN', 'Update FRONTEND API tokens', 'root'), ('DELETE_FRONTEND_API_TOKEN', 'Delete FRONTEND API tokens', 'root'), ('READ_FRONTEND_API_TOKEN', 'Read FRONTEND API tokens', 'root'), - ('UPDATE_FEATURE_DEPENDENCY', 'Update feature dependency', 'project') + ('UPDATE_FEATURE_DEPENDENCY', 'Update feature dependency', 'project'), ('CREATE_TAG_TYPE', 'Create tag types', 'root') ) AS new_permissions(permission, display_name, type) WHERE NOT EXISTS ( @@ -90,6 +90,39 @@ WHERE NOT EXISTS ( ); }; -exports.down = function (db, callback) { - callback(); +exports.down = function (db, cb) { + db.runSql( + ` +-- STEP 1: Undo foreign key constraint on 'role_permission' +ALTER TABLE role_permission +DROP CONSTRAINT fk_role_permission_permission; + +-- STEP 2: Undo primary key constraint on 'permissions' +ALTER TABLE permissions +DROP CONSTRAINT permissions_pkey; + +-- STEP 3: Re-add the permissions 'id' column +ALTER TABLE permissions +ADD COLUMN id SERIAL PRIMARY KEY; + +-- STEP 4: Re-add the role_permission 'permission_id' column +ALTER TABLE role_permission +ADD COLUMN permission_id INTEGER; + +-- STEP 5: Re-add the permissions +UPDATE role_permission rp +SET permission_id = p.id +FROM permissions p +WHERE rp.permission = p.permission; + +-- STEP 6: Drop the new 'permission' column +ALTER TABLE role_permission +DROP COLUMN permission; + +-- STEP 7: Drop the unique constraint on 'permission' +ALTER TABLE permissions +DROP CONSTRAINT permission_unique; + `, + cb + ); };