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:
      const attachmentObjectMetadata =
        findFlatEntityByUniversalIdentifier<FlatObjectMetadata>({
          flatEntityMaps: flatObjectMetadataMaps,
          universalIdentifier: STANDARD_OBJECTS.attachment.universalIdentifier,
        });

      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 [];
          }

Review comment (suggestion):
***Polish:*** Would make a util or method but avoid declaring the util inside the run
***Polish:*** It should never be undefined, lets log an error if it's

Code after:

      const attachmentObjectMetadata =
        findFlatEntityByUniversalIdentifier<FlatObjectMetadata>({
          flatEntityMaps: flatObjectMetadataMaps,
          universalIdentifier: STANDARD_OBJECTS.attachment.universalIdentifier,
        });

      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,