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:
        .filter((field) => field.type === 'RELATION')
        .filter((field) => {
          if (!field.relationTargetObjectMetadataId) {
            return false;
          }

          const relatedObject = relatedObjectMetadataMap.get(
            field.relationTargetObjectMetadataId,
          );

          if (!relatedObject) {
            return false;
          }

          const isStandardTarget =
            attachmentTargetFieldUniversalIdentifiers.has(
              field.universalIdentifier,
            );
          const isCustomTarget = relatedObject.isCustom;

          return isStandardTarget || isCustomTarget;
        });

      const fieldMigrations = attachmentTargetRelationFields.flatMap(
        (field) => {
          if (!field.relationTargetObjectMetadataId) {
            return [];
          }

          const relatedObject = relatedObjectMetadataMap.get(
            field.relationTargetObjectMetadataId,
          );

          if (!relatedObject) {
            return [];
          }

          const newFieldName = `target${capitalize(relatedObject.nameSingular)}`;
          const relationSettings =
            field.settings as FieldMetadataRelationSettings | null;
          const oldJoinColumnName =
            relationSettings?.joinColumnName ??
            computeMorphOrRelationFieldJoinColumnName({ name: field.name });
          const newJoinColumnName = computeMorphOrRelationFieldJoinColumnName({
            name: newFieldName,
          });

          return [
            {
              field,
              relatedObject,

Review comment (suggestion):
***Suggestion:*** `isMorphOrRelationFlatFieldMetadata`

Code after:
        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,
          oldJoinColumnName,
          newJoinColumnName,
        };