Repository: twentyhq/twenty
File: packages/twenty-server/src/database/commands/upgrade-version-command/1-17/1-17-migrate-attachment-to-morph-relations.command.ts (TypeScript)

Code before:

      if (!attachmentObjectMetadata) {
        this.logger.error(
          `🟥 Attachment object metadata not found for workspace ${workspaceId}`,
        );

        return;
      }

      const attachmentFieldMetadatas = getFlatFieldsFromFlatObjectMetadata(
        attachmentObjectMetadata,
        flatFieldMetadataMaps,
      );

      const isTwentyStandardApplicationField = (field: {
        applicationId: string;
      }) => {
        const application = flatApplicationMaps.byId[field.applicationId];

        return (
          application?.universalIdentifier ===
          TWENTY_STANDARD_APPLICATION.universalIdentifier
        );
      };

      const getRelatedObjectMetadata = (field: {
        relationTargetObjectMetadataId?: string | null;
      }) => {
        if (!field.relationTargetObjectMetadataId) {
          return;
        }

        return flatObjectMetadataMaps.byId[
          field.relationTargetObjectMetadataId
        ];
      };

      const attachmentTargetRelationFields = attachmentFieldMetadatas
        .filter(isMorphOrRelationFlatFieldMetadata)
        .filter((field) => field.type === FieldMetadataType.RELATION)
        .flatMap((field) => {
          const relatedObject = getRelatedObjectMetadata(field);

          if (!isDefined(relatedObject)) {
            return [];
          }

          const isStandardAppField = isTwentyStandardApplicationField(field);
          const isStandardTarget =
            isStandardAppField &&
            attachmentTargetFieldUniversalIdentifiers.has(

Review comment (suggestion):
***Subjective polish:*** bit redundant as could use the `findFlatEntityInMapsFromId` directly

Code after:
        });

      if (!attachmentObjectMetadata) {
        this.logger.error(
          `🟥 Attachment object metadata not found for workspace ${workspaceId}`,
        );

        return;
      }

      const attachmentFieldMetadatas = getFlatFieldsFromFlatObjectMetadata(
        attachmentObjectMetadata,
        flatFieldMetadataMaps,
      );

      const attachmentTargetRelationFields = attachmentFieldMetadatas
        .filter(isMorphOrRelationFlatFieldMetadata)
        .filter((field) => field.type === FieldMetadataType.RELATION)
        .filter((field) => {
          const isStandardAppField = this.isTwentyStandardApplicationField({
            field,
            flatApplicationMaps,
            workspaceId,
          });
          const isStandardTarget =
            isStandardAppField &&
            attachmentTargetFieldUniversalIdentifiers.has(
              field.universalIdentifier,
            );
          const targetObjectMetadata = field.relationTargetObjectMetadataId
            ? flatObjectMetadataMaps.byId[field.relationTargetObjectMetadataId]
            : undefined;
          const isCustomTarget =
            !isStandardAppField && targetObjectMetadata?.isCustom === true;

          return isStandardTarget || isCustomTarget;
        });

      const fieldMigrations = attachmentTargetRelationFields.map((field) => {
        const newFieldName = `target${capitalize(field.name)}`;
        const relationSettings: RelationFieldMetadataSettings = field.settings;
        const oldJoinColumnName =
          relationSettings?.joinColumnName ??
          computeMorphOrRelationFieldJoinColumnName({ name: field.name });
        const newJoinColumnName = computeMorphOrRelationFieldJoinColumnName({
          name: newFieldName,
        });

        return {
          field,
          newFieldName,