ToolJet/server/migrations/1683136077244-AddUniqueConstraintToWorkspaceName.ts
Muhsin Shah C P 9257f6feea
Workspace name sanitisation (#6299)
* added unique organization name constraint

* sanitization task: workspace-name

* refactoring the code

* resolved PR changes

* fixed migration & added some checks

* change: trimming workspace name

* handle error messages

* added missing css

* working on the error messages

* change: first workspace's name of the new user

* change: removed underscore from the list

* Tested and fixed first workspace name changes

* Fixing error handler issues

* working on session storage implementation

* revert: session storage

* change: close modal if the edited name is same as prev name

* fixing bugs

* workspace name: refactoring the code

* workspace name: refactoring again

* workspace name: fixing and refactoring code

* fixed e2e test cases

* added catchDbException to catch db lever errors

* added: PR changes

* workspance name: css fix

* updated workspace name constraints and default name

* fix: test cases

* fix: app.e2e tests case

* fix: wrong error message

* fix: last state issue

* reverted some changes

* added workspace name tooltip

* added more tooltip for workspace name

---------

Co-authored-by: gsmithun4 <gsmithun4@gmail.com>
2023-06-01 19:21:38 +05:30

36 lines
1.4 KiB
TypeScript

import { Organization } from 'src/entities/organization.entity';
import { DataBaseConstraints } from 'src/helpers/db_constraints.constants';
import { EntityManager, MigrationInterface, QueryRunner, TableUnique } from 'typeorm';
export class AddUniqueConstraintToWorkspaceName1683136077244 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const entityManager = queryRunner.manager;
await this.migrateWorkspaceNames(entityManager);
await queryRunner.createUniqueConstraint(
'organizations',
new TableUnique({
name: DataBaseConstraints.WORKSPACE_NAME_UNIQUE,
columnNames: ['name'],
})
);
}
public async migrateWorkspaceNames(entityManager: EntityManager) {
const workspaces = await entityManager.query(
'select sub_query.name from (select count(*) as name_count, name from organizations group by name) sub_query where name_count > 1 '
);
for (const workspace of workspaces) {
const { name } = workspace;
const sameNameWorkspaces = await entityManager.find(Organization, { name });
for (const workspaceToChange of sameNameWorkspaces.slice(1)) {
await entityManager.update(
Organization,
{ id: workspaceToChange.id },
{ name: `${workspaceToChange.name} ${Date.now()}` }
);
}
}
}
public async down(queryRunner: QueryRunner): Promise<void> {}
}