Skip to content

Commit

Permalink
separate custom and standard logic to fix command
Browse files Browse the repository at this point in the history
  • Loading branch information
Weiko committed Sep 12, 2024
1 parent 7f7b000 commit 7ba972d
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { FieldMetadataService } from 'src/engine/metadata-modules/field-metadata
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { computeTableName } from 'src/engine/utils/compute-table-name.util';
import { PERSON_STANDARD_FIELD_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-field-ids';
import { ViewService } from 'src/modules/view/services/view.service';
import { ViewFieldWorkspaceEntity } from 'src/modules/view/standard-objects/view-field.workspace-entity';
@Command({
Expand Down Expand Up @@ -76,35 +77,43 @@ export class MigrateEmailFieldsToEmailsCommand extends ActiveWorkspacesCommandRu
);
}

const fieldsWithEmailType = await this.fieldMetadataRepository.find({
where: {
workspaceId,
type: FieldMetadataType.EMAIL,
},
relations: ['object'],
});
const workspaceQueryRunner = workspaceDataSource.createQueryRunner();

await workspaceQueryRunner.connect();

const customFieldsWithEmailType =
await this.fieldMetadataRepository.find({
where: {
workspaceId,
type: FieldMetadataType.EMAIL,
isCustom: true,
},
});

await this.migratePersonEmailFieldToEmailsField(
workspaceId,
workspaceQueryRunner,
dataSourceMetadata,
);

for (const fieldWithEmailType of fieldsWithEmailType) {
for (const customFieldWithEmailType of customFieldsWithEmailType) {
const objectMetadata = await this.objectMetadataRepository.findOne({
where: { id: fieldWithEmailType.objectMetadataId },
where: { id: customFieldWithEmailType.objectMetadataId },
});

if (!objectMetadata) {
throw new Error(
`Could not find objectMetadata for field ${fieldWithEmailType.name}`,
`Could not find objectMetadata for field ${customFieldWithEmailType.name}`,
);
}

this.logger.log(
`Attempting to migrate field ${fieldWithEmailType.name} on ${objectMetadata.nameSingular}.`,
`Attempting to migrate custom field ${customFieldWithEmailType.name} on ${objectMetadata.nameSingular}.`,
);
const workspaceQueryRunner = workspaceDataSource.createQueryRunner();

await workspaceQueryRunner.connect();

const fieldName = fieldWithEmailType.name;
const fieldName = customFieldWithEmailType.name;
const { id: _id, ...fieldWithEmailTypeWithoutId } =
fieldWithEmailType;
customFieldWithEmailType;

const emailDefaultValue = fieldWithEmailTypeWithoutId.defaultValue;

Expand All @@ -130,7 +139,7 @@ export class MigrateEmailFieldsToEmailsCommand extends ActiveWorkspacesCommandRu

// Migrate data from email to emails.primaryEmail
await this.migrateDataWithinTable({
sourceColumnName: `${fieldWithEmailType.name}`,
sourceColumnName: `${customFieldWithEmailType.name}`,
targetColumnName: `${tmpNewEmailsField.name}PrimaryEmail`,
tableName,
workspaceQueryRunner,
Expand All @@ -151,7 +160,7 @@ export class MigrateEmailFieldsToEmailsCommand extends ActiveWorkspacesCommandRu
const viewFieldsWithDeprecatedField =
await viewFieldRepository.find({
where: {
fieldMetadataId: fieldWithEmailType.id,
fieldMetadataId: customFieldWithEmailType.id,
isVisible: true,
},
});
Expand All @@ -177,7 +186,7 @@ export class MigrateEmailFieldsToEmailsCommand extends ActiveWorkspacesCommandRu

// Delete email field
await this.fieldMetadataService.deleteOneField(
{ id: fieldWithEmailType.id },
{ id: customFieldWithEmailType.id },
workspaceId,
);

Expand All @@ -190,11 +199,11 @@ export class MigrateEmailFieldsToEmailsCommand extends ActiveWorkspacesCommandRu
});

this.logger.log(
`Migration of ${fieldWithEmailType.name} on ${objectMetadata.nameSingular} done!`,
`Migration of ${customFieldWithEmailType.name} on ${objectMetadata.nameSingular} done!`,
);
} catch (error) {
this.logger.log(
`Failed to migrate field ${fieldWithEmailType.name} on ${objectMetadata.nameSingular}, rolling back.`,
`Failed to migrate field ${customFieldWithEmailType.name} on ${objectMetadata.nameSingular}, rolling back.`,
);

// Re-create initial field if it was deleted
Expand All @@ -203,8 +212,8 @@ export class MigrateEmailFieldsToEmailsCommand extends ActiveWorkspacesCommandRu
workspaceId,
{
where: {
name: `${fieldWithEmailType.name}`,
objectMetadataId: fieldWithEmailType.objectMetadataId,
name: `${customFieldWithEmailType.name}`,
objectMetadataId: customFieldWithEmailType.objectMetadataId,
},
},
);
Expand All @@ -214,18 +223,18 @@ export class MigrateEmailFieldsToEmailsCommand extends ActiveWorkspacesCommandRu
workspaceId,
{
where: {
name: `${fieldWithEmailType.name}Tmp`,
objectMetadataId: fieldWithEmailType.objectMetadataId,
name: `${customFieldWithEmailType.name}Tmp`,
objectMetadataId: customFieldWithEmailType.objectMetadataId,
},
},
);

if (!initialField) {
this.logger.log(
`Re-creating initial Email field ${fieldWithEmailType.name} but of type emails`, // Cannot create email fields anymore
`Re-creating initial Email field ${customFieldWithEmailType.name} but of type emails`, // Cannot create email fields anymore
);
const restoredField = await this.fieldMetadataService.createOne({
...fieldWithEmailType,
...customFieldWithEmailType,
defaultValue: defaultValueForEmailsField,
type: FieldMetadataType.EMAILS,
});
Expand All @@ -236,7 +245,7 @@ export class MigrateEmailFieldsToEmailsCommand extends ActiveWorkspacesCommandRu

if (tmpNewEmailsField) {
this.logger.log(
`Restoring data in field ${fieldWithEmailType.name}`,
`Restoring data in field ${customFieldWithEmailType.name}`,
);
await this.migrateDataWithinTable({
sourceColumnName: `${tmpNewEmailsField.name}PrimaryEmail`,
Expand All @@ -247,7 +256,7 @@ export class MigrateEmailFieldsToEmailsCommand extends ActiveWorkspacesCommandRu
});
} else {
this.logger.log(
`Failed to restore data in link field ${fieldWithEmailType.name}`,
`Failed to restore data in link field ${customFieldWithEmailType.name}`,
);
}
}
Expand Down Expand Up @@ -275,6 +284,40 @@ export class MigrateEmailFieldsToEmailsCommand extends ActiveWorkspacesCommandRu
}
}

private async migratePersonEmailFieldToEmailsField(
workspaceId: string,
workspaceQueryRunner: any,
dataSourceMetadata: any,
) {
this.logger.log(`Migrating person email field of type EMAIL to EMAILS`);

await this.migrateDataWithinTable({
sourceColumnName: 'email',
targetColumnName: 'emailsPrimaryEmail',
tableName: 'person',
workspaceQueryRunner,
dataSourceMetadata,
});

const personEmailFieldMetadata = await this.fieldMetadataRepository.findOne(
{
where: {
workspaceId,
standardId: PERSON_STANDARD_FIELD_IDS.email,
},
},
);

if (personEmailFieldMetadata) {
await this.fieldMetadataService.deleteOneField(
{
id: personEmailFieldMetadata.id,
},
workspaceId,
);
}
}

private async migrateDataWithinTable({
sourceColumnName,
targetColumnName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ export class UpgradeTo0_30Command extends CommandRunner {
passedParam: string[],
options: UpdateTo0_30CommandOptions,
): Promise<void> {
await this.setCustomObjectIsSoftDeletableCommand.run(passedParam, options);
await this.migrateEmailFieldsToEmails.run(passedParam, options);
await this.syncWorkspaceMetadataCommand.run(passedParam, {
...options,
force: true,
});
await this.setCustomObjectIsSoftDeletableCommand.run(passedParam, options);
await this.migrateEmailFieldsToEmails.run(passedParam, options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { MigrateEmailFieldsToEmailsCommand } from 'src/database/commands/upgrade-version/0-30/0-30-migrate-email-fields-to-emails.command';
import { SetCustomObjectIsSoftDeletableCommand } from 'src/database/commands/upgrade-version/0-30/0-30-set-custom-object-is-soft-deletable.command';
import { UpgradeTo0_30Command } from 'src/database/commands/upgrade-version/0-30/0-30-upgrade-version.command';
import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
Expand All @@ -27,6 +28,10 @@ import { ViewModule } from 'src/modules/view/view.module';
TypeORMModule,
ViewModule,
],
providers: [UpgradeTo0_30Command, MigrateEmailFieldsToEmailsCommand],
providers: [
UpgradeTo0_30Command,
MigrateEmailFieldsToEmailsCommand,
SetCustomObjectIsSoftDeletableCommand,
],
})
export class UpgradeTo0_30CommandModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,5 @@ export class FieldMetadataDefaultValueEmails {

@ValidateIf((_object, value) => value !== null)
@IsObject()
additionalEmails: string[] | null;
additionalEmails: object | null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export const OPPORTUNITY_STANDARD_FIELD_IDS = {
export const PERSON_STANDARD_FIELD_IDS = {
name: '20202020-3875-44d5-8c33-a6239011cab8',
email: '20202020-a740-42bb-8849-8980fb3f12e1',
emails: '16b9b29e-3c51-43fa-8b6e-af39e29368ab',
emails: '20202020-3c51-43fa-8b6e-af39e29368ab',
linkedinLink: '20202020-f1af-48f7-893b-2007a73dd508',
xLink: '20202020-8fc2-487c-b84a-55a99b145cfd',
jobTitle: '20202020-b0d0-415a-bef9-640a26dacd9b',
Expand Down

0 comments on commit 7ba972d

Please sign in to comment.