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:

            await this.fieldMetadataRepository.update(field.id, {
              isNullable: true,
              defaultValue: null,
            });
          }

          const relevantIndexes = objectMetadataItem.indexMetadatas.filter(
            (index) =>
              index.isUnique &&
              index.indexFieldMetadatas.some(
                (ifm) => ifm.fieldMetadataId === field.id,
              ),
          );

          for (const index of relevantIndexes) {
            if (!isDryRun) {
              this.logger.log(
                `Removing where clause from index ${index.name} on ${tableName}`,
              );

              await this.indexMetadataRepository.update(index.id, {
                indexWhereClause: null,
              });

              const columnNames = index.indexFieldMetadatas.flatMap(
                (indexFieldMetadata) => {
                  const fieldMetadata = objectMetadataItem.fields.find(
                    (f) => f.id === indexFieldMetadata.fieldMetadataId,
                  );

                  if (!isDefined(fieldMetadata)) {
                    throw new Error(
                      `Field metadata not found for index field metadata ${indexFieldMetadata.id}`,
                    );
                  }

                  if (isCompositeFieldMetadataType(fieldMetadata.type)) {
                    const compositeType = compositeTypeDefinitions.get(
                      fieldMetadata.type,
                    ) as CompositeType;

                    const uniqueCompositeProperties =
                      compositeType.properties.filter(
                        (property) => property.isIncludedInUniqueConstraint,
                      );

                    return uniqueCompositeProperties.map((subField) =>
                      computeCompositeColumnName(fieldMetadata.name, subField),
                    );
                  }

Review comment (suggestion):
***Remark:*** Small duplication of `computeFlatIndexFieldColumnNames` from `packages/twenty-server/src/engine/workspace-manager/workspace-migration-v2/workspace-migration-runner-v2/action-handlers/index/services/create-index-action-handler.service.ts`

Code after:
            );

            await dataSource.query(
              `UPDATE "${schemaName}"."${tableName}" SET "${field.name}" = NULL WHERE "${field.name}" = ${field.defaultValue}`,
              [],
              undefined,
              { shouldBypassPermissionChecks: true },
            );
          }

          await this.fieldMetadataRepository.update(field.id, {
            isNullable: true,
            defaultValue: null,
          });

          await this.workspaceManyOrAllFlatEntityMapsCacheService.invalidateFlatEntityMaps(
            {
              workspaceId,
              flatMapsKeys: ['flatFieldMetadataMaps'],
            },
          );
        }

        const relevantIndexes = objectMetadataItem.indexMetadatas.filter(
          (index) =>
            index.isUnique &&
            index.indexFieldMetadatas.some(
              (ifm) => ifm.fieldMetadataId === field.id,
            ),
        );

        for (const index of relevantIndexes) {
          if (!isDryRun) {
            this.logger.log(
              `Removing where clause from index ${index.name} on ${tableName}`,
            );

            await this.indexMetadataRepository.update(index.id, {
              indexWhereClause: null,
            });

            await this.workspaceManyOrAllFlatEntityMapsCacheService.invalidateFlatEntityMaps(
              {
                workspaceId,
                flatMapsKeys: ['flatIndexMaps'],
              },
            );

            const columnNames = index.indexFieldMetadatas.flatMap(
              (indexFieldMetadata) => {
                const fieldMetadata = objectMetadataItem.fields.find(