ToolJet/server/migrations/1684145489093-AddUniqueConstraintToFolderName.ts
Muhsin Shah C P a455a50e98
Sanitising workspace folder name while creating or editing a folder (#6396)
* added: frontend validation

* added attributes and constraints to folder name

* fixed migration

* replaced isAlphanumeric decorator with regex matches

* change: handle unwanted spaces

* handle error messages

* reverted css change

* folder name: adding error messages, refactoring code

* adding: PR changes

* fix: test cases

* fix: test cases

* changed max-length

* added PR changes and fixes

* added common error css change

* changed error message

* adding folder name to the prompt

* updated folder constraints

* updated maximum length

* fix: empty check

* added : folder dto name empty message

* fixed failing test cases

* added folder tooltip
2023-06-07 15:03:54 +05:30

42 lines
1.7 KiB
TypeScript

import { Folder } from 'src/entities/folder.entity';
import { Organization } from 'src/entities/organization.entity';
import { EntityManager, MigrationInterface, QueryRunner, TableUnique } from 'typeorm';
import { DataBaseConstraints } from 'src/helpers/db_constraints.constants';
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);
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()}` }
);
}
}
}
}
public async down(queryRunner: QueryRunner): Promise<void> {}
}