mirror of
https://github.com/twentyhq/twenty.git
synced 2024-12-23 03:51:36 +03:00
Refactor standard relations update at custom object renaming (#8638)
Refactoring update of standard relations when a custom object is renamed, after observing occasional issues. --------- Co-authored-by: Weiko <corentin@twenty.com>
This commit is contained in:
parent
cf73e32969
commit
670c8a0b98
@ -424,14 +424,14 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
|
||||
existingObjectMetadata,
|
||||
);
|
||||
|
||||
if (!(newTargetTableName === existingTargetTableName)) {
|
||||
if (newTargetTableName !== existingTargetTableName) {
|
||||
await this.objectMetadataMigrationService.createRenameTableMigration(
|
||||
existingObjectMetadata,
|
||||
objectMetadataForUpdate,
|
||||
objectMetadataForUpdate.workspaceId,
|
||||
);
|
||||
|
||||
await this.objectMetadataMigrationService.createRelationsUpdatesMigrations(
|
||||
await this.objectMetadataMigrationService.createStandardRelationsUpdatesMigrations(
|
||||
existingObjectMetadata,
|
||||
objectMetadataForUpdate,
|
||||
objectMetadataForUpdate.workspaceId,
|
||||
|
@ -6,7 +6,10 @@ import { Repository } from 'typeorm';
|
||||
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
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 { buildDescriptionForRelationFieldMetadataOnFromField } from 'src/engine/metadata-modules/object-metadata/utils/build-description-for-relation-field-on-from-field.util';
|
||||
import { buildDescriptionForRelationFieldMetadataOnToField } from 'src/engine/metadata-modules/object-metadata/utils/build-description-for-relation-field-on-to-field.util';
|
||||
import { buildMigrationsForCustomObjectRelations } from 'src/engine/metadata-modules/object-metadata/utils/build-migrations-for-custom-object-relations.util';
|
||||
import { buildNameLabelAndDescriptionForForeignKeyFieldMetadata } from 'src/engine/metadata-modules/object-metadata/utils/build-name-label-and-description-for-foreign-key-field-metadata.util';
|
||||
import { RelationMetadataEntity } from 'src/engine/metadata-modules/relation-metadata/relation-metadata.entity';
|
||||
import { RelationToDelete } from 'src/engine/metadata-modules/relation-metadata/types/relation-to-delete';
|
||||
import { fieldMetadataTypeToColumnType } from 'src/engine/metadata-modules/workspace-migration/utils/field-metadata-type-to-column-type.util';
|
||||
@ -115,7 +118,7 @@ export class ObjectMetadataMigrationService {
|
||||
);
|
||||
}
|
||||
|
||||
public async createRelationsUpdatesMigrations(
|
||||
public async createStandardRelationsUpdatesMigrations(
|
||||
existingObjectMetadata: ObjectMetadataEntity,
|
||||
updatedObjectMetadata: ObjectMetadataEntity,
|
||||
workspaceId: string,
|
||||
@ -124,54 +127,120 @@ export class ObjectMetadataMigrationService {
|
||||
const newTableName = computeObjectTargetTable(updatedObjectMetadata);
|
||||
|
||||
if (existingTableName !== newTableName) {
|
||||
const searchCriteria = {
|
||||
isCustom: false,
|
||||
settings: {
|
||||
isForeignKey: true,
|
||||
const relatedObjectsIds = await this.relationMetadataRepository
|
||||
.find({
|
||||
where: {
|
||||
workspaceId,
|
||||
fromObjectMetadataId: existingObjectMetadata.id,
|
||||
},
|
||||
name: `${existingObjectMetadata.nameSingular}Id`,
|
||||
workspaceId: workspaceId,
|
||||
};
|
||||
})
|
||||
.then((relations) =>
|
||||
relations.map((relation) => relation.toObjectMetadataId),
|
||||
);
|
||||
|
||||
const fieldsWihStandardRelation = await this.fieldMetadataRepository.find(
|
||||
{
|
||||
const foreignKeyFieldMetadataForStandardRelation =
|
||||
await this.fieldMetadataRepository.find({
|
||||
where: {
|
||||
isCustom: false,
|
||||
settings: {
|
||||
isForeignKey: true,
|
||||
},
|
||||
name: `${existingObjectMetadata.nameSingular}Id`,
|
||||
workspaceId: workspaceId,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
await this.fieldMetadataRepository.update(searchCriteria, {
|
||||
name: `${updatedObjectMetadata.nameSingular}Id`,
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
fieldsWihStandardRelation.map(async (fieldWihStandardRelation) => {
|
||||
const relatedObject = await this.objectMetadataRepository.findOneBy({
|
||||
id: fieldWihStandardRelation.objectMetadataId,
|
||||
foreignKeyFieldMetadataForStandardRelation.map(
|
||||
async (foreignKeyFieldMetadata) => {
|
||||
if (
|
||||
relatedObjectsIds.includes(
|
||||
foreignKeyFieldMetadata.objectMetadataId,
|
||||
)
|
||||
) {
|
||||
const relatedObject =
|
||||
await this.objectMetadataRepository.findOneBy({
|
||||
id: foreignKeyFieldMetadata.objectMetadataId,
|
||||
workspaceId: workspaceId,
|
||||
});
|
||||
|
||||
if (relatedObject) {
|
||||
await this.fieldMetadataRepository.update(
|
||||
{
|
||||
// 1. Update to and from relation fieldMetadata)
|
||||
const toFieldRelationFieldMetadataId =
|
||||
await this.fieldMetadataRepository
|
||||
.findOneByOrFail({
|
||||
name: existingObjectMetadata.nameSingular,
|
||||
label: existingObjectMetadata.labelSingular,
|
||||
},
|
||||
objectMetadataId: relatedObject.id,
|
||||
workspaceId: workspaceId,
|
||||
})
|
||||
.then((field) => field.id);
|
||||
|
||||
const { description: descriptionForToField } =
|
||||
buildDescriptionForRelationFieldMetadataOnToField({
|
||||
relationObjectMetadataNamePlural: relatedObject.namePlural,
|
||||
targetObjectLabelSingular:
|
||||
updatedObjectMetadata.labelSingular,
|
||||
});
|
||||
|
||||
await this.fieldMetadataRepository.update(
|
||||
toFieldRelationFieldMetadataId,
|
||||
{
|
||||
name: updatedObjectMetadata.nameSingular,
|
||||
label: updatedObjectMetadata.labelSingular,
|
||||
description: descriptionForToField,
|
||||
},
|
||||
);
|
||||
|
||||
const relationTableName = computeObjectTargetTable(relatedObject);
|
||||
const fromFieldRelationFieldMetadataId =
|
||||
await this.relationMetadataRepository
|
||||
.findOneByOrFail({
|
||||
fromObjectMetadataId: existingObjectMetadata.id,
|
||||
toObjectMetadataId: relatedObject.id,
|
||||
toFieldMetadataId: toFieldRelationFieldMetadataId,
|
||||
workspaceId,
|
||||
})
|
||||
.then((relation) => relation?.fromFieldMetadataId);
|
||||
|
||||
await this.fieldMetadataRepository.update(
|
||||
fromFieldRelationFieldMetadataId,
|
||||
{
|
||||
description:
|
||||
buildDescriptionForRelationFieldMetadataOnFromField({
|
||||
relationObjectMetadataNamePlural:
|
||||
relatedObject.namePlural,
|
||||
targetObjectLabelSingular:
|
||||
updatedObjectMetadata.labelSingular,
|
||||
}).description,
|
||||
},
|
||||
);
|
||||
|
||||
// 2. Update foreign key fieldMetadata
|
||||
const {
|
||||
name: updatedNameForForeignKeyFieldMetadata,
|
||||
label: updatedLabelForForeignKeyFieldMetadata,
|
||||
description: updatedDescriptionForForeignKeyFieldMetadata,
|
||||
} = buildNameLabelAndDescriptionForForeignKeyFieldMetadata({
|
||||
targetObjectNameSingular: updatedObjectMetadata.nameSingular,
|
||||
targetObjectLabelSingular:
|
||||
updatedObjectMetadata.labelSingular,
|
||||
relatedObjectLabelSingular: relatedObject.labelSingular,
|
||||
});
|
||||
|
||||
await this.fieldMetadataRepository.update(
|
||||
foreignKeyFieldMetadata.id,
|
||||
{
|
||||
name: updatedNameForForeignKeyFieldMetadata,
|
||||
label: updatedLabelForForeignKeyFieldMetadata,
|
||||
description: updatedDescriptionForForeignKeyFieldMetadata,
|
||||
},
|
||||
);
|
||||
|
||||
const relatedObjectTableName =
|
||||
computeObjectTargetTable(relatedObject);
|
||||
const columnName = `${existingObjectMetadata.nameSingular}Id`;
|
||||
const columnType = fieldMetadataTypeToColumnType(
|
||||
fieldWihStandardRelation.type,
|
||||
foreignKeyFieldMetadata.type,
|
||||
);
|
||||
|
||||
await this.workspaceMigrationService.createCustomMigration(
|
||||
@ -181,7 +250,7 @@ export class ObjectMetadataMigrationService {
|
||||
workspaceId,
|
||||
[
|
||||
{
|
||||
name: relationTableName,
|
||||
name: relatedObjectTableName,
|
||||
action: WorkspaceMigrationTableActionType.ALTER,
|
||||
columns: [
|
||||
{
|
||||
@ -204,7 +273,9 @@ export class ObjectMetadataMigrationService {
|
||||
],
|
||||
);
|
||||
}
|
||||
}),
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,9 @@ import {
|
||||
FieldMetadataType,
|
||||
} from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
|
||||
import { buildDescriptionForRelationFieldMetadataOnFromField } from 'src/engine/metadata-modules/object-metadata/utils/build-description-for-relation-field-on-from-field.util';
|
||||
import { buildDescriptionForRelationFieldMetadataOnToField } from 'src/engine/metadata-modules/object-metadata/utils/build-description-for-relation-field-on-to-field.util';
|
||||
import { buildNameLabelAndDescriptionForForeignKeyFieldMetadata } from 'src/engine/metadata-modules/object-metadata/utils/build-name-label-and-description-for-foreign-key-field-metadata.util';
|
||||
import {
|
||||
RelationMetadataEntity,
|
||||
RelationMetadataType,
|
||||
@ -94,6 +97,13 @@ export class ObjectMetadataRelationService {
|
||||
);
|
||||
}
|
||||
|
||||
const { name, label, description } =
|
||||
buildNameLabelAndDescriptionForForeignKeyFieldMetadata({
|
||||
targetObjectNameSingular: createdObjectMetadata.nameSingular,
|
||||
targetObjectLabelSingular: createdObjectMetadata.labelSingular,
|
||||
relatedObjectLabelSingular: relatedObjectMetadata.labelSingular,
|
||||
});
|
||||
|
||||
await this.fieldMetadataRepository.save({
|
||||
standardId: createForeignKeyDeterministicUuid({
|
||||
objectId: createdObjectMetadata.id,
|
||||
@ -104,9 +114,9 @@ export class ObjectMetadataRelationService {
|
||||
isCustom: false,
|
||||
isActive: true,
|
||||
type: objectPrimaryKeyType,
|
||||
name: `${createdObjectMetadata.nameSingular}Id`,
|
||||
label: `${createdObjectMetadata.labelSingular} ID (foreign key)`,
|
||||
description: `${relatedObjectMetadata.labelSingular} ${createdObjectMetadata.labelSingular} id foreign key`,
|
||||
name,
|
||||
label,
|
||||
description,
|
||||
icon: undefined,
|
||||
isNullable: true,
|
||||
isSystem: true,
|
||||
@ -141,6 +151,13 @@ export class ObjectMetadataRelationService {
|
||||
) {
|
||||
const relationObjectMetadataNamePlural = relatedObjectMetadata.namePlural;
|
||||
|
||||
const { description } = buildDescriptionForRelationFieldMetadataOnFromField(
|
||||
{
|
||||
relationObjectMetadataNamePlural,
|
||||
targetObjectLabelSingular: createdObjectMetadata.labelSingular,
|
||||
},
|
||||
);
|
||||
|
||||
return {
|
||||
standardId:
|
||||
CUSTOM_OBJECT_STANDARD_FIELD_IDS[relationObjectMetadataNamePlural],
|
||||
@ -152,7 +169,7 @@ export class ObjectMetadataRelationService {
|
||||
type: FieldMetadataType.RELATION,
|
||||
name: relatedObjectMetadata.namePlural,
|
||||
label: capitalize(relationObjectMetadataNamePlural),
|
||||
description: `${capitalize(relationObjectMetadataNamePlural)} tied to the ${createdObjectMetadata.labelSingular}`,
|
||||
description,
|
||||
icon:
|
||||
STANDARD_OBJECT_ICONS[relatedObjectMetadata.nameSingular] ||
|
||||
'IconBuildingSkyscraper',
|
||||
@ -174,6 +191,11 @@ export class ObjectMetadataRelationService {
|
||||
);
|
||||
}
|
||||
|
||||
const { description } = buildDescriptionForRelationFieldMetadataOnToField({
|
||||
relationObjectMetadataNamePlural: relatedObjectMetadata.namePlural,
|
||||
targetObjectLabelSingular: createdObjectMetadata.labelSingular,
|
||||
});
|
||||
|
||||
return {
|
||||
standardId: createRelationDeterministicUuid({
|
||||
objectId: createdObjectMetadata.id,
|
||||
@ -187,7 +209,7 @@ export class ObjectMetadataRelationService {
|
||||
type: FieldMetadataType.RELATION,
|
||||
name: createdObjectMetadata.nameSingular,
|
||||
label: createdObjectMetadata.labelSingular,
|
||||
description: `${capitalize(relatedObjectMetadata.nameSingular)} ${createdObjectMetadata.labelSingular}`,
|
||||
description,
|
||||
icon: 'IconBuildingSkyscraper',
|
||||
isNullable: true,
|
||||
};
|
||||
|
@ -0,0 +1,13 @@
|
||||
import { capitalize } from 'src/utils/capitalize';
|
||||
|
||||
export const buildDescriptionForRelationFieldMetadataOnFromField = ({
|
||||
relationObjectMetadataNamePlural,
|
||||
targetObjectLabelSingular,
|
||||
}: {
|
||||
relationObjectMetadataNamePlural: string;
|
||||
targetObjectLabelSingular: string;
|
||||
}) => {
|
||||
const description = `${capitalize(relationObjectMetadataNamePlural)} tied to the ${targetObjectLabelSingular}`;
|
||||
|
||||
return { description };
|
||||
};
|
@ -0,0 +1,13 @@
|
||||
import { capitalize } from 'src/utils/capitalize';
|
||||
|
||||
export const buildDescriptionForRelationFieldMetadataOnToField = ({
|
||||
relationObjectMetadataNamePlural,
|
||||
targetObjectLabelSingular,
|
||||
}: {
|
||||
relationObjectMetadataNamePlural: string;
|
||||
targetObjectLabelSingular: string;
|
||||
}) => {
|
||||
const description = `${capitalize(relationObjectMetadataNamePlural)} ${targetObjectLabelSingular}`;
|
||||
|
||||
return { description };
|
||||
};
|
@ -0,0 +1,15 @@
|
||||
export const buildNameLabelAndDescriptionForForeignKeyFieldMetadata = ({
|
||||
targetObjectNameSingular,
|
||||
targetObjectLabelSingular,
|
||||
relatedObjectLabelSingular,
|
||||
}: {
|
||||
targetObjectNameSingular: string;
|
||||
targetObjectLabelSingular: string;
|
||||
relatedObjectLabelSingular: string;
|
||||
}) => {
|
||||
const name = `${targetObjectNameSingular}Id`;
|
||||
const label = `${targetObjectLabelSingular} ID (foreign key)`;
|
||||
const description = `${relatedObjectLabelSingular} ${targetObjectLabelSingular} id foreign key`;
|
||||
|
||||
return { name, label, description };
|
||||
};
|
@ -332,6 +332,7 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
|
||||
type: FieldMetadataType.UUID,
|
||||
objectMetadataId: relationMetadataInput.toObjectMetadataId,
|
||||
workspaceId: relationMetadataInput.workspaceId,
|
||||
settings: { isForeignKey: true },
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user