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:
        ATTACHMENT_STANDARD_FIELD_IDS.targetCompany,
        ATTACHMENT_STANDARD_FIELD_IDS.targetOpportunity,
        ATTACHMENT_STANDARD_FIELD_IDS.targetDashboard,
        ATTACHMENT_STANDARD_FIELD_IDS.targetWorkflow,
      ]);

      const attachmentObjectMetadata =
        await this.objectMetadataRepository.findOne({
          where: {
            workspaceId,
            universalIdentifier: STANDARD_OBJECT_IDS.attachment,
          },
          relations: ['fields'],
        });

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

        return;
      }

      const attachmentTargetRelationFields = attachmentObjectMetadata.fields
        .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 [];

Review comment (suggestion):
***Remark:*** If this filter aims to filter out any field entry that do not have an `applicationId`nor `universalIdentifier` it's redundant as now not nullable

Code after:
      } = await this.workspaceCacheService.getOrRecompute(workspaceId, [
        'flatObjectMetadataMaps',
        'flatFieldMetadataMaps',
        'flatApplicationMaps',
      ]);

      const attachmentTargetFieldUniversalIdentifiers = new Set<string>([
        STANDARD_OBJECTS.attachment.fields.targetTask.universalIdentifier,
        STANDARD_OBJECTS.attachment.fields.targetNote.universalIdentifier,
        STANDARD_OBJECTS.attachment.fields.targetPerson.universalIdentifier,
        STANDARD_OBJECTS.attachment.fields.targetCompany.universalIdentifier,
        STANDARD_OBJECTS.attachment.fields.targetOpportunity
          .universalIdentifier,
        STANDARD_OBJECTS.attachment.fields.targetDashboard.universalIdentifier,
        STANDARD_OBJECTS.attachment.fields.targetWorkflow.universalIdentifier,
      ]);

      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