Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -21,9 +21,10 @@
import io.flamingock.api.annotations.Nullable;
import io.flamingock.api.annotations.Rollback;
import io.flamingock.api.template.AbstractChangeTemplate;
import io.flamingock.template.mongodb.model.MongoApplyPayload;
import io.flamingock.template.mongodb.model.MongoOperation;

public class MongoChangeTemplate extends AbstractChangeTemplate<Void, MongoOperation, MongoOperation> {
public class MongoChangeTemplate extends AbstractChangeTemplate<Void, MongoApplyPayload, MongoApplyPayload> {

public MongoChangeTemplate() {
super(MongoOperation.class);
Expand All @@ -34,19 +35,24 @@ public void apply(MongoDatabase db, @Nullable ClientSession clientSession) {
if (this.isTransactional && clientSession == null) {
throw new IllegalArgumentException(String.format("Transactional change[%s] requires transactional ecosystem with ClientSession", changeId));
}
executeOp(db, applyPayload, clientSession);
executeOperations(db, applyPayload, clientSession);
}

@Rollback
public void rollback(MongoDatabase db, @Nullable ClientSession clientSession) {
if (this.isTransactional && clientSession == null) {
throw new IllegalArgumentException(String.format("Transactional change[%s] requires transactional ecosystem with ClientSession", changeId));
}
executeOp(db, rollbackPayload, clientSession);
executeOperations(db, rollbackPayload, clientSession);
}

private void executeOp(MongoDatabase db, MongoOperation op, ClientSession clientSession) {
op.getOperator(db).apply(clientSession);
private void executeOperations(MongoDatabase db, MongoApplyPayload payload, ClientSession clientSession) {
if (payload == null) {
return;
}
for (MongoOperation op : payload.getOperations()) {
op.getOperator(db).apply(clientSession);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2025 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.flamingock.template.mongodb.mapper;

import com.mongodb.client.model.CreateViewOptions;

import java.util.Map;

import static io.flamingock.template.mongodb.mapper.MapperUtil.getCollation;

public final class CreateViewOptionsMapper {

private CreateViewOptionsMapper() {}

public static CreateViewOptions map(Map<String, Object> options) {
CreateViewOptions result = new CreateViewOptions();

if (options.containsKey("collation")) {
result.collation(getCollation(options, "collation"));
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2025 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.flamingock.template.mongodb.mapper;

import com.mongodb.client.model.RenameCollectionOptions;

import java.util.Map;

import static io.flamingock.template.mongodb.mapper.MapperUtil.getBoolean;

public final class RenameCollectionOptionsMapper {

private RenameCollectionOptionsMapper() {}

public static RenameCollectionOptions map(Map<String, Object> options) {
RenameCollectionOptions result = new RenameCollectionOptions();

if (options.containsKey("dropTarget")) {
result.dropTarget(getBoolean(options, "dropTarget"));
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2025 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.flamingock.template.mongodb.model;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Container class for MongoDB apply/rollback payload that supports both:
* <ul>
* <li>Single operation format (backward compatible):
* <pre>
* apply:
* type: createCollection
* collection: users
* </pre>
* </li>
* <li>Multiple operations format:
* <pre>
* apply:
* operations:
* - type: createCollection
* collection: users
* - type: createIndex
* collection: users
* parameters:
* keys: { name: 1 }
* </pre>
* </li>
* </ul>
*/
public class MongoApplyPayload {

private List<MongoOperation> operations;

// For backward compatibility
private String type;
private String collection;
private Map<String, Object> parameters;

/**
* Returns the list of operations to execute.
* Handles both formats:
* <ul>
* <li>Multiple: returns the operations list directly</li>
* <li>Single: wraps the single operation in a list</li>
* </ul>
*
* @return list of operations to execute, never null
*/
public List<MongoOperation> getOperations() {
if (operations != null && !operations.isEmpty()) {
return operations;
}
if (type != null) {
MongoOperation singleOp = new MongoOperation();
singleOp.setType(type);
singleOp.setCollection(collection);
singleOp.setParameters(parameters != null ? parameters : new HashMap<>());
return Collections.singletonList(singleOp);
}
return Collections.emptyList();
}

public void setOperations(List<MongoOperation> operations) {
this.operations = operations;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getCollection() {
return collection;
}

public void setCollection(String collection) {
this.collection = collection;
}

public Map<String, Object> getParameters() {
return parameters;
}

public void setParameters(Map<String, Object> parameters) {
this.parameters = parameters;
}

@Override
public String toString() {
if (operations != null && !operations.isEmpty()) {
return "MongoApplyPayload{operations=" + operations + "}";
}
return "MongoApplyPayload{type='" + type + "', collection='" + collection + "', parameters=" + parameters + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,40 @@ public Document getFilter() {
return new Document((Map<String, Object>) parameters.get("filter"));
}

public String getIndexName() {
Object value = parameters.get("indexName");
return value != null ? (String) value : null;
}

public String getTarget() {
return (String) parameters.get("target");
}

@SuppressWarnings("unchecked")
public Document getValidator() {
Object value = parameters.get("validator");
return value != null ? new Document((Map<String, Object>) value) : null;
}

public String getValidationLevel() {
return (String) parameters.get("validationLevel");
}

public String getValidationAction() {
return (String) parameters.get("validationAction");
}

public String getViewOn() {
return (String) parameters.get("viewOn");
}

@SuppressWarnings("unchecked")
public List<Document> getPipeline() {
List<Map<String, Object>> rawPipeline = (List<Map<String, Object>>) parameters.get("pipeline");
return rawPipeline != null
? rawPipeline.stream().map(Document::new).collect(Collectors.toList())
: null;
}

public MongoOperator getOperator(MongoDatabase db) {
return MongoOperationType.getFromValue(getType()).getOperator(db, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@
import com.mongodb.client.MongoDatabase;
import io.flamingock.template.mongodb.model.operator.CreateCollectionOperator;
import io.flamingock.template.mongodb.model.operator.CreateIndexOperator;
import io.flamingock.template.mongodb.model.operator.CreateViewOperator;
import io.flamingock.template.mongodb.model.operator.DropCollectionOperator;
import io.flamingock.template.mongodb.model.operator.DropIndexOperator;
import io.flamingock.template.mongodb.model.operator.DropViewOperator;
import io.flamingock.template.mongodb.model.operator.InsertOperator;
import io.flamingock.template.mongodb.model.operator.ModifyCollectionOperator;
import io.flamingock.template.mongodb.model.operator.MongoOperator;
import io.flamingock.template.mongodb.model.operator.RenameCollectionOperator;

import java.util.Arrays;
import java.util.function.BiFunction;
Expand All @@ -28,7 +34,13 @@ public enum MongoOperationType {

CREATE_COLLECTION("createCollection", CreateCollectionOperator::new),
CREATE_INDEX("createIndex", CreateIndexOperator::new),
INSERT("insert", InsertOperator::new);
INSERT("insert", InsertOperator::new),
DROP_COLLECTION("dropCollection", DropCollectionOperator::new),
DROP_INDEX("dropIndex", DropIndexOperator::new),
RENAME_COLLECTION("renameCollection", RenameCollectionOperator::new),
MODIFY_COLLECTION("modifyCollection", ModifyCollectionOperator::new),
CREATE_VIEW("createView", CreateViewOperator::new),
DROP_VIEW("dropView", DropViewOperator::new);

private final String value;
private final BiFunction<MongoDatabase, MongoOperation, MongoOperator> createOperatorFunction;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2025 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.flamingock.template.mongodb.model.operator;

import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.CreateViewOptions;
import io.flamingock.template.mongodb.mapper.CreateViewOptionsMapper;
import io.flamingock.template.mongodb.model.MongoOperation;

public class CreateViewOperator extends MongoOperator {

public CreateViewOperator(MongoDatabase mongoDatabase, MongoOperation operation) {
super(mongoDatabase, operation, false);
}

@Override
protected void applyInternal(ClientSession clientSession) {
CreateViewOptions options = CreateViewOptionsMapper.map(op.getOptions());
mongoDatabase.createView(op.getCollection(), op.getViewOn(), op.getPipeline(), options);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2025 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.flamingock.template.mongodb.model.operator;

import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoDatabase;
import io.flamingock.template.mongodb.model.MongoOperation;

public class DropCollectionOperator extends MongoOperator {

public DropCollectionOperator(MongoDatabase mongoDatabase, MongoOperation operation) {
super(mongoDatabase, operation, false);
}

@Override
protected void applyInternal(ClientSession clientSession) {
mongoDatabase.getCollection(op.getCollection()).drop();
}
}
Loading
Loading