2022-08-03 04:06:53 -07:00
|
|
|
import { MigrationInterface, QueryRunner } from 'typeorm';
|
2022-11-09 06:25:00 -08:00
|
|
|
import config from '@/config';
|
|
|
|
import { runInBatches } from '@db/utils/migrationHelpers';
|
2022-08-03 04:06:53 -07:00
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
|
|
|
|
// add node ids in workflow objects
|
|
|
|
|
|
|
|
export class AddNodeIds1658932090381 implements MigrationInterface {
|
|
|
|
name = 'AddNodeIds1658932090381';
|
|
|
|
|
|
|
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
|
|
|
let tablePrefix = config.getEnv('database.tablePrefix');
|
|
|
|
const schema = config.getEnv('database.postgresdb.schema');
|
|
|
|
if (schema) {
|
|
|
|
tablePrefix = schema + '.' + tablePrefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
await queryRunner.query(`SET search_path TO ${schema};`);
|
|
|
|
|
|
|
|
const workflowsQuery = `
|
|
|
|
SELECT id, nodes
|
|
|
|
FROM ${tablePrefix}workflow_entity
|
|
|
|
`;
|
|
|
|
|
|
|
|
// @ts-ignore
|
2022-08-22 08:46:22 -07:00
|
|
|
await runInBatches(queryRunner, workflowsQuery, (workflows) => {
|
2022-08-03 04:06:53 -07:00
|
|
|
workflows.forEach(async (workflow) => {
|
|
|
|
const nodes = workflow.nodes;
|
|
|
|
// @ts-ignore
|
|
|
|
nodes.forEach((node) => {
|
|
|
|
if (!node.id) {
|
|
|
|
node.id = uuid();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-08-22 08:46:22 -07:00
|
|
|
const [updateQuery, updateParams] = queryRunner.connection.driver.escapeQueryWithParameters(
|
|
|
|
`
|
2022-08-03 04:06:53 -07:00
|
|
|
UPDATE ${tablePrefix}workflow_entity
|
|
|
|
SET nodes = :nodes
|
|
|
|
WHERE id = '${workflow.id}'
|
|
|
|
`,
|
2022-08-22 08:46:22 -07:00
|
|
|
{ nodes: JSON.stringify(nodes) },
|
|
|
|
{},
|
|
|
|
);
|
2022-08-03 04:06:53 -07:00
|
|
|
|
|
|
|
queryRunner.query(updateQuery, updateParams);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
|
|
|
let tablePrefix = config.getEnv('database.tablePrefix');
|
|
|
|
const schema = config.getEnv('database.postgresdb.schema');
|
|
|
|
if (schema) {
|
|
|
|
tablePrefix = schema + '.' + tablePrefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
await queryRunner.query(`SET search_path TO ${schema};`);
|
|
|
|
|
|
|
|
const workflowsQuery = `
|
|
|
|
SELECT id, nodes
|
|
|
|
FROM ${tablePrefix}workflow_entity
|
|
|
|
`;
|
|
|
|
|
|
|
|
// @ts-ignore
|
2022-08-22 08:46:22 -07:00
|
|
|
await runInBatches(queryRunner, workflowsQuery, (workflows) => {
|
2022-08-03 04:06:53 -07:00
|
|
|
workflows.forEach(async (workflow) => {
|
|
|
|
const nodes = workflow.nodes;
|
|
|
|
// @ts-ignore
|
2022-08-22 08:46:22 -07:00
|
|
|
nodes.forEach((node) => delete node.id);
|
2022-08-03 04:06:53 -07:00
|
|
|
|
2022-08-22 08:46:22 -07:00
|
|
|
const [updateQuery, updateParams] = queryRunner.connection.driver.escapeQueryWithParameters(
|
|
|
|
`
|
2022-08-03 04:06:53 -07:00
|
|
|
UPDATE ${tablePrefix}workflow_entity
|
|
|
|
SET nodes = :nodes
|
|
|
|
WHERE id = '${workflow.id}'
|
|
|
|
`,
|
2022-08-22 08:46:22 -07:00
|
|
|
{ nodes: JSON.stringify(nodes) },
|
|
|
|
{},
|
|
|
|
);
|
2022-08-03 04:06:53 -07:00
|
|
|
|
|
|
|
queryRunner.query(updateQuery, updateParams);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|