n8n/packages/cli/src/databases/migrations/postgresdb/1669739707126-AddWorkflowVersionIdColumn.ts
कारतोफ्फेलस्क्रिप्ट™ a86c9a628b
refactor(core): Add support for implicit schema in postgres migrations (#5233)
2023-01-24 10:55:20 +01:00

43 lines
1.2 KiB
TypeScript

import { MigrationInterface, QueryRunner } from 'typeorm';
import { v4 as uuidv4 } from 'uuid';
import { getTablePrefix, logMigrationEnd, logMigrationStart } from '@db/utils/migrationHelpers';
export class AddWorkflowVersionIdColumn1669739707126 implements MigrationInterface {
name = 'AddWorkflowVersionIdColumn1669739707126';
async up(queryRunner: QueryRunner) {
logMigrationStart(this.name);
const tablePrefix = getTablePrefix();
await queryRunner.query(
`ALTER TABLE ${tablePrefix}workflow_entity ADD COLUMN "versionId" CHAR(36)`,
);
const workflowIds: Array<{ id: number }> = await queryRunner.query(`
SELECT id
FROM ${tablePrefix}workflow_entity
`);
workflowIds.map(({ id }) => {
const [updateQuery, updateParams] = queryRunner.connection.driver.escapeQueryWithParameters(
`
UPDATE ${tablePrefix}workflow_entity
SET "versionId" = :versionId
WHERE id = '${id}'
`,
{ versionId: uuidv4() },
{},
);
return queryRunner.query(updateQuery, updateParams);
});
logMigrationEnd(this.name);
}
async down(queryRunner: QueryRunner) {
const tablePrefix = getTablePrefix();
await queryRunner.query(`ALTER TABLE ${tablePrefix}workflow_entity DROP COLUMN "versionId"`);
}
}