mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 02:21:13 +00:00
feat(core): Add migration to add property userActivated to user settings (no-changelog) (#5940)
* Add userActivated migration
* Fix migration logic
* Remove duplication when retrieving the activated users
* Fix bug updating settings in mysql
* Make userSettings type conform with naming convention
* Disable naming convention rule only in IDatabaseCollections interface
* Fix down method in Postgres migration
* Reset '{}' to NULL when reversing migration
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
import { getTablePrefix, logMigrationEnd, logMigrationStart } from '@db/utils/migrationHelpers';
|
||||
import type { UserSettings } from '@/Interfaces';
|
||||
|
||||
export class AddUserActivatedProperty1681134145996 implements MigrationInterface {
|
||||
name = 'AddUserActivatedProperty1681134145996';
|
||||
|
||||
async up(queryRunner: QueryRunner): Promise<void> {
|
||||
logMigrationStart(this.name);
|
||||
|
||||
const tablePrefix = getTablePrefix();
|
||||
|
||||
const activatedUsers: UserSettings[] = await queryRunner.query(
|
||||
`SELECT DISTINCT sw."userId" AS id,
|
||||
JSONB_SET(COALESCE(u.settings::jsonb, '{}'), '{userActivated}', 'true', true) as settings
|
||||
FROM ${tablePrefix}workflow_statistics ws
|
||||
JOIN ${tablePrefix}shared_workflow sw
|
||||
ON ws."workflowId" = sw."workflowId"
|
||||
JOIN ${tablePrefix}role r
|
||||
ON r.id = sw."roleId"
|
||||
JOIN "${tablePrefix}user" u
|
||||
ON u.id = sw."userId"
|
||||
WHERE ws.name = 'production_success'
|
||||
AND r.name = 'owner'
|
||||
AND r.scope = 'workflow'`,
|
||||
);
|
||||
|
||||
const updatedUsers = activatedUsers.map((user) =>
|
||||
queryRunner.query(
|
||||
`UPDATE "${tablePrefix}user" SET settings = '${JSON.stringify(
|
||||
user.settings,
|
||||
)}' WHERE id = '${user.id}' `,
|
||||
),
|
||||
);
|
||||
|
||||
await Promise.all(updatedUsers);
|
||||
|
||||
if (!activatedUsers.length) {
|
||||
await queryRunner.query(
|
||||
`UPDATE "${tablePrefix}user" SET settings = JSONB_SET(COALESCE(settings::jsonb, '{}'), '{userActivated}', 'false', true)`,
|
||||
);
|
||||
} else {
|
||||
const activatedUserIds = activatedUsers.map((user) => `'${user.id}'`).join(',');
|
||||
|
||||
await queryRunner.query(
|
||||
`UPDATE "${tablePrefix}user" SET settings = JSONB_SET(COALESCE(settings::jsonb, '{}'), '{userActivated}', 'false', true) WHERE id NOT IN (${activatedUserIds})`,
|
||||
);
|
||||
}
|
||||
|
||||
logMigrationEnd(this.name);
|
||||
}
|
||||
|
||||
async down(queryRunner: QueryRunner): Promise<void> {
|
||||
const tablePrefix = getTablePrefix();
|
||||
await queryRunner.query(
|
||||
`UPDATE "${tablePrefix}user" SET settings = settings::jsonb - 'userActivated'`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`UPDATE "${tablePrefix}user" SET settings = NULL WHERE settings::jsonb = '{}'::jsonb`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,7 @@ import { MigrateExecutionStatus1676996103000 } from './1676996103000-MigrateExec
|
||||
import { UpdateRunningExecutionStatus1677236854063 } from './1677236854063-UpdateRunningExecutionStatus';
|
||||
import { CreateExecutionMetadataTable1679416281778 } from './1679416281778-CreateExecutionMetadataTable';
|
||||
import { CreateVariables1677501636754 } from './1677501636754-CreateVariables';
|
||||
import { AddUserActivatedProperty1681134145996 } from './1681134145996-AddUserActivatedProperty';
|
||||
|
||||
export const postgresMigrations = [
|
||||
InitialMigration1587669153312,
|
||||
@@ -72,4 +73,5 @@ export const postgresMigrations = [
|
||||
UpdateRunningExecutionStatus1677236854063,
|
||||
CreateExecutionMetadataTable1679416281778,
|
||||
CreateVariables1677501636754,
|
||||
AddUserActivatedProperty1681134145996,
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user