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:
Ricardo Espinoza
2023-04-21 11:15:08 -04:00
committed by GitHub
parent ab12d3e327
commit 8a38624cbc
7 changed files with 189 additions and 1 deletions

View File

@@ -0,0 +1,57 @@
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,
JSON_SET(COALESCE(u.settings, '{}'), '$.userActivated', JSON('true')) AS settings
FROM ${tablePrefix}workflow_statistics AS ws
JOIN ${tablePrefix}shared_workflow AS sw
ON ws.workflowId = sw.workflowId
JOIN ${tablePrefix}role AS r
ON r.id = sw.roleId
JOIN ${tablePrefix}user AS 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 = '${user.settings}' WHERE id = '${user.id}' `,
),
);
await Promise.all(updatedUsers);
if (!activatedUsers.length) {
await queryRunner.query(
`UPDATE ${tablePrefix}user SET settings = JSON_SET(COALESCE(settings, '{}'), '$.userActivated', JSON('false'))`,
);
} else {
const activatedUserIds = activatedUsers.map((user) => `'${user.id}'`).join(',');
await queryRunner.query(
`UPDATE ${tablePrefix}user SET settings = JSON_SET(COALESCE(settings, '{}'), '$.userActivated', JSON('false')) 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 = JSON_REMOVE(settings, '$.userActivated')`,
);
await queryRunner.query(`UPDATE ${tablePrefix}user SET settings = NULL WHERE settings = '{}'`);
}
}

View File

@@ -33,6 +33,7 @@ import { MigrateExecutionStatus1676996103000 } from './1676996103000-MigrateExec
import { UpdateRunningExecutionStatus1677237073720 } from './1677237073720-UpdateRunningExecutionStatus';
import { CreateExecutionMetadataTable1679416281777 } from './1679416281777-CreateExecutionMetadataTable';
import { CreateVariables1677501636752 } from './1677501636752-CreateVariables';
import { AddUserActivatedProperty1681134145996 } from './1681134145996-AddUserActivatedProperty';
const sqliteMigrations = [
InitialMigration1588102412422,
@@ -70,6 +71,7 @@ const sqliteMigrations = [
UpdateRunningExecutionStatus1677237073720,
CreateVariables1677501636752,
CreateExecutionMetadataTable1679416281777,
AddUserActivatedProperty1681134145996,
];
export { sqliteMigrations };