mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 04:34:06 -08:00
fix(core): Fix DropRoleMapping migration (#8521)
This commit is contained in:
parent
5832d3ca46
commit
4fed68ee34
|
@ -8,6 +8,12 @@ const idColumns: Record<Table, string> = {
|
||||||
shared_workflow: 'workflowId',
|
shared_workflow: 'workflowId',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const uidColumns: Record<Table, string> = {
|
||||||
|
user: 'id',
|
||||||
|
shared_credentials: 'userId',
|
||||||
|
shared_workflow: 'userId',
|
||||||
|
};
|
||||||
|
|
||||||
const roleScopes: Record<Table, string> = {
|
const roleScopes: Record<Table, string> = {
|
||||||
user: 'global',
|
user: 'global',
|
||||||
shared_credentials: 'credential',
|
shared_credentials: 'credential',
|
||||||
|
@ -48,24 +54,30 @@ export class DropRoleMapping1705429061930 implements ReversibleMigration {
|
||||||
const roleTable = escape.tableName('role');
|
const roleTable = escape.tableName('role');
|
||||||
const tableName = escape.tableName(table);
|
const tableName = escape.tableName(table);
|
||||||
const idColumn = escape.columnName(idColumns[table]);
|
const idColumn = escape.columnName(idColumns[table]);
|
||||||
|
const uidColumn = escape.columnName(uidColumns[table]);
|
||||||
const roleColumnName = table === 'user' ? 'globalRoleId' : 'roleId';
|
const roleColumnName = table === 'user' ? 'globalRoleId' : 'roleId';
|
||||||
const roleColumn = escape.columnName(roleColumnName);
|
const roleColumn = escape.columnName(roleColumnName);
|
||||||
const scope = roleScopes[table];
|
const scope = roleScopes[table];
|
||||||
const isMySQL = ['mariadb', 'mysqldb'].includes(dbType);
|
const isMySQL = ['mariadb', 'mysqldb'].includes(dbType);
|
||||||
const roleField = isMySQL ? `CONCAT('${scope}:', R.name)` : `'${scope}:' || R.name`;
|
const roleField = isMySQL ? `CONCAT('${scope}:', R.name)` : `'${scope}:' || R.name`;
|
||||||
const subQuery = `
|
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
|
FROM ${tableName} T
|
||||||
LEFT JOIN ${roleTable} R
|
LEFT JOIN ${roleTable} R
|
||||||
ON T.${roleColumn} = R.id and R.scope = '${scope}'`;
|
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
|
const swQuery = isMySQL
|
||||||
? `UPDATE ${tableName}, (${subQuery}) as mapping
|
? `UPDATE ${tableName}, (${subQuery}) as mapping
|
||||||
SET ${tableName}.role = mapping.role
|
SET ${tableName}.role = mapping.role
|
||||||
WHERE ${tableName}.${idColumn} = mapping.id`
|
${where}`
|
||||||
: `UPDATE ${tableName}
|
: `UPDATE ${tableName}
|
||||||
SET role = mapping.role
|
SET role = mapping.role
|
||||||
FROM (${subQuery}) as mapping
|
FROM (${subQuery}) as mapping
|
||||||
WHERE ${tableName}.${idColumn} = mapping.id`;
|
${where}`;
|
||||||
await runQuery(swQuery);
|
await runQuery(swQuery);
|
||||||
|
|
||||||
await addNotNull(table, 'role');
|
await addNotNull(table, 'role');
|
||||||
|
@ -95,23 +107,27 @@ export class DropRoleMapping1705429061930 implements ReversibleMigration {
|
||||||
const roleTable = escape.tableName('role');
|
const roleTable = escape.tableName('role');
|
||||||
const tableName = escape.tableName(table);
|
const tableName = escape.tableName(table);
|
||||||
const idColumn = escape.columnName(idColumns[table]);
|
const idColumn = escape.columnName(idColumns[table]);
|
||||||
|
const uidColumn = escape.columnName(uidColumns[table]);
|
||||||
const roleColumn = escape.columnName(roleColumnName);
|
const roleColumn = escape.columnName(roleColumnName);
|
||||||
const scope = roleScopes[table];
|
const scope = roleScopes[table];
|
||||||
const isMySQL = ['mariadb', 'mysqldb'].includes(dbType);
|
const isMySQL = ['mariadb', 'mysqldb'].includes(dbType);
|
||||||
const roleField = isMySQL ? `CONCAT('${scope}:', R.name)` : `'${scope}:' || R.name`;
|
const roleField = isMySQL ? `CONCAT('${scope}:', R.name)` : `'${scope}:' || R.name`;
|
||||||
const subQuery = `
|
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
|
FROM ${tableName} T
|
||||||
LEFT JOIN ${roleTable} R
|
LEFT JOIN ${roleTable} R
|
||||||
ON T.role = ${roleField} and R.scope = '${scope}'`;
|
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
|
const query = isMySQL
|
||||||
? `UPDATE ${tableName}, (${subQuery}) as mapping
|
? `UPDATE ${tableName}, (${subQuery}) as mapping
|
||||||
SET ${tableName}.${roleColumn} = mapping.role_id
|
SET ${tableName}.${roleColumn} = mapping.role_id
|
||||||
WHERE ${tableName}.${idColumn} = mapping.id`
|
${where}`
|
||||||
: `UPDATE ${tableName}
|
: `UPDATE ${tableName}
|
||||||
SET ${roleColumn} = mapping.role_id
|
SET ${roleColumn} = mapping.role_id
|
||||||
FROM (${subQuery}) as mapping
|
FROM (${subQuery}) as mapping
|
||||||
WHERE ${tableName}.${idColumn} = mapping.id`;
|
${where}`;
|
||||||
await runQuery(query);
|
await runQuery(query);
|
||||||
|
|
||||||
await addNotNull(table, roleColumnName);
|
await addNotNull(table, roleColumnName);
|
||||||
|
|
Loading…
Reference in a new issue