Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor standard relations update at custom object renaming #8638

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -115,7 +118,7 @@ export class ObjectMetadataMigrationService {
);
}

public async createRelationsUpdatesMigrations(
public async createStandardRelationsUpdatesMigrations(
existingObjectMetadata: ObjectMetadataEntity,
updatedObjectMetadata: ObjectMetadataEntity,
workspaceId: string,
Expand All @@ -124,87 +127,155 @@ export class ObjectMetadataMigrationService {
const newTableName = computeObjectTargetTable(updatedObjectMetadata);

if (existingTableName !== newTableName) {
const searchCriteria = {
isCustom: false,
settings: {
isForeignKey: true,
},
name: `${existingObjectMetadata.nameSingular}Id`,
workspaceId: workspaceId,
};
const relatedObjectsIds = await this.relationMetadataRepository
.find({
where: {
workspaceId,
fromObjectMetadataId: existingObjectMetadata.id,
},
})
.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,
workspaceId: workspaceId,
});
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(
{
name: existingObjectMetadata.nameSingular,
label: existingObjectMetadata.labelSingular,
},
{
name: updatedObjectMetadata.nameSingular,
label: updatedObjectMetadata.labelSingular,
},
);
if (relatedObject) {
// 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 relationTableName = computeObjectTargetTable(relatedObject);
const columnName = `${existingObjectMetadata.nameSingular}Id`;
const columnType = fieldMetadataTypeToColumnType(
fieldWihStandardRelation.type,
);
const { description: descriptionForToField } =
buildDescriptionForRelationFieldMetadataOnToField({
relationObjectMetadataNamePlural: relatedObject.namePlural,
targetObjectLabelSingular:
updatedObjectMetadata.labelSingular,
});

await this.workspaceMigrationService.createCustomMigration(
generateMigrationName(
`rename-${existingObjectMetadata.nameSingular}-to-${updatedObjectMetadata.nameSingular}-in-${relatedObject.nameSingular}`,
),
workspaceId,
[
{
name: relationTableName,
action: WorkspaceMigrationTableActionType.ALTER,
columns: [
await this.fieldMetadataRepository.update(
toFieldRelationFieldMetadataId,
{
name: updatedObjectMetadata.nameSingular,
label: updatedObjectMetadata.labelSingular,
description: descriptionForToField,
},
);

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(
foreignKeyFieldMetadata.type,
);

await this.workspaceMigrationService.createCustomMigration(
generateMigrationName(
`rename-${existingObjectMetadata.nameSingular}-to-${updatedObjectMetadata.nameSingular}-in-${relatedObject.nameSingular}`,
),
workspaceId,
[
{
action: WorkspaceMigrationColumnActionType.ALTER,
currentColumnDefinition: {
columnName,
columnType,
isNullable: true,
defaultValue: null,
},
alteredColumnDefinition: {
columnName: `${updatedObjectMetadata.nameSingular}Id`,
columnType,
isNullable: true,
defaultValue: null,
},
name: relatedObjectTableName,
action: WorkspaceMigrationTableActionType.ALTER,
columns: [
{
action: WorkspaceMigrationColumnActionType.ALTER,
currentColumnDefinition: {
columnName,
columnType,
isNullable: true,
defaultValue: null,
},
alteredColumnDefinition: {
columnName: `${updatedObjectMetadata.nameSingular}Id`,
columnType,
isNullable: true,
defaultValue: null,
},
},
],
},
],
},
],
);
}
}),
);
}
}
},
),
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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],
Expand All @@ -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',
Expand All @@ -174,6 +191,11 @@ export class ObjectMetadataRelationService {
);
}

const { description } = buildDescriptionForRelationFieldMetadataOnToField({
relationObjectMetadataNamePlural: relatedObjectMetadata.namePlural,
targetObjectLabelSingular: createdObjectMetadata.labelSingular,
});

return {
standardId: createRelationDeterministicUuid({
objectId: createdObjectMetadata.id,
Expand All @@ -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,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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 };
};
Original file line number Diff line number Diff line change
@@ -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 };
};
Original file line number Diff line number Diff line change
@@ -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`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: description format is inconsistent with label format - 'id' should be capitalized as 'ID' to match label style


return { name, label, description };
};
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
type: FieldMetadataType.UUID,
objectMetadataId: relationMetadataInput.toObjectMetadataId,
workspaceId: relationMetadataInput.workspaceId,
settings: { isForeignKey: true },
};
}

Expand Down
Loading