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

          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,
          });


Review comment (suggestion):
***Suggestion:*** To check if an metadata entity is own by the twenty-standard app I would rather check field `applicationId` and check if related `FlatApplication` universalIdentifier from `FlatApplicationMaps` is equal to `TWENTY_STANDARD_APPLICATION.universalIdentifier`

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

        return {
          field,