ToolJet/server/migrations/1655726247638-AddUniqueConstraintToVersionName.ts

47 lines
1.6 KiB
TypeScript
Raw Normal View History

import { AppVersion } from 'src/entities/app_version.entity';
import { EntityManager, MigrationInterface, QueryRunner, TableUnique } from 'typeorm';
export class AddUniqueConstraintToVersionName1655726247638 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const entityManager = queryRunner.manager;
await this.migrateVersions(entityManager);
await queryRunner.createUniqueConstraint(
'app_versions',
new TableUnique({
name: 'name_app_id_app_versions_unique',
columnNames: ['name', 'app_id'],
})
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropUniqueConstraint('app_versions', 'name_app_id_app_versions_unique');
}
private async migrateVersions(entityManager: EntityManager) {
[Improvements] Environment changes (#6762) * Added priority, enabled, current_environment_id columns - added data-migrations to backfill the priority and current environment id for each version * working on multi environments improvement - added checks for promoted env - promote env feature - released app & production env check - promoted version definition check * added import support * working on migration issues * fixed failing migration * fixed failing migration once again * Fixed possible bugs - seed - test case helpers - app version release * fixed migration bug * working on migration progress * working on migration progress class * added migration progress * fixed unit tests * fixed e2e cases * added default priority * added PR changes * changed import logic * added global datasource design and api changes * changed default env if the id is null * added unique constraint and a bug fix * changed app versions api - added current environment id to the where conditions * fixed failing test cases * added new test cases * added new api changes * added back the enabled check * fixed test case * change: added environment to create versions dto * typo: environmentId * added new api for fetching versions * added appVersions count changes to CE * Changed app versions by environmentid logic * added PR changes for EE * fixed wrong promoted env id issue * fix: can't switch to staging * added import export changes - update delete modal text * added EE import export code, modal updated text * added common migration code for CE and EE * fixes - enable run button for released version - disable change datasource for queries * changed released version popup design to new EE design * add: hide delete icons for released version
2023-07-11 04:40:03 +00:00
const appVersions = await entityManager.find(AppVersion, {
select: ['name', 'appId', 'id'],
});
for (const version of appVersions) {
const { name, appId, id } = version;
[Improvements] Environment changes (#6762) * Added priority, enabled, current_environment_id columns - added data-migrations to backfill the priority and current environment id for each version * working on multi environments improvement - added checks for promoted env - promote env feature - released app & production env check - promoted version definition check * added import support * working on migration issues * fixed failing migration * fixed failing migration once again * Fixed possible bugs - seed - test case helpers - app version release * fixed migration bug * working on migration progress * working on migration progress class * added migration progress * fixed unit tests * fixed e2e cases * added default priority * added PR changes * changed import logic * added global datasource design and api changes * changed default env if the id is null * added unique constraint and a bug fix * changed app versions api - added current environment id to the where conditions * fixed failing test cases * added new test cases * added new api changes * added back the enabled check * fixed test case * change: added environment to create versions dto * typo: environmentId * added new api for fetching versions * added appVersions count changes to CE * Changed app versions by environmentid logic * added PR changes for EE * fixed wrong promoted env id issue * fix: can't switch to staging * added import export changes - update delete modal text * added EE import export code, modal updated text * added common migration code for CE and EE * fixes - enable run button for released version - disable change datasource for queries * changed released version popup design to new EE design * add: hide delete icons for released version
2023-07-11 04:40:03 +00:00
const versionsWithSameName = await entityManager.find(AppVersion, {
where: {
appId,
name,
},
select: ['name', 'appId', 'id'],
});
if (versionsWithSameName.length > 1) {
const versionsNeedToChange = versionsWithSameName.filter((appVersion) => appVersion.id !== id);
for (const versionToChange of versionsNeedToChange) {
await entityManager.update(
AppVersion,
{ id: versionToChange.id },
{ name: `${versionToChange.name}_${Date.now()}` }
);
}
}
}
}
}