Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.appsmith.server.migrations.db.ce;

import com.appsmith.external.helpers.Stopwatch;
import com.mongodb.client.result.UpdateResult;
import io.mongock.api.annotations.ChangeUnit;
import io.mongock.api.annotations.Execution;
import io.mongock.api.annotations.RollbackExecution;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.core.query.UpdateDefinition;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Set;

import static com.appsmith.server.migrations.constants.FieldName.POLICY_MAP;
import static org.springframework.data.mongodb.core.query.Criteria.where;

@RequiredArgsConstructor
@Slf4j
@ChangeUnit(order = "062", id = "add_empty_policyMap_for_null_entries")
public class Migration062AddEmptyPolicyMapForNullValues {

private final ReactiveMongoTemplate mongoTemplate;

private static final Set<String> CE_COLLECTION_NAMES = Set.of(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to use mongoTemplate and run this migration for every collection in the db? This way we can avoid adding a EE migration and also not missing any collection.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping to mimic the policyMap refactor to have complete control over which collections are getting migrated. Should we just add the EE collection names as well in that case?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case lets stick to this approach, this will save us time in running this migration on prod. Yes we should add EE collection names and handle collection does not exists kind of errors if required.

@abhvsn abhvsn Sep 17, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle collection does not exists kind of errors if required.

This won't be required as no objects will be returned.

"actionCollection",
"application",
"applicationSnapshot",
"asset",
"collection",
"config",
"customJSLib",
"datasource",
"datasourceStorage",
"datasourceStorageStructure",
"emailVerificationToken",
"gitDeployKeys",
"newAction",
"newPage",
"passwordResetToken",
"permissionGroup",
"plugin",
"theme",
"user",
"userData",
"workspace");

@RollbackExecution
public void rollbackExecution() {}

@Execution
public void execute() {
Stopwatch stopwatch = new Stopwatch("Migration062AddEmptyPolicyMapForNullValues");
Mono.whenDelayError(CE_COLLECTION_NAMES.stream()
.map(c -> addEmptyPolicyMapForNullEntries(mongoTemplate, c))
.toList())
.onErrorResume(error -> {
String errorPrefix = "Error while adding empty policyMap for null values";
// As we are using Mono.whenDelayError, we expect multiple errors to be suppressed in a single error
if (error.getSuppressed().length > 0) {
for (Throwable suppressed : error.getSuppressed()) {
log.error(errorPrefix, suppressed);
}
} else {
log.error(errorPrefix, error);
}
return Mono.error(error);
})
.block();
stopwatch.stopAndLogTimeInMillis();
}

public static Mono<Void> addEmptyPolicyMapForNullEntries(
ReactiveMongoTemplate mongoTemplate, String collectionName) {
log.info("Adding empty policyMap for empty policies {}", collectionName);

// Update policyMap for documents where policies is empty or null
Criteria policyMapNotExists =
new Criteria().orOperator(where(POLICY_MAP).exists(false));
Criteria notDeletedCriteria = where("deletedAt").isNull();

final Query query = new Query().addCriteria(policyMapNotExists).addCriteria(notDeletedCriteria);
UpdateDefinition update = new Update().set(POLICY_MAP, new HashMap<>());
final Mono<UpdateResult> convertToMap = mongoTemplate.updateMulti(query, update, collectionName);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this result in an error state if the collection does not exist?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned above this is equivalent to db.random_collection.find() which doesn't return any result so should not be an issue for us.


return convertToMap
.elapsed()
.doOnSuccess(it -> log.info(
"Migrated {} documents in {} in {}ms",
it.getT2().getModifiedCount(),
collectionName,
it.getT1()))
.then();
}
}