Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Add migration to add property userActivated to user settings (no-changelog) #5940

Merged
merged 9 commits into from
Apr 21, 2023
2 changes: 2 additions & 0 deletions packages/cli/src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -868,3 +868,5 @@ export interface N8nApp {
externalHooks: IExternalHooksClass;
activeWorkflowRunner: ActiveWorkflowRunner;
}

export type userSettings = Pick<User, 'id' | 'settings'>;
RicardoE105 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
import { getTablePrefix, logMigrationEnd, logMigrationStart } from '@db/utils/migrationHelpers';
import { userSettings } from '@/Interfaces';
RicardoE105 marked this conversation as resolved.
Show resolved Hide resolved

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 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 = '${user.settings}' WHERE id = '${user.id}' `,
RicardoE105 marked this conversation as resolved.
Show resolved Hide resolved
),
);

await Promise.all(updatedUsers);

const activatedUserIds = activatedUsers.map((user) => `'${user.id}'`).join(',');

await queryRunner.query(
RicardoE105 marked this conversation as resolved.
Show resolved Hide resolved
`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')`,
);
RicardoE105 marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 2 additions & 0 deletions packages/cli/src/databases/migrations/mysqldb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { AddStatusToExecutions1674138566000 } from './1674138566000-AddStatusToE
import { MigrateExecutionStatus1676996103000 } from './1676996103000-MigrateExecutionStatus';
import { UpdateRunningExecutionStatus1677236788851 } from './1677236788851-UpdateRunningExecutionStatus';
import { CreateExecutionMetadataTable1679416281779 } from './1679416281779-CreateExecutionMetadataTable';
import { AddUserActivatedProperty1681134145996 } from './1681134145996-AddUserActivatedProperty';

export const mysqlMigrations = [
InitialMigration1588157391238,
Expand Down Expand Up @@ -74,4 +75,5 @@ export const mysqlMigrations = [
MigrateExecutionStatus1676996103000,
UpdateRunningExecutionStatus1677236788851,
CreateExecutionMetadataTable1679416281779,
AddUserActivatedProperty1681134145996,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
import { getTablePrefix, logMigrationEnd, logMigrationStart } from '@db/utils/migrationHelpers';
import { 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 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);

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 = JSON_EXTRACT_PATH(settings, 'userActivated')`,
RicardoE105 marked this conversation as resolved.
Show resolved Hide resolved
);
}
}
2 changes: 2 additions & 0 deletions packages/cli/src/databases/migrations/postgresdb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { AddStatusToExecutions1674138566000 } from './1674138566000-AddStatusToE
import { MigrateExecutionStatus1676996103000 } from './1676996103000-MigrateExecutionStatus';
import { UpdateRunningExecutionStatus1677236854063 } from './1677236854063-UpdateRunningExecutionStatus';
import { CreateExecutionMetadataTable1679416281778 } from './1679416281778-CreateExecutionMetadataTable';
import { AddUserActivatedProperty1681134145996 } from './1681134145996-AddUserActivatedProperty';

export const postgresMigrations = [
InitialMigration1587669153312,
Expand Down Expand Up @@ -70,4 +71,5 @@ export const postgresMigrations = [
MigrateExecutionStatus1676996103000,
UpdateRunningExecutionStatus1677236854063,
CreateExecutionMetadataTable1679416281778,
AddUserActivatedProperty1681134145996,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
import { getTablePrefix, logMigrationEnd, logMigrationStart } from '@db/utils/migrationHelpers';
import { 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 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);

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')`,
);
}
}
2 changes: 2 additions & 0 deletions packages/cli/src/databases/migrations/sqlite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { AddStatusToExecutions1674138566000 } from './1674138566000-AddStatusToE
import { MigrateExecutionStatus1676996103000 } from './1676996103000-MigrateExecutionStatus';
import { UpdateRunningExecutionStatus1677237073720 } from './1677237073720-UpdateRunningExecutionStatus';
import { CreateExecutionMetadataTable1679416281777 } from './1679416281777-CreateExecutionMetadataTable';
import { AddUserActivatedProperty1681134145996 } from './1681134145996-AddUserActivatedProperty';

const sqliteMigrations = [
InitialMigration1588102412422,
Expand Down Expand Up @@ -68,6 +69,7 @@ const sqliteMigrations = [
MigrateExecutionStatus1676996103000,
UpdateRunningExecutionStatus1677237073720,
CreateExecutionMetadataTable1679416281777,
AddUserActivatedProperty1681134145996,
];

export { sqliteMigrations };