-
Notifications
You must be signed in to change notification settings - Fork 4.6k
chore: Add empty policyMap when policies are empty or null to fix NPE #36374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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( | ||
| "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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned above this is equivalent to |
||
|
|
||
| return convertToMap | ||
| .elapsed() | ||
| .doOnSuccess(it -> log.info( | ||
| "Migrated {} documents in {} in {}ms", | ||
| it.getT2().getModifiedCount(), | ||
| collectionName, | ||
| it.getT1())) | ||
| .then(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
policyMaprefactor to have complete control over which collections are getting migrated. Should we just add the EE collection names as well in that case?There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't be required as no objects will be returned.