Remove deprecated EMAIL, PHONE, LINK (#7551)

In this PR:
- remove deprecated EMAIL, PHONE, LINK field types (except for Zapier
package as there is another work ongoing)
- remove composite currency filter on currencyCode, actor filter on name
and workspaceMember as the UX is not great yet
This commit is contained in:
Charles Bochet
2024-10-10 14:14:58 +02:00
committed by GitHub
parent a7d5aa933d
commit a58236e6da
87 changed files with 75 additions and 2746 deletions

View File

@ -7,7 +7,6 @@ import { DataSeedDemoWorkspaceCommand } from 'src/database/commands/data-seed-de
import { DataSeedDemoWorkspaceModule } from 'src/database/commands/data-seed-demo-workspace/data-seed-demo-workspace.module';
import { DataSeedWorkspaceCommand } from 'src/database/commands/data-seed-dev-workspace.command';
import { ConfirmationQuestion } from 'src/database/commands/questions/confirmation.question';
import { UpgradeTo0_30CommandModule } from 'src/database/commands/upgrade-version/0-30/0-30-upgrade-version.module';
import { UpgradeTo0_31CommandModule } from 'src/database/commands/upgrade-version/0-31/0-31-upgrade-version.module';
import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
import { BillingSubscription } from 'src/engine/core-modules/billing/entities/billing-subscription.entity';
@ -47,7 +46,6 @@ import { WorkspaceSyncMetadataModule } from 'src/engine/workspace-manager/worksp
DataSeedDemoWorkspaceModule,
WorkspaceCacheStorageModule,
WorkspaceMetadataVersionModule,
UpgradeTo0_30CommandModule,
UpgradeTo0_31CommandModule,
FeatureFlagModule,
],

View File

@ -1,161 +0,0 @@
import { InjectRepository } from '@nestjs/typeorm';
import chalk from 'chalk';
import { isDefined } from 'class-validator';
import { Command } from 'nest-commander';
import { Repository } from 'typeorm';
import {
ActiveWorkspacesCommandOptions,
ActiveWorkspacesCommandRunner,
} from 'src/database/commands/active-workspaces.command';
import { TypeORMService } from 'src/database/typeorm/typeorm.service';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { FieldMetadataService } from 'src/engine/metadata-modules/field-metadata/field-metadata.service';
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';
@Command({
name: 'upgrade-0.30:fix-email-field-migration',
description:
'Fix migration - delete deprecated email fields and add emails to person views',
})
export class FixEmailFieldsToEmailsCommand extends ActiveWorkspacesCommandRunner {
constructor(
@InjectRepository(Workspace, 'core')
protected readonly workspaceRepository: Repository<Workspace>,
@InjectRepository(FieldMetadataEntity, 'metadata')
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>,
private readonly fieldMetadataService: FieldMetadataService,
private readonly typeORMService: TypeORMService,
private readonly dataSourceService: DataSourceService,
private readonly viewService: ViewService,
) {
super(workspaceRepository);
}
async executeActiveWorkspacesCommand(
_passedParam: string[],
_options: ActiveWorkspacesCommandOptions,
workspaceIds: string[],
): Promise<void> {
this.logger.log('Running command to fix migration');
for (const workspaceId of workspaceIds) {
let dataSourceMetadata;
this.logger.log(`Running command for workspace ${workspaceId}`);
try {
dataSourceMetadata =
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceId(
workspaceId,
);
if (!dataSourceMetadata) {
throw new Error(
`Could not find dataSourceMetadata for workspace ${workspaceId}`,
);
}
const workspaceDataSource =
await this.typeORMService.connectToDataSource(dataSourceMetadata);
if (!workspaceDataSource) {
throw new Error(
`Could not connect to dataSource for workspace ${workspaceId}`,
);
}
} catch (error) {
this.logger.log(
chalk.red(
`Could not connect to workspace data source for workspace ${workspaceId}`,
),
);
continue;
}
try {
const deprecatedPersonEmailFieldsMetadata =
await this.fieldMetadataRepository.findBy({
standardId: PERSON_STANDARD_FIELD_IDS.email,
workspaceId: workspaceId,
});
const migratedEmailFieldMetadata = await this.fieldMetadataRepository
.findBy({
standardId: PERSON_STANDARD_FIELD_IDS.emails,
workspaceId: workspaceId,
})
.then((fields) => fields[0]);
const personEmailFieldWasMigratedButHasDuplicate =
deprecatedPersonEmailFieldsMetadata.length > 0 &&
isDefined(migratedEmailFieldMetadata);
if (!personEmailFieldWasMigratedButHasDuplicate) {
this.logger.log(
chalk.yellow('No fields to migrate for workspace ' + workspaceId),
);
continue;
}
for (const deprecatedEmailFieldMetadata of deprecatedPersonEmailFieldsMetadata) {
await this.fieldMetadataService.deleteOneField(
{ id: deprecatedEmailFieldMetadata.id },
workspaceId,
);
this.logger.log(
chalk.green(`Deleted email field for workspace ${workspaceId}.`),
);
}
const personObjectMetadaIdForWorkspace =
migratedEmailFieldMetadata.objectMetadataId;
if (!isDefined(personObjectMetadaIdForWorkspace)) {
this.logger.log(
chalk.red(
`Could not find person object for workspace ${workspaceId}. Could not add emails to person view`,
),
);
continue;
}
const personViewsIds =
await this.viewService.getViewsIdsForObjectMetadataId({
workspaceId,
objectMetadataId: personObjectMetadaIdForWorkspace as string,
});
await this.viewService.addFieldToViews({
workspaceId: workspaceId,
fieldId: migratedEmailFieldMetadata.id,
viewsIds: personViewsIds,
positions: personViewsIds.reduce((acc, personView) => {
if (!personView.id) {
return acc;
}
acc[personView.id] = 4;
return acc;
}, []),
});
this.logger.log(chalk.green(`Added emails to view ${workspaceId}.`));
} catch (error) {
this.logger.log(
chalk.red(
`Running command on workspace ${workspaceId} failed with error: ${error}`,
),
);
continue;
} finally {
this.logger.log(
chalk.green(`Finished running command for workspace ${workspaceId}.`),
);
}
this.logger.log(chalk.green(`Command completed!`));
}
}
}

View File

@ -1,110 +0,0 @@
import { InjectRepository } from '@nestjs/typeorm';
import chalk from 'chalk';
import { Command } from 'nest-commander';
import { Any, Repository } from 'typeorm';
import {
ActiveWorkspacesCommandOptions,
ActiveWorkspacesCommandRunner,
} from 'src/database/commands/active-workspaces.command';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import {
FieldMetadataEntity,
FieldMetadataType,
} from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { ViewFilterWorkspaceEntity } from 'src/modules/view/standard-objects/view-filter.workspace-entity';
@Command({
name: 'upgrade-0.30:fix-view-filter-operand-for-date-time',
description: 'Fix the view filter operand for date time fields',
})
export class FixViewFilterOperandForDateTimeCommand extends ActiveWorkspacesCommandRunner {
constructor(
@InjectRepository(Workspace, 'core')
protected readonly workspaceRepository: Repository<Workspace>,
@InjectRepository(FieldMetadataEntity, 'metadata')
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>,
private readonly dataSourceService: DataSourceService,
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
) {
super(workspaceRepository);
}
async executeActiveWorkspacesCommand(
_passedParam: string[],
_options: ActiveWorkspacesCommandOptions,
workspaceIds: string[],
): Promise<void> {
for (const workspaceId of workspaceIds) {
try {
const dataSourceMetadata =
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceId(
workspaceId,
);
if (!dataSourceMetadata) {
throw new Error(
`Could not find dataSourceMetadata for workspace ${workspaceId}`,
);
}
const viewFilterRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<ViewFilterWorkspaceEntity>(
workspaceId,
'viewFilter',
);
const dateTimeFieldMetadata = await this.fieldMetadataRepository.find({
where: {
workspaceId,
type: FieldMetadataType.DATE_TIME,
},
});
const dateTimeFieldMetadataIds = dateTimeFieldMetadata.map(
(fieldMetadata) => fieldMetadata.id,
);
const lessThanUpdatedResult = await viewFilterRepository.update(
{
operand: 'lessThan',
fieldMetadataId: Any(dateTimeFieldMetadataIds),
},
{
operand: 'isBefore',
},
);
const greaterThanUpdatedResult = await viewFilterRepository.update(
{
operand: 'greaterThan',
fieldMetadataId: Any(dateTimeFieldMetadataIds),
},
{
operand: 'isAfter',
},
);
this.logger.log(
`Updated ${(lessThanUpdatedResult.affected ?? 0) + (greaterThanUpdatedResult.affected ?? 0)} view filters for workspace ${workspaceId}`,
);
} catch (error) {
this.logger.log(
chalk.red(
`Error running command for workspace ${workspaceId}: ${error}`,
),
);
continue;
} finally {
this.logger.log(
chalk.green(`Finished running command for workspace ${workspaceId}.`),
);
}
this.logger.log(chalk.green(`Command completed!`));
}
}
}

View File

@ -1,360 +0,0 @@
import { InjectRepository } from '@nestjs/typeorm';
import chalk from 'chalk';
import { Command } from 'nest-commander';
import { QueryRunner, Repository } from 'typeorm';
import {
ActiveWorkspacesCommandOptions,
ActiveWorkspacesCommandRunner,
} from 'src/database/commands/active-workspaces.command';
import { TypeORMService } from 'src/database/typeorm/typeorm.service';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-source.entity';
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import { CreateFieldInput } from 'src/engine/metadata-modules/field-metadata/dtos/create-field.input';
import {
FieldMetadataEntity,
FieldMetadataType,
} from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { FieldMetadataService } from 'src/engine/metadata-modules/field-metadata/field-metadata.service';
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({
name: 'upgrade-0.30:migrate-email-fields-to-emails',
description: 'Migrating fields of deprecated type EMAIL to type EMAILS',
})
export class MigrateEmailFieldsToEmailsCommand extends ActiveWorkspacesCommandRunner {
constructor(
@InjectRepository(Workspace, 'core')
protected readonly workspaceRepository: Repository<Workspace>,
@InjectRepository(FieldMetadataEntity, 'metadata')
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>,
@InjectRepository(ObjectMetadataEntity, 'metadata')
private readonly objectMetadataRepository: Repository<ObjectMetadataEntity>,
private readonly fieldMetadataService: FieldMetadataService,
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
private readonly typeORMService: TypeORMService,
private readonly dataSourceService: DataSourceService,
private readonly viewService: ViewService,
) {
super(workspaceRepository);
}
async executeActiveWorkspacesCommand(
_passedParam: string[],
_options: ActiveWorkspacesCommandOptions,
workspaceIds: string[],
): Promise<void> {
this.logger.log(
'Running command to migrate email type fields to emails type',
);
for (const workspaceId of workspaceIds) {
let dataSourceMetadata;
let workspaceQueryRunner;
this.logger.log(`Running command for workspace ${workspaceId}`);
try {
dataSourceMetadata =
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceId(
workspaceId,
);
if (!dataSourceMetadata) {
throw new Error(
`Could not find dataSourceMetadata for workspace ${workspaceId}`,
);
}
const workspaceDataSource =
await this.typeORMService.connectToDataSource(dataSourceMetadata);
if (!workspaceDataSource) {
throw new Error(
`Could not connect to dataSource for workspace ${workspaceId}`,
);
}
workspaceQueryRunner = workspaceDataSource.createQueryRunner();
await workspaceQueryRunner.connect();
} catch (error) {
this.logger.log(
chalk.red(
`Could not connect to workspace data source for workspace ${workspaceId}`,
),
);
continue;
}
try {
await this.migratePersonEmailFieldToEmailsField(
workspaceId,
workspaceQueryRunner,
dataSourceMetadata,
);
const customFieldsWithEmailType =
await this.fieldMetadataRepository.find({
where: {
workspaceId,
type: FieldMetadataType.EMAIL,
isCustom: true,
},
});
for (const customFieldWithEmailType of customFieldsWithEmailType) {
const objectMetadata = await this.objectMetadataRepository.findOne({
where: { id: customFieldWithEmailType.objectMetadataId },
});
if (!objectMetadata) {
throw new Error(
`Could not find objectMetadata for field ${customFieldWithEmailType.name}`,
);
}
this.logger.log(
`Attempting to migrate custom field ${customFieldWithEmailType.name} on ${objectMetadata.nameSingular}.`,
);
const fieldName = customFieldWithEmailType.name;
const { id: _id, ...fieldWithEmailTypeWithoutId } =
customFieldWithEmailType;
const emailDefaultValue = fieldWithEmailTypeWithoutId.defaultValue;
const defaultValueForEmailsField = {
primaryEmail: emailDefaultValue,
additionalEmails: null,
};
try {
const tmpNewEmailsField = await this.fieldMetadataService.createOne(
{
...fieldWithEmailTypeWithoutId,
type: FieldMetadataType.EMAILS,
defaultValue: defaultValueForEmailsField,
name: `${fieldName}Tmp`,
} satisfies CreateFieldInput,
);
const tableName = computeTableName(
objectMetadata.nameSingular,
objectMetadata.isCustom,
);
// Migrate data from email to emails.primaryEmail
await this.migrateDataWithinTable({
sourceColumnName: `${customFieldWithEmailType.name}`,
targetColumnName: `${tmpNewEmailsField.name}PrimaryEmail`,
tableName,
workspaceQueryRunner,
dataSourceMetadata,
});
// Duplicate email field's views behaviour for new emails field
await this.viewService.removeFieldFromViews({
workspaceId: workspaceId,
fieldId: tmpNewEmailsField.id,
});
const viewFieldRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<ViewFieldWorkspaceEntity>(
workspaceId,
'viewField',
);
const viewFieldsWithDeprecatedField =
await viewFieldRepository.find({
where: {
fieldMetadataId: customFieldWithEmailType.id,
isVisible: true,
},
});
await this.viewService.addFieldToViews({
workspaceId: workspaceId,
fieldId: tmpNewEmailsField.id,
viewsIds: viewFieldsWithDeprecatedField
.filter((viewField) => viewField.viewId !== null)
.map((viewField) => viewField.viewId as string),
positions: viewFieldsWithDeprecatedField.reduce(
(acc, viewField) => {
if (!viewField.viewId) {
return acc;
}
acc[viewField.viewId] = viewField.position;
return acc;
},
[],
),
});
// Delete email field
await this.fieldMetadataService.deleteOneField(
{ id: customFieldWithEmailType.id },
workspaceId,
);
// Rename temporary emails field
await this.fieldMetadataService.updateOne(tmpNewEmailsField.id, {
id: tmpNewEmailsField.id,
workspaceId: tmpNewEmailsField.workspaceId,
name: `${fieldName}`,
isCustom: false,
});
this.logger.log(
`Migration of ${customFieldWithEmailType.name} on ${objectMetadata.nameSingular} done!`,
);
} catch (error) {
this.logger.log(
`Failed to migrate field ${customFieldWithEmailType.name} on ${objectMetadata.nameSingular}, rolling back.`,
);
// Re-create initial field if it was deleted
const initialField =
await this.fieldMetadataService.findOneWithinWorkspace(
workspaceId,
{
where: {
name: `${customFieldWithEmailType.name}`,
objectMetadataId: customFieldWithEmailType.objectMetadataId,
},
},
);
const tmpNewEmailsField =
await this.fieldMetadataService.findOneWithinWorkspace(
workspaceId,
{
where: {
name: `${customFieldWithEmailType.name}Tmp`,
objectMetadataId: customFieldWithEmailType.objectMetadataId,
},
},
);
if (!initialField) {
this.logger.log(
`Re-creating initial Email field ${customFieldWithEmailType.name} but of type emails`, // Cannot create email fields anymore
);
const restoredField = await this.fieldMetadataService.createOne({
...customFieldWithEmailType,
defaultValue: defaultValueForEmailsField,
type: FieldMetadataType.EMAILS,
});
const tableName = computeTableName(
objectMetadata.nameSingular,
objectMetadata.isCustom,
);
if (tmpNewEmailsField) {
this.logger.log(
`Restoring data in field ${customFieldWithEmailType.name}`,
);
await this.migrateDataWithinTable({
sourceColumnName: `${tmpNewEmailsField.name}PrimaryEmail`,
targetColumnName: `${restoredField.name}PrimaryEmail`,
tableName,
workspaceQueryRunner,
dataSourceMetadata,
});
} else {
this.logger.log(
`Failed to restore data in link field ${customFieldWithEmailType.name}`,
);
}
}
if (tmpNewEmailsField) {
await this.fieldMetadataService.deleteOneField(
{ id: tmpNewEmailsField.id },
workspaceId,
);
}
}
}
} catch (error) {
await workspaceQueryRunner.release();
this.logger.log(
chalk.red(
`Running command on workspace ${workspaceId} failed with error: ${error}`,
),
);
continue;
} finally {
await workspaceQueryRunner.release();
}
this.logger.log(chalk.green(`Command completed!`));
}
}
private async migratePersonEmailFieldToEmailsField(
workspaceId: string,
workspaceQueryRunner: any,
dataSourceMetadata: any,
) {
this.logger.log(`Migrating person email field of type EMAIL to EMAILS`);
const personEmailFieldMetadata = await this.fieldMetadataRepository.findOne(
{
where: {
workspaceId,
standardId: PERSON_STANDARD_FIELD_IDS.email,
},
},
);
if (!personEmailFieldMetadata) {
this.logger.log(
`Could not find person email field with standardId ${PERSON_STANDARD_FIELD_IDS.email}, skipping migration`,
);
return;
}
await this.migrateDataWithinTable({
sourceColumnName: 'email',
targetColumnName: 'emailsPrimaryEmail',
tableName: 'person',
workspaceQueryRunner,
dataSourceMetadata,
});
if (personEmailFieldMetadata) {
await this.fieldMetadataService.deleteOneField(
{
id: personEmailFieldMetadata.id,
},
workspaceId,
);
}
}
private async migrateDataWithinTable({
sourceColumnName,
targetColumnName,
tableName,
workspaceQueryRunner,
dataSourceMetadata,
}: {
sourceColumnName: string;
targetColumnName: string;
tableName: string;
workspaceQueryRunner: QueryRunner;
dataSourceMetadata: DataSourceEntity;
}) {
await workspaceQueryRunner.query(
`UPDATE "${dataSourceMetadata.schema}"."${tableName}" SET "${targetColumnName}" = "${sourceColumnName}"`,
);
}
}

View File

@ -1,366 +0,0 @@
import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
import chalk from 'chalk';
import { isDefined, isEmpty } from 'class-validator';
import { parsePhoneNumber } from 'libphonenumber-js';
import { Command } from 'nest-commander';
import { DataSource, QueryRunner, Repository } from 'typeorm';
import {
ActiveWorkspacesCommandOptions,
ActiveWorkspacesCommandRunner,
} from 'src/database/commands/active-workspaces.command';
import { TypeORMService } from 'src/database/typeorm/typeorm.service';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import { CreateFieldInput } from 'src/engine/metadata-modules/field-metadata/dtos/create-field.input';
import {
FieldMetadataEntity,
FieldMetadataType,
} from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { FieldMetadataService } from 'src/engine/metadata-modules/field-metadata/field-metadata.service';
import { computeColumnName } from 'src/engine/metadata-modules/field-metadata/utils/compute-column-name.util';
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';
type MigratePhoneFieldsToPhonesCommandOptions = ActiveWorkspacesCommandOptions;
@Command({
name: 'upgrade-0.30:migrate-phone-fields-to-phones',
description: 'Migrating fields of deprecated type PHONE to type PHONES',
})
export class MigratePhoneFieldsToPhonesCommand extends ActiveWorkspacesCommandRunner {
constructor(
@InjectRepository(Workspace, 'core')
protected readonly workspaceRepository: Repository<Workspace>,
@InjectRepository(FieldMetadataEntity, 'metadata')
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>,
@InjectRepository(ObjectMetadataEntity, 'metadata')
private readonly objectMetadataRepository: Repository<ObjectMetadataEntity>,
@InjectDataSource('metadata')
private readonly metadataDataSource: DataSource,
private readonly fieldMetadataService: FieldMetadataService,
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
private readonly typeORMService: TypeORMService,
private readonly dataSourceService: DataSourceService,
private readonly viewService: ViewService,
) {
super(workspaceRepository);
}
async executeActiveWorkspacesCommand(
_passedParam: string[],
_options: MigratePhoneFieldsToPhonesCommandOptions,
workspaceIds: string[],
): Promise<void> {
this.logger.log(
'Running command to migrate phone type fields to phones type',
);
for (const workspaceId of workspaceIds) {
this.logger.log(`Running command for workspace ${workspaceId}`);
try {
const dataSourceMetadata =
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceId(
workspaceId,
);
if (!dataSourceMetadata) {
throw new Error(
`Could not find dataSourceMetadata for workspace ${workspaceId}`,
);
}
const workspaceDataSource =
await this.typeORMService.connectToDataSource(dataSourceMetadata);
if (!workspaceDataSource) {
throw new Error(
`Could not connect to dataSource for workspace ${workspaceId}`,
);
}
const standardPersonPhoneFieldWithTextType =
await this.fieldMetadataRepository.findOneBy({
workspaceId,
standardId: PERSON_STANDARD_FIELD_IDS.phone,
});
if (!standardPersonPhoneFieldWithTextType) {
throw new Error(
`Could not find standard phone field on person for workspace ${workspaceId}`,
);
}
await this.migrateStandardPersonPhoneField({
standardPersonPhoneField: standardPersonPhoneFieldWithTextType,
workspaceDataSource,
workspaceSchemaName: dataSourceMetadata.schema,
});
const fieldsWithPhoneType = await this.fieldMetadataRepository.find({
where: {
workspaceId,
type: FieldMetadataType.PHONE,
},
});
for (const deprecatedPhoneField of fieldsWithPhoneType) {
await this.migrateCustomPhoneField({
phoneField: deprecatedPhoneField,
workspaceDataSource,
workspaceSchemaName: dataSourceMetadata.schema,
});
}
} catch (error) {
this.logger.log(
chalk.red(
`Field migration on workspace ${workspaceId} failed with error: ${error}`,
),
);
continue;
}
this.logger.log(chalk.green(`Command completed!`));
}
}
private async migrateStandardPersonPhoneField({
standardPersonPhoneField,
workspaceDataSource,
workspaceSchemaName,
}: {
standardPersonPhoneField: FieldMetadataEntity;
workspaceDataSource: DataSource;
workspaceSchemaName: string;
}) {
const personObjectMetadata = await this.objectMetadataRepository.findOne({
where: { id: standardPersonPhoneField.objectMetadataId },
});
if (!personObjectMetadata) {
throw new Error(
`Could not find Person objectMetadata (id ${standardPersonPhoneField.objectMetadataId})`,
);
}
this.logger.log(`Attempting to migrate standard person phone field.`);
const workspaceQueryRunner = workspaceDataSource.createQueryRunner();
await workspaceQueryRunner.connect();
const { id: _id, ...deprecatedPhoneFieldWithoutId } =
standardPersonPhoneField;
const workspaceId = standardPersonPhoneField.workspaceId;
try {
let standardPersonPhonesField =
await this.fieldMetadataRepository.findOneBy({
workspaceId,
standardId: PERSON_STANDARD_FIELD_IDS.phones,
});
if (!standardPersonPhonesField) {
standardPersonPhonesField = await this.fieldMetadataService.createOne({
...deprecatedPhoneFieldWithoutId,
label: 'Phones',
type: FieldMetadataType.PHONES,
defaultValue: null,
name: 'phones',
} satisfies CreateFieldInput);
// StandardId and isCustom are not exposed in CreateFieldInput
await this.metadataDataSource.query(
`UPDATE "metadata"."fieldMetadata" SET "standardId" = $1, "isCustom" = $2 where "id"=$3`,
[
PERSON_STANDARD_FIELD_IDS.phones,
'false',
standardPersonPhonesField.id,
],
);
await this.viewService.removeFieldFromViews({
workspaceId: workspaceId,
fieldId: standardPersonPhonesField.id,
});
}
// Copy phone data from Text type to Phones type
await this.copyAndParseDeprecatedPhoneFieldDataIntoNewPhonesField({
workspaceQueryRunner,
workspaceSchemaName,
});
// Add (deprecated) to Phone field label
await this.metadataDataSource.query(
`UPDATE "metadata"."fieldMetadata" SET "label" = $1 where "id"=$2`,
['Phone (deprecated)', standardPersonPhoneField.id],
);
// Add new phones field to views and hide deprecated phone field
const viewFieldRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<ViewFieldWorkspaceEntity>(
workspaceId,
'viewField',
);
const viewFieldsWithDeprecatedPhoneField = await viewFieldRepository.find(
{
where: {
fieldMetadataId: standardPersonPhoneField.id,
isVisible: true,
},
},
);
await this.viewService.addFieldToViews({
workspaceId: workspaceId,
fieldId: standardPersonPhonesField.id,
viewsIds: viewFieldsWithDeprecatedPhoneField
.filter((viewField) => viewField.viewId !== null)
.map((viewField) => viewField.viewId as string),
positions: viewFieldsWithDeprecatedPhoneField.reduce(
(acc, viewField) => {
if (!viewField.viewId) {
return acc;
}
acc[viewField.viewId] = viewField.position;
return acc;
},
[],
),
size: 150,
});
await this.viewService.removeFieldFromViews({
workspaceId: workspaceId,
fieldId: standardPersonPhoneField.id,
});
this.logger.log(
`Migration of standard person phone field to phones is done!`,
);
} catch (error) {
this.logger.log(
chalk.red(
`Failed to migrate field standard person phone field to phones, rolling back. (Error: ${error})`,
),
);
// Delete new phones field if it was created
const newPhonesField =
await this.fieldMetadataService.findOneWithinWorkspace(workspaceId, {
where: {
name: 'phones',
objectMetadataId: standardPersonPhoneField.objectMetadataId,
},
});
if (newPhonesField) {
this.logger.log(
`Deleting phones field of type Phone as part of rolling back.`,
);
await this.fieldMetadataService.deleteOneField(
{ id: newPhonesField.id },
workspaceId,
);
}
// Revert Phone field label (remove (deprecated))
await this.metadataDataSource.query(
`UPDATE "metadata"."fieldMetadata" SET "label" = $1 where "id"=$2`,
['Phone', standardPersonPhoneField.id],
);
} finally {
await workspaceQueryRunner.release();
}
}
private async migrateCustomPhoneField({
phoneField,
workspaceDataSource,
workspaceSchemaName,
}: {
phoneField: FieldMetadataEntity;
workspaceDataSource: DataSource;
workspaceSchemaName: string;
}) {
if (!phoneField) return;
const objectMetadata = await this.objectMetadataRepository.findOne({
where: { id: phoneField.objectMetadataId },
});
if (!objectMetadata) {
throw new Error(
`Could not find objectMetadata for field ${phoneField.name}`,
);
}
this.logger.log(
`Attempting to migrate field ${phoneField.name} on ${objectMetadata.nameSingular} from Phone to Text.`,
);
const workspaceQueryRunner = workspaceDataSource.createQueryRunner();
await workspaceQueryRunner.connect();
try {
await this.metadataDataSource.query(
`UPDATE "metadata"."fieldMetadata" SET "type" = $1 where "id"=$2`,
[FieldMetadataType.TEXT, phoneField.id],
);
await workspaceQueryRunner.query(
`ALTER TABLE "${workspaceSchemaName}"."${computeTableName(objectMetadata.nameSingular, objectMetadata.isCustom)}" ALTER COLUMN "${computeColumnName(phoneField.name)}" TYPE TEXT`,
);
} catch (error) {
this.logger.log(
chalk.red(
`Failed to migrate field ${phoneField.name} on ${objectMetadata.nameSingular} from Phone to Text.`,
),
);
} finally {
await workspaceQueryRunner.release();
}
}
private async copyAndParseDeprecatedPhoneFieldDataIntoNewPhonesField({
workspaceQueryRunner,
workspaceSchemaName,
}: {
workspaceQueryRunner: QueryRunner;
workspaceSchemaName: string;
}) {
const deprecatedPhoneFieldRows = await workspaceQueryRunner.query(
`SELECT id, phone FROM "${workspaceSchemaName}"."person" WHERE
phone IS NOT null`,
);
for (const row of deprecatedPhoneFieldRows) {
const phoneColumnValue = row['phone'];
if (isDefined(phoneColumnValue) && !isEmpty(phoneColumnValue)) {
const query = `UPDATE "${workspaceSchemaName}"."person" SET "phonesPrimaryPhoneCountryCode" = $1,"phonesPrimaryPhoneNumber" = $2 where "id"=$3 AND ("phonesPrimaryPhoneCountryCode" IS NULL OR "phonesPrimaryPhoneCountryCode" = '');`;
try {
const parsedPhoneColumnValue = parsePhoneNumber(phoneColumnValue);
await workspaceQueryRunner.query(query, [
`+${parsedPhoneColumnValue.countryCallingCode}`,
parsedPhoneColumnValue.nationalNumber,
row.id,
]);
} catch (error) {
this.logger.log(
chalk.red(
`Could not save phone number ${phoneColumnValue}, will try again storing value as is without parsing, with default country code.`,
),
);
// Store the invalid string for invalid phone numbers
await workspaceQueryRunner.query(query, [
'',
phoneColumnValue,
row.id,
]);
}
}
}
}
}

View File

@ -1,90 +0,0 @@
import { InjectRepository } from '@nestjs/typeorm';
import chalk from 'chalk';
import { Command } from 'nest-commander';
import { IsNull, Repository } from 'typeorm';
import {
ActiveWorkspacesCommandOptions,
ActiveWorkspacesCommandRunner,
} from 'src/database/commands/active-workspaces.command';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import {
MessageChannelSyncStage,
MessageChannelWorkspaceEntity,
} from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
@Command({
name: 'upgrade-0.30:set-stale-message-sync-back-to-pending',
description: 'Set stale message sync back to pending',
})
export class SetStaleMessageSyncBackToPendingCommand extends ActiveWorkspacesCommandRunner {
constructor(
@InjectRepository(Workspace, 'core')
protected readonly workspaceRepository: Repository<Workspace>,
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
) {
super(workspaceRepository);
}
async executeActiveWorkspacesCommand(
_passedParam: string[],
_options: ActiveWorkspacesCommandOptions,
workspaceIds: string[],
): Promise<void> {
this.logger.log(
'Running command to set stale message sync back to pending',
);
for (const workspaceId of workspaceIds) {
this.logger.log(`Running command for workspace ${workspaceId}`);
try {
const dataSource =
await this.twentyORMGlobalManager.getDataSourceForWorkspace(
workspaceId,
);
const messageChannelRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<MessageChannelWorkspaceEntity>(
workspaceId,
'messageChannel',
);
dataSource.transaction(async (entityManager) => {
await messageChannelRepository.update(
{
syncStage: MessageChannelSyncStage.MESSAGES_IMPORT_ONGOING,
syncStageStartedAt: IsNull(),
},
{
syncStage: MessageChannelSyncStage.MESSAGES_IMPORT_PENDING,
},
entityManager,
);
await messageChannelRepository.update(
{
syncStage: MessageChannelSyncStage.MESSAGE_LIST_FETCH_ONGOING,
syncStageStartedAt: IsNull(),
},
{
syncStage:
MessageChannelSyncStage.PARTIAL_MESSAGE_LIST_FETCH_PENDING,
},
entityManager,
);
});
} catch (error) {
this.logger.log(
chalk.red(
`Running command on workspace ${workspaceId} failed with error: ${error}`,
),
);
continue;
}
this.logger.log(chalk.green(`Command completed!`));
}
}
}

View File

@ -1,76 +0,0 @@
import { InjectRepository } from '@nestjs/typeorm';
import { Command } from 'nest-commander';
import { Repository } from 'typeorm';
import { ActiveWorkspacesCommandRunner } from 'src/database/commands/active-workspaces.command';
import { FixEmailFieldsToEmailsCommand } from 'src/database/commands/upgrade-version/0-30/0-30-fix-email-field-migration.command';
import { FixViewFilterOperandForDateTimeCommand } from 'src/database/commands/upgrade-version/0-30/0-30-fix-view-filter-operand-for-date-time.command';
import { MigrateEmailFieldsToEmailsCommand } from 'src/database/commands/upgrade-version/0-30/0-30-migrate-email-fields-to-emails.command';
import { MigratePhoneFieldsToPhonesCommand } from 'src/database/commands/upgrade-version/0-30/0-30-migrate-phone-fields-to-phones.command';
import { SetStaleMessageSyncBackToPendingCommand } from 'src/database/commands/upgrade-version/0-30/0-30-set-stale-message-sync-back-to-pending';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { SyncWorkspaceMetadataCommand } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/sync-workspace-metadata.command';
interface UpdateTo0_30CommandOptions {
workspaceId?: string;
}
@Command({
name: 'upgrade-0.30',
description: 'Upgrade to 0.30',
})
export class UpgradeTo0_30Command extends ActiveWorkspacesCommandRunner {
constructor(
@InjectRepository(Workspace, 'core')
protected readonly workspaceRepository: Repository<Workspace>,
private readonly syncWorkspaceMetadataCommand: SyncWorkspaceMetadataCommand,
private readonly migrateEmailFieldsToEmails: MigrateEmailFieldsToEmailsCommand,
private readonly setStaleMessageSyncBackToPendingCommand: SetStaleMessageSyncBackToPendingCommand,
private readonly fixEmailFieldsToEmailsCommand: FixEmailFieldsToEmailsCommand,
private readonly migratePhoneFieldsToPhones: MigratePhoneFieldsToPhonesCommand,
private readonly fixViewFilterOperandForDateTimeCommand: FixViewFilterOperandForDateTimeCommand,
) {
super(workspaceRepository);
}
async executeActiveWorkspacesCommand(
passedParam: string[],
options: UpdateTo0_30CommandOptions,
workspaceIds: string[],
): Promise<void> {
await this.syncWorkspaceMetadataCommand.executeActiveWorkspacesCommand(
passedParam,
{
...options,
force: true,
},
workspaceIds,
);
await this.migrateEmailFieldsToEmails.executeActiveWorkspacesCommand(
passedParam,
options,
workspaceIds,
);
await this.setStaleMessageSyncBackToPendingCommand.executeActiveWorkspacesCommand(
passedParam,
options,
workspaceIds,
);
await this.fixEmailFieldsToEmailsCommand.executeActiveWorkspacesCommand(
passedParam,
options,
workspaceIds,
);
await this.migratePhoneFieldsToPhones.executeActiveWorkspacesCommand(
passedParam,
options,
workspaceIds,
);
await this.fixViewFilterOperandForDateTimeCommand.executeActiveWorkspacesCommand(
passedParam,
options,
workspaceIds,
);
}
}

View File

@ -1,43 +0,0 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { FixEmailFieldsToEmailsCommand } from 'src/database/commands/upgrade-version/0-30/0-30-fix-email-field-migration.command';
import { FixViewFilterOperandForDateTimeCommand } from 'src/database/commands/upgrade-version/0-30/0-30-fix-view-filter-operand-for-date-time.command';
import { MigrateEmailFieldsToEmailsCommand } from 'src/database/commands/upgrade-version/0-30/0-30-migrate-email-fields-to-emails.command';
import { MigratePhoneFieldsToPhonesCommand } from 'src/database/commands/upgrade-version/0-30/0-30-migrate-phone-fields-to-phones.command';
import { SetStaleMessageSyncBackToPendingCommand } from 'src/database/commands/upgrade-version/0-30/0-30-set-stale-message-sync-back-to-pending';
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';
import { DataSourceModule } from 'src/engine/metadata-modules/data-source/data-source.module';
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { FieldMetadataModule } from 'src/engine/metadata-modules/field-metadata/field-metadata.module';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { WorkspaceMetadataVersionModule } from 'src/engine/metadata-modules/workspace-metadata-version/workspace-metadata-version.module';
import { WorkspaceSyncMetadataCommandsModule } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/workspace-sync-metadata-commands.module';
import { ViewModule } from 'src/modules/view/view.module';
@Module({
imports: [
TypeOrmModule.forFeature([Workspace], 'core'),
WorkspaceSyncMetadataCommandsModule,
DataSourceModule,
WorkspaceMetadataVersionModule,
FieldMetadataModule,
TypeOrmModule.forFeature(
[FieldMetadataEntity, ObjectMetadataEntity],
'metadata',
),
TypeORMModule,
ViewModule,
],
providers: [
UpgradeTo0_30Command,
MigrateEmailFieldsToEmailsCommand,
SetStaleMessageSyncBackToPendingCommand,
FixEmailFieldsToEmailsCommand,
MigratePhoneFieldsToPhonesCommand,
FixViewFilterOperandForDateTimeCommand,
],
})
export class UpgradeTo0_30CommandModule {}