forked from n8n-io/n8n
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Postgres migration to duplicate values for workflow-entity-with…
…-version (#12) * Added postgres migration for saving workflow entity versions
- Loading branch information
1 parent
a85c038
commit 68970c5
Showing
4 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...ges/cli/src/databases/migrations/postgresdb/1681134145997-AddWorkflowEntityWithVersion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters