* ⚡ Set path alias for config * ⚡ Update config export * ⚡ Simplify config imports * ⚡ Update also additional imports * ⚡ Update path in collation migration * ⚡ Resolve aliased paths * 👕 Fix Codacy issue * 👕 Retry to fix Codacy issue Co-authored-by: Jan <janober@users.noreply.github.com> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
48 lines
1.6 KiB
TypeScript
48 lines
1.6 KiB
TypeScript
import {MigrationInterface, QueryRunner} from "typeorm";
|
|
import { config } from '@config';
|
|
|
|
export class UniqueWorkflowNames1620821879465 implements MigrationInterface {
|
|
name = 'UniqueWorkflowNames1620821879465';
|
|
|
|
async up(queryRunner: QueryRunner): Promise<void> {
|
|
const tablePrefix = config.get('database.tablePrefix');
|
|
|
|
const workflowNames = await queryRunner.query(`
|
|
SELECT name
|
|
FROM "${tablePrefix}workflow_entity"
|
|
`);
|
|
|
|
for (const { name } of workflowNames) {
|
|
const [duplicatesQuery, parameters] = queryRunner.connection.driver.escapeQueryWithParameters(`
|
|
SELECT id, name
|
|
FROM "${tablePrefix}workflow_entity"
|
|
WHERE name = :name
|
|
ORDER BY createdAt ASC
|
|
`, { name }, {});
|
|
|
|
const duplicates = await queryRunner.query(duplicatesQuery, parameters);
|
|
|
|
if (duplicates.length > 1) {
|
|
await Promise.all(duplicates.map(({ id, name }: { id: number; name: string; }, index: number) => {
|
|
if (index === 0) return Promise.resolve();
|
|
const [updateQuery, updateParams] = queryRunner.connection.driver.escapeQueryWithParameters(`
|
|
UPDATE "${tablePrefix}workflow_entity"
|
|
SET name = :name
|
|
WHERE id = '${id}'
|
|
`, { name: `${name} ${index + 1}`}, {});
|
|
|
|
return queryRunner.query(updateQuery, updateParams);
|
|
}));
|
|
}
|
|
}
|
|
|
|
await queryRunner.query(`CREATE UNIQUE INDEX "IDX_${tablePrefix}943d8f922be094eb507cb9a7f9" ON "${tablePrefix}workflow_entity" ("name") `);
|
|
}
|
|
|
|
async down(queryRunner: QueryRunner): Promise<void> {
|
|
const tablePrefix = config.get('database.tablePrefix');
|
|
await queryRunner.query(`DROP INDEX "IDX_${tablePrefix}943d8f922be094eb507cb9a7f9"`);
|
|
}
|
|
|
|
}
|