ToolJet/server/data-migrations/1684145489093-AddUniqueConstraintToFolderName.ts
2025-02-25 12:22:50 +05:30

47 lines
1.8 KiB
TypeScript

import { Folder } from '@entities/folder.entity';
import { Organization } from '@entities/organization.entity';
import { EntityManager, MigrationInterface, QueryRunner, TableUnique } from 'typeorm';
import { DataBaseConstraints } from '@helpers/db_constraints.constants';
import { addWait } from '@helpers/migration.helper';
export class AddUniqueConstraintToFolderName1684145489093 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const entityManager = queryRunner.manager;
await this.migrateFolderNames(entityManager);
await queryRunner.createUniqueConstraint(
'folders',
new TableUnique({
name: DataBaseConstraints.FOLDER_NAME_UNIQUE,
columnNames: ['name', 'organization_id'],
})
);
}
public async migrateFolderNames(entityManager: EntityManager) {
const workspaces = await entityManager.find(Organization, {
select: ['id'],
});
for (const workspace of workspaces) {
const { id: organizationId } = workspace;
const folders = await entityManager.query(
'select sub_query.name from (select count(*) as name_count, name from folders where organization_id=$1 group by name) sub_query where name_count > 1 ',
[organizationId]
);
for (const folder of folders) {
const { name } = folder;
const sameFolders = await entityManager.find(Folder, { where: { name, organizationId } });
for (const folderToChange of sameFolders.slice(1)) {
await entityManager.update(
Folder,
{ id: folderToChange.id },
{ name: `${folderToChange.name} ${Date.now()}` }
);
// Add 1 millisecond wait to prevent duplicate timestamp generation
addWait(1);
}
}
}
}
public async down(queryRunner: QueryRunner): Promise<void> {}
}