diff --git a/packages/cli/src/databases/migrations/postgresdb/1681134145997-AddWorkflowEntityWithVersion.ts b/packages/cli/src/databases/migrations/postgresdb/1681134145997-AddWorkflowEntityWithVersion.ts new file mode 100644 index 0000000000000..95ae4b55a82c0 --- /dev/null +++ b/packages/cli/src/databases/migrations/postgresdb/1681134145997-AddWorkflowEntityWithVersion.ts @@ -0,0 +1,53 @@ +import type { MigrationContext, ReversibleMigration } from '@db/types'; + +export class AddWorkflowEntityWithVersion1681134145997 implements ReversibleMigration { + async up({ queryRunner, tablePrefix, schemaPrefix }: MigrationContext) { + // Create a new table that behaves like workflow_entity + await queryRunner.query( + `CREATE TABLE ${tablePrefix}workflow_entity_with_version (LIKE ${tablePrefix}workflow_entity)`, + ); + + // Add a unique constraint to avoid duplicated data + await queryRunner.query( + `ALTER TABLE ${tablePrefix}workflow_entity_with_version ADD CONSTRAINT unique_version_id UNIQUE ("versionId")`, + ); + + // Create a function that is inserting every row from workflow_entity to workflow_entity_with_version + await queryRunner.query( + `CREATE FUNCTION ${tablePrefix}duplicate_data_function() + RETURNS TRIGGER AS $$ + BEGIN + INSERT INTO ${schemaPrefix}.${tablePrefix}workflow_entity_with_version SELECT NEW.* ON CONFLICT DO NOTHING; + RETURN NEW; + END; + $$ LANGUAGE plpgsql;`, + ); + + // Create an insertion trigger + await queryRunner.query( + `CREATE TRIGGER duplicate_insert_trigger + AFTER INSERT ON ${tablePrefix}workflow_entity + FOR EACH ROW + EXECUTE FUNCTION ${tablePrefix}duplicate_data_function();`, + ); + + // Create an update trigger + await queryRunner.query( + `CREATE TRIGGER duplicate_update_trigger + AFTER UPDATE ON ${tablePrefix}workflow_entity + FOR EACH ROW + EXECUTE FUNCTION ${tablePrefix}duplicate_data_function();`, + ); + } + + async down({ queryRunner, tablePrefix }: MigrationContext) { + await queryRunner.query( + `DROP TRIGGER duplicate_insert_trigger ON ${tablePrefix}workflow_entity`, + ); + await queryRunner.query( + `DROP TRIGGER duplicate_update_trigger ON ${tablePrefix}workflow_entity`, + ); + await queryRunner.query(`DROP FUNCTION ${tablePrefix}duplicate_data_function() CASCADE`); + await queryRunner.query(`DROP TABLE ${tablePrefix}workflow_entity_with_version CASCADE`); + } +} diff --git a/packages/cli/src/databases/migrations/postgresdb/index.ts b/packages/cli/src/databases/migrations/postgresdb/index.ts index 8c047b84f240a..6c0d7ce16d057 100644 --- a/packages/cli/src/databases/migrations/postgresdb/index.ts +++ b/packages/cli/src/databases/migrations/postgresdb/index.ts @@ -36,6 +36,7 @@ import { UpdateRunningExecutionStatus1677236854063 } from './1677236854063-Updat import { CreateExecutionMetadataTable1679416281778 } from './1679416281778-CreateExecutionMetadataTable'; import { CreateVariables1677501636754 } from './1677501636754-CreateVariables'; import { AddUserActivatedProperty1681134145996 } from './1681134145996-AddUserActivatedProperty'; +import { AddWorkflowEntityWithVersion1681134145997 } from './1681134145997-AddWorkflowEntityWithVersion'; export const postgresMigrations: Migration[] = [ InitialMigration1587669153312, @@ -75,4 +76,5 @@ export const postgresMigrations: Migration[] = [ CreateExecutionMetadataTable1679416281778, CreateVariables1677501636754, AddUserActivatedProperty1681134145996, + AddWorkflowEntityWithVersion1681134145997, ]; diff --git a/packages/cli/src/databases/types.ts b/packages/cli/src/databases/types.ts index 32fb7b0d9c153..7c2dab37f486b 100644 --- a/packages/cli/src/databases/types.ts +++ b/packages/cli/src/databases/types.ts @@ -10,6 +10,7 @@ export interface MigrationContext { dbType: DatabaseType; dbName: string; migrationName: string; + schemaPrefix?: string; } type MigrationFn = (ctx: MigrationContext) => Promise; diff --git a/packages/cli/src/databases/utils/migrationHelpers.ts b/packages/cli/src/databases/utils/migrationHelpers.ts index 21d731e39ae35..e099782acd0b1 100644 --- a/packages/cli/src/databases/utils/migrationHelpers.ts +++ b/packages/cli/src/databases/utils/migrationHelpers.ts @@ -67,8 +67,14 @@ export const wrapMigration = (migration: Migration) => { const dbType = config.getEnv('database.type'); const dbName = config.getEnv(`database.${dbType === 'mariadb' ? 'mysqldb' : dbType}.database`); const tablePrefix = config.getEnv('database.tablePrefix'); + + const schemaPrefix = + config.getEnv('database.type') === 'postgresdb' + ? config.getEnv('database.postgresdb.schema') + : ''; + const migrationName = migration.name; - const context = { tablePrefix, dbType, dbName, migrationName }; + const context = { tablePrefix, dbType, dbName, migrationName, schemaPrefix }; const { up, down } = migration.prototype; Object.assign(migration.prototype, {