Repository: twentyhq/twenty
File: packages/twenty-server/src/database/commands/upgrade-version-command/1-12/1-12-clean-null-equivalent-values.ts (TypeScript)

Code before:

    const schemaName = getWorkspaceSchemaName(workspaceId);

    if (isDryRun) {
      this.logger.log('Dry run mode: No changes will be applied');
    }

    const featureFlag = await this.featureFlagRepository.findOne({
      where: {
        key: FeatureFlagKey.IS_NULL_EQUIVALENCE_ENABLED,
        value: true,
        workspaceId,
      },
    });

    if (isDefined(featureFlag)) {
      this.logger.log(
        `Feature flag ${FeatureFlagKey.IS_NULL_EQUIVALENCE_ENABLED} already enabled for workspace ${workspaceId}`,
      );

      return;
    }

    const objectMetadataItems = await this.objectMetadataRepository.find({
      where: { workspaceId },
      relations: [
        'fields',
        'indexMetadatas',
        'indexMetadatas.indexFieldMetadatas',
      ],
    });

    for (const objectMetadataItem of objectMetadataItems) {
      const tableName = computeObjectTargetTable(objectMetadataItem);

      for (const field of objectMetadataItem.fields) {
        const fieldDefaultDefaultValue = deprecatedGenerateDefaultValue(
          field.type,
        );

        if (
          isDefined(field.defaultValue) &&
          ((typeof field.defaultValue === 'string' &&
            field.defaultValue === fieldDefaultDefaultValue) ||
            this.objectEquals(field.defaultValue, fieldDefaultDefaultValue)) &&
          field.type !== FieldMetadataType.ACTOR
        ) {
          this.logger.log(
            `Processing field ${field.name} on object ${objectMetadataItem.nameSingular} (Table: ${tableName})`,
          );
          if (!isDryRun) {

Review comment (performance):
***Remark:*** Not asking anything here that's very reliable and good, in the future we might wanna be consuming the cache here instead

Code after:
      );
    }

    const schemaName = getWorkspaceSchemaName(workspaceId);

    if (isDryRun) {
      this.logger.log('Dry run mode: No changes will be applied');
    }

    const featureFlag = await this.featureFlagRepository.findOne({
      where: {
        key: FeatureFlagKey.IS_NULL_EQUIVALENCE_ENABLED,
        value: true,
        workspaceId,
      },
    });

    if (isDefined(featureFlag)) {
      this.logger.log(
        `Feature flag ${FeatureFlagKey.IS_NULL_EQUIVALENCE_ENABLED} already enabled for workspace ${workspaceId}`,
      );

      return;
    }

    const objectMetadataItems = await this.objectMetadataRepository.find({
      where: { workspaceId },
      relations: [
        'fields',
        'indexMetadatas',
        'indexMetadatas.indexFieldMetadatas',
      ],
    });

    for (const objectMetadataItem of objectMetadataItems) {
      const tableName = computeObjectTargetTable(objectMetadataItem);

      for (const field of objectMetadataItem.fields) {
        const fieldDefaultDefaultValue = deprecatedGenerateDefaultValue(
          field.type,
        );

        if (
          !(
            isDefined(field.defaultValue) &&
            ((typeof field.defaultValue === 'string' &&
              field.defaultValue === fieldDefaultDefaultValue) ||
              this.objectEquals(
                field.defaultValue,
                fieldDefaultDefaultValue,
              )) &&