ToolJet/server/data-migrations/1684157120658-AddUniqueConstraintToAppName.ts
Muhsin Shah C P e7bb22e649
[enhancement] Sanitising app name while creating or editing an app (#6423)
* working on app name unique migrationworking on app name unique migration

* Added checks and conditions

* change: remove staring and ending spaces

* fix: error management

* fix: regex

* working on app name generation

* app name:fixing bugs

* app name: fixing and refactoring code

* app name: fixed test cases

* updated app name constraints and default name

* fix: apps.e2e tests case

* fix: added app name to the prompt

* added app input tooltip

* removed unwanted code

* added tooltip close code

* fixed auto generated name issue

* merging develop

* code refactoring

---------

Co-authored-by: gsmithun4 <gsmithun4@gmail.com>
2023-06-06 14:12:36 +05:30

38 lines
1.6 KiB
TypeScript

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