Skip to content

Commit

Permalink
Added Postgres migration to duplicate values for workflow-entity-with…
Browse files Browse the repository at this point in the history
…-version (#12)

* Added postgres migration for saving workflow entity versions
  • Loading branch information
omrijoyous authored Jun 19, 2023
1 parent a85c038 commit 68970c5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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`);
}
}
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 @@ -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,
Expand Down Expand Up @@ -75,4 +76,5 @@ export const postgresMigrations: Migration[] = [
CreateExecutionMetadataTable1679416281778,
CreateVariables1677501636754,
AddUserActivatedProperty1681134145996,
AddWorkflowEntityWithVersion1681134145997,
];
1 change: 1 addition & 0 deletions packages/cli/src/databases/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface MigrationContext {
dbType: DatabaseType;
dbName: string;
migrationName: string;
schemaPrefix?: string;
}

type MigrationFn = (ctx: MigrationContext) => Promise<void>;
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/src/databases/utils/migrationHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down

0 comments on commit 68970c5

Please sign in to comment.