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

38 lines
1.5 KiB
TypeScript

import { EncryptionService } from '@modules/encryption/service';
import { OrgEnvironmentConstantValue } from '@entities/org_environment_constant_values.entity';
import { dbTransactionWrap } from '@helpers/database.helper';
import { EntityManager, MigrationInterface, QueryRunner } from 'typeorm';
export class EncryptValuesForExistingOrganizationConstants1709618105790 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const encryptionService = new EncryptionService();
const entityManager = queryRunner.manager;
const organizationConstantValues = await entityManager
.createQueryBuilder(OrgEnvironmentConstantValue, 'orgEnvironmentConstantValue')
.leftJoinAndSelect('orgEnvironmentConstantValue.organizationConstant', 'organizationConstant')
.getMany();
for (const organizationConstantValue of organizationConstantValues) {
const { organizationConstant, value, id } = organizationConstantValue;
const { organizationId } = organizationConstant;
const encryptedValue = await encryptionService.encryptColumnValue(
'org_environment_constant_values',
organizationId,
value
);
await dbTransactionWrap(async (manager: EntityManager) => {
await manager.update(
OrgEnvironmentConstantValue,
{
id,
},
{ value: encryptedValue, updatedAt: new Date() }
);
}, entityManager);
}
}
public async down(queryRunner: QueryRunner): Promise<void> {}
}