-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add migration to add property userActivated to user setti…
…ngs (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
- Loading branch information
1 parent
ab12d3e
commit 8a38624
Showing
7 changed files
with
189 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/cli/src/databases/migrations/mysqldb/1681134145996-AddUserActivatedProperty.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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', 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 = '${JSON.stringify(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', false)`, | ||
); | ||
} else { | ||
const activatedUserIds = activatedUsers.map((user) => `'${user.id}'`).join(','); | ||
|
||
await queryRunner.query( | ||
`UPDATE ${tablePrefix}user SET settings = JSON_SET(COALESCE(settings, '{}'), '$.userActivated', 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 = '{}'`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/cli/src/databases/migrations/postgresdb/1681134145996-AddUserActivatedProperty.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/cli/src/databases/migrations/sqlite/1681134145996-AddUserActivatedProperty.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = '{}'`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters