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 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(
              field.universalIdentifier,
            );
          const isCustomTarget = relatedObject.isCustom && !isStandardAppField;

          if (!isStandardTarget && !isCustomTarget) {
            return [];
          }

          return [field];
        });

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

      for (const { oldJoinColumnName, newJoinColumnName } of fieldMigrations) {

Review comment (suggestion):
***Question:*** I'm not sure to understand how this would be possible ? What's the goal of this function overall ?

If it's retrieving all attachement relation field, either custom or standard ( which by definition any field row is one of the two ) then `.filter(isFlatFieldMetadataOfType(RELATION))

Would be enough

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

      for (const { oldJoinColumnName, newJoinColumnName } of fieldMigrations) {
        if (oldJoinColumnName === newJoinColumnName) {
          this.logger.log(
            `Column "${oldJoinColumnName}" already renamed. Skipping...`,
          );
          continue;
        }

        try {
          await queryRunner.query(
            `ALTER TABLE "${schemaName}"."${tableName}"
           RENAME COLUMN "${oldJoinColumnName}" TO "${newJoinColumnName}"`,
          );
          this.logger.log(
            `Renamed column "${oldJoinColumnName}" to "${newJoinColumnName}" for "${tableName}"`,
          );
        } catch (error) {
          this.logger.error(
            `Error renaming column "${oldJoinColumnName}" to "${newJoinColumnName}" for "${tableName}" in workspace ${workspaceId}`,
            error,
          );

          throw error;
        }
      }