From 731b3058602fe51358c6a0f502278441d2e3d0ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Fri, 2 Feb 2024 12:22:09 +0100 Subject: [PATCH] fix(core): Fix DropRoleMapping migration (#8521) --- .../common/1705429061930-DropRoleMapping.ts | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/cli/src/databases/migrations/common/1705429061930-DropRoleMapping.ts b/packages/cli/src/databases/migrations/common/1705429061930-DropRoleMapping.ts index dd91e6f5413a9..2fa897707aee9 100644 --- a/packages/cli/src/databases/migrations/common/1705429061930-DropRoleMapping.ts +++ b/packages/cli/src/databases/migrations/common/1705429061930-DropRoleMapping.ts @@ -8,6 +8,12 @@ const idColumns: Record = { shared_workflow: 'workflowId', }; +const uidColumns: Record = { + user: 'id', + shared_credentials: 'userId', + shared_workflow: 'userId', +}; + const roleScopes: Record = { user: 'global', shared_credentials: 'credential', @@ -48,24 +54,30 @@ export class DropRoleMapping1705429061930 implements ReversibleMigration { const roleTable = escape.tableName('role'); const tableName = escape.tableName(table); const idColumn = escape.columnName(idColumns[table]); + const uidColumn = escape.columnName(uidColumns[table]); const roleColumnName = table === 'user' ? 'globalRoleId' : 'roleId'; const roleColumn = escape.columnName(roleColumnName); const scope = roleScopes[table]; const isMySQL = ['mariadb', 'mysqldb'].includes(dbType); const roleField = isMySQL ? `CONCAT('${scope}:', R.name)` : `'${scope}:' || R.name`; const subQuery = ` - SELECT ${roleField} as role, T.${idColumn} as id + SELECT ${roleField} as role, T.${idColumn} as id${ + table !== 'user' ? `, T.${uidColumn} as uid` : '' + } FROM ${tableName} T LEFT JOIN ${roleTable} R ON T.${roleColumn} = R.id and R.scope = '${scope}'`; + const where = `WHERE ${tableName}.${idColumn} = mapping.id${ + table !== 'user' ? ` AND ${tableName}.${uidColumn} = mapping.uid` : '' + }`; const swQuery = isMySQL ? `UPDATE ${tableName}, (${subQuery}) as mapping SET ${tableName}.role = mapping.role - WHERE ${tableName}.${idColumn} = mapping.id` + ${where}` : `UPDATE ${tableName} SET role = mapping.role FROM (${subQuery}) as mapping - WHERE ${tableName}.${idColumn} = mapping.id`; + ${where}`; await runQuery(swQuery); await addNotNull(table, 'role'); @@ -95,23 +107,27 @@ export class DropRoleMapping1705429061930 implements ReversibleMigration { const roleTable = escape.tableName('role'); const tableName = escape.tableName(table); const idColumn = escape.columnName(idColumns[table]); + const uidColumn = escape.columnName(uidColumns[table]); const roleColumn = escape.columnName(roleColumnName); const scope = roleScopes[table]; const isMySQL = ['mariadb', 'mysqldb'].includes(dbType); const roleField = isMySQL ? `CONCAT('${scope}:', R.name)` : `'${scope}:' || R.name`; const subQuery = ` - SELECT R.id as role_id, T.${idColumn} as id + SELECT R.id as role_id, T.${idColumn} as id${table !== 'user' ? `, T.${uidColumn} as uid` : ''} FROM ${tableName} T LEFT JOIN ${roleTable} R ON T.role = ${roleField} and R.scope = '${scope}'`; + const where = `WHERE ${tableName}.${idColumn} = mapping.id${ + table !== 'user' ? ` AND ${tableName}.${uidColumn} = mapping.uid` : '' + }`; const query = isMySQL ? `UPDATE ${tableName}, (${subQuery}) as mapping SET ${tableName}.${roleColumn} = mapping.role_id - WHERE ${tableName}.${idColumn} = mapping.id` + ${where}` : `UPDATE ${tableName} SET ${roleColumn} = mapping.role_id FROM (${subQuery}) as mapping - WHERE ${tableName}.${idColumn} = mapping.id`; + ${where}`; await runQuery(query); await addNotNull(table, roleColumnName);