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

fix(core): Disallow orphan executions #7069

Merged
merged 22 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { MigrationContext, ReversibleMigration } from '@db/types';

export class DisallowOrphanExecutions1693554410387 implements ReversibleMigration {
/**
* Ensure all executions point to a workflow.
*/
async up({ queryRunner, escape }: MigrationContext) {
const executionEntity = escape.tableName('execution_entity');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not combine the 3 migrations into a single one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you mean? The syntax to modify a column differs between MySQL and Postgres, and sqlite requires recreating the table, and it looks like the DSL does not support this yet.

const workflowId = escape.columnName('workflowId');

await queryRunner.query(`DELETE FROM ${executionEntity} WHERE ${workflowId} IS NULL;`);
await queryRunner.query(
`ALTER TABLE ${executionEntity} MODIFY ${workflowId} VARCHAR(36) NOT NULL;`,
);
}

/**
* Reversal excludes restoring deleted rows.
*/
async down({ queryRunner, escape }: MigrationContext) {
const executionEntity = escape.tableName('execution_entity');
const workflowId = escape.columnName('workflowId');

await queryRunner.query(`ALTER TABLE ${executionEntity} MODIFY ${workflowId} VARCHAR(36);`);
}
}
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 @@ -46,6 +46,7 @@ import { RemoveResetPasswordColumns1690000000030 } from '../common/1690000000030
import { CreateWorkflowNameIndex1691088862123 } from '../common/1691088862123-CreateWorkflowNameIndex';
import { AddMfaColumns1690000000030 } from './../common/1690000000040-AddMfaColumns';
import { CreateWorkflowHistoryTable1692967111175 } from '../common/1692967111175-CreateWorkflowHistoryTable';
import { DisallowOrphanExecutions1693554410387 } from './1693554410387-DisallowOrphanExecutions';

export const mysqlMigrations: Migration[] = [
InitialMigration1588157391238,
Expand Down Expand Up @@ -95,4 +96,5 @@ export const mysqlMigrations: Migration[] = [
CreateWorkflowNameIndex1691088862123,
AddMfaColumns1690000000030,
CreateWorkflowHistoryTable1692967111175,
DisallowOrphanExecutions1693554410387,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { MigrationContext, ReversibleMigration } from '@db/types';

export class DisallowOrphanExecutions1693554410387 implements ReversibleMigration {
/**
* Ensure all executions point to a workflow.
*/
async up({ queryRunner, escape }: MigrationContext) {
const executionEntity = escape.tableName('execution_entity');
const workflowId = escape.columnName('workflowId');

await queryRunner.query(`DELETE FROM ${executionEntity} WHERE ${workflowId} IS NULL;`);
await queryRunner.query(
`ALTER TABLE ${executionEntity} ALTER COLUMN ${workflowId} SET NOT NULL;`,
);
}

/**
* Reversal excludes restoring deleted rows.
*/
async down({ queryRunner, escape }: MigrationContext) {
const executionEntity = escape.tableName('execution_entity');
const workflowId = escape.columnName('workflowId');

await queryRunner.query(
`ALTER TABLE ${executionEntity} ALTER COLUMN ${workflowId} DROP NOT NULL;`,
);
}
}
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 @@ -44,6 +44,7 @@ import { AddMissingPrimaryKeyOnExecutionData1690787606731 } from './169078760673
import { CreateWorkflowNameIndex1691088862123 } from '../common/1691088862123-CreateWorkflowNameIndex';
import { AddMfaColumns1690000000030 } from './../common/1690000000040-AddMfaColumns';
import { CreateWorkflowHistoryTable1692967111175 } from '../common/1692967111175-CreateWorkflowHistoryTable';
import { DisallowOrphanExecutions1693554410387 } from './1693554410387-DisallowOrphanExecutions';

export const postgresMigrations: Migration[] = [
InitialMigration1587669153312,
Expand Down Expand Up @@ -91,4 +92,5 @@ export const postgresMigrations: Migration[] = [
CreateWorkflowNameIndex1691088862123,
AddMfaColumns1690000000030,
CreateWorkflowHistoryTable1692967111175,
DisallowOrphanExecutions1693554410387,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import type { MigrationContext, ReversibleMigration } from '@db/types';

export class DisallowOrphanExecutions1693554410387 implements ReversibleMigration {
transaction = false as const;

/**
* Ensure all executions point to a workflow. Recreate table because sqlite
* does not support modifying column.
*/
async up({ escape, schemaBuilder: { createTable, column }, runQuery }: MigrationContext) {
const executionEntity = escape.tableName('execution_entity');
const workflowId = escape.columnName('workflowId');

await runQuery(`DELETE FROM ${executionEntity} WHERE ${workflowId} IS NULL;`);

await createTable('temp')
.withColumns(
column('id').int.primary.autoGenerate.notNull,
column('workflowId').varchar(36).notNull, // only change
column('finished').bool.notNull,
column('mode').varchar().notNull,
column('retryOf').varchar(),
column('retrySuccessId').varchar(),
column('startedAt').timestamp().notNull,
column('stoppedAt').timestamp(),
column('waitTill').timestamp(),
column('status').varchar(),
)
.withForeignKey('workflowId', {
tableName: 'workflow_entity',
columnName: 'id',
onDelete: 'CASCADE',
});
ivov marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will require recreating the indexes as well I think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see 672d2d7


await runQuery(
`INSERT INTO "temp" ("id", "workflowId", "finished", "mode", "retryOf", "retrySuccessId", "startedAt", "stoppedAt", "waitTill", "status") SELECT "id", "workflowId", "finished", "mode", "retryOf", "retrySuccessId", "startedAt", "stoppedAt", "waitTill", "status" FROM ${executionEntity};`,
);

await runQuery(`DROP TABLE ${executionEntity};`);
ivov marked this conversation as resolved.
Show resolved Hide resolved

await runQuery(`ALTER TABLE "temp" RENAME TO ${executionEntity};`);
}

/**
* Reversal excludes restoring deleted rows.
*/
async down({ escape, schemaBuilder: { createTable, column }, runQuery }: MigrationContext) {
const executionEntity = escape.tableName('execution_entity');

await createTable('temp')
.withColumns(
column('id').int.primary.autoGenerate.notNull,
column('workflowId').varchar(36), // only change
column('finished').bool.notNull,
column('mode').varchar().notNull,
column('retryOf').varchar(),
column('retrySuccessId').varchar(),
column('startedAt').timestamp().notNull,
column('stoppedAt').timestamp(),
column('waitTill').timestamp(),
column('status').varchar(),
)
.withForeignKey('workflowId', {
tableName: 'workflow_entity',
columnName: 'id',
onDelete: 'CASCADE',
});

await runQuery(
`INSERT INTO "temp" ("id", "workflowId", "finished", "mode", "retryOf", "retrySuccessId", "startedAt", "stoppedAt", "waitTill", "status") SELECT "id", "workflowId", "finished", "mode", "retryOf", "retrySuccessId", "startedAt", "stoppedAt", "waitTill", "status" FROM ${executionEntity};`,
);

await runQuery(`DROP TABLE ${executionEntity};`);

await runQuery(`ALTER TABLE "temp" RENAME TO ${executionEntity};`);
}
}
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 @@ -43,6 +43,7 @@ import { RemoveResetPasswordColumns1690000000030 } from './1690000000030-RemoveR
import { CreateWorkflowNameIndex1691088862123 } from '../common/1691088862123-CreateWorkflowNameIndex';
import { AddMfaColumns1690000000030 } from './1690000000040-AddMfaColumns';
import { CreateWorkflowHistoryTable1692967111175 } from '../common/1692967111175-CreateWorkflowHistoryTable';
import { DisallowOrphanExecutions1693554410387 } from './1693554410387-DisallowOrphanExecutions';

const sqliteMigrations: Migration[] = [
InitialMigration1588102412422,
Expand Down Expand Up @@ -89,6 +90,7 @@ const sqliteMigrations: Migration[] = [
CreateWorkflowNameIndex1691088862123,
AddMfaColumns1690000000030,
CreateWorkflowHistoryTable1692967111175,
DisallowOrphanExecutions1693554410387,
];

export { sqliteMigrations };