mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 10:31:15 +00:00
refactor: On workflow deletion, cascade delete all entities associated with it (#5102)
This commit is contained in:
committed by
GitHub
parent
7df0728999
commit
0e955760a1
@@ -0,0 +1,52 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
import { logMigrationEnd, logMigrationStart } from '@db/utils/migrationHelpers';
|
||||
import config from '@/config';
|
||||
|
||||
export class DeleteExecutionsWithWorkflows1673268682475 implements MigrationInterface {
|
||||
name = 'DeleteExecutionsWithWorkflows1673268682475';
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
logMigrationStart(this.name);
|
||||
const tablePrefix = config.getEnv('database.tablePrefix');
|
||||
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE ${tablePrefix}execution_entity
|
||||
ALTER COLUMN "workflowId" TYPE INTEGER USING "workflowId"::integer`,
|
||||
);
|
||||
|
||||
const workflowIds: Array<{ id: number }> = await queryRunner.query(`
|
||||
SELECT id FROM ${tablePrefix}workflow_entity
|
||||
`);
|
||||
|
||||
await queryRunner.query(
|
||||
`DELETE FROM ${tablePrefix}execution_entity
|
||||
WHERE "workflowId" IS NOT NULL
|
||||
${
|
||||
workflowIds.length
|
||||
? `AND "workflowId" NOT IN (${workflowIds.map(({ id }) => id).join()})`
|
||||
: ''
|
||||
}`,
|
||||
);
|
||||
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE ${tablePrefix}execution_entity
|
||||
ADD CONSTRAINT "FK_${tablePrefix}execution_entity_workflowId"
|
||||
FOREIGN KEY ("workflowId") REFERENCES ${tablePrefix}workflow_entity ("id")
|
||||
ON DELETE CASCADE`,
|
||||
);
|
||||
|
||||
logMigrationEnd(this.name);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
const tablePrefix = config.getEnv('database.tablePrefix');
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE ${tablePrefix}execution_entity
|
||||
DROP CONSTRAINT "FK_${tablePrefix}execution_entity_workflowId"`,
|
||||
);
|
||||
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE ${tablePrefix}execution_entity
|
||||
ALTER COLUMN "workflowId" TYPE TEXT`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import { AddWorkflowVersionIdColumn1669739707126 } from './1669739707126-AddWork
|
||||
import { AddTriggerCountColumn1669823906995 } from './1669823906995-AddTriggerCountColumn';
|
||||
import { RemoveWorkflowDataLoadedFlag1671726148421 } from './1671726148421-RemoveWorkflowDataLoadedFlag';
|
||||
import { MessageEventBusDestinations1671535397530 } from './1671535397530-MessageEventBusDestinations';
|
||||
import { DeleteExecutionsWithWorkflows1673268682475 } from './1673268682475-DeleteExecutionsWithWorkflows';
|
||||
|
||||
export const postgresMigrations = [
|
||||
InitialMigration1587669153312,
|
||||
@@ -56,4 +57,5 @@ export const postgresMigrations = [
|
||||
AddTriggerCountColumn1669823906995,
|
||||
RemoveWorkflowDataLoadedFlag1671726148421,
|
||||
MessageEventBusDestinations1671535397530,
|
||||
DeleteExecutionsWithWorkflows1673268682475,
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user