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,72 @@
import io.flamingock.api.annotations.Nullable;
import io.flamingock.api.annotations.Rollback;
import io.flamingock.api.template.AbstractChangeTemplate;
import io.flamingock.internal.util.log.FlamingockLoggerFactory;
import io.flamingock.template.mongodb.model.MongoApplyPayload;
import io.flamingock.template.mongodb.model.MongoOperation;
import org.slf4j.Logger;

public class MongoChangeTemplate extends AbstractChangeTemplate<Void, MongoOperation, MongoOperation> {
import java.util.ArrayList;
import java.util.List;

/**
* MongoDB Change Template for executing declarative MongoDB operations defined in YAML.
*
* <h2>Apply Behavior</h2>
* <p>The {@link #apply} method executes all operations defined in the payload sequentially.
* The behavior differs based on the transactional mode:</p>
*
* <h3>Transactional Mode ({@code transactional: true})</h3>
* <ul>
* <li>All operations execute within a MongoDB transaction (ClientSession)</li>
* <li>If any operation fails, MongoDB automatically rolls back the entire transaction</li>
* <li>Per-operation rollback definitions are NOT executed (the transaction handles atomicity)</li>
* </ul>
*
* <h3>Non-Transactional Mode ({@code transactional: false})</h3>
* <ul>
* <li>Each operation executes independently without a transaction</li>
* <li>Successfully completed operations are tracked</li>
* <li>If operation N fails, auto-rollback is triggered for operations 1 to N-1</li>
* <li>Auto-rollback executes per-operation rollbacks in reverse order</li>
* <li>Operations without a rollback definition are skipped during auto-rollback</li>
* <li>After auto-rollback completes, the original exception is re-thrown</li>
* </ul>
*
* <h2>YAML Example</h2>
* <pre>{@code
* id: create-orders-collection
* transactional: false
* template: MongoChangeTemplate
* targetSystem:
* id: "mongodb"
* apply:
* operations:
* - type: createCollection
* collection: orders
* rollback:
* type: dropCollection
* collection: orders
*
* - type: insert
* collection: orders
* parameters:
* documents:
* - orderId: "ORD-001"
* customer: "John Doe"
* rollback:
* type: delete
* collection: orders
* parameters:
* filter: {}
* }</pre>
*
* @see MongoOperation
* @see MongoApplyPayload
*/
public class MongoChangeTemplate extends AbstractChangeTemplate<Void, MongoApplyPayload, MongoApplyPayload> {

private static final Logger logger = FlamingockLoggerFactory.getLogger(MongoChangeTemplate.class);

public MongoChangeTemplate() {
super(MongoOperation.class);
Expand All @@ -34,19 +97,70 @@ 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);
executeOperationsWithAutoRollback(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);
executeRollbackOperations(db, applyPayload, clientSession);
}

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

List<MongoOperation> operations = payload.getOperations();

// Transactional, MongoDB handles rollback
if (this.isTransactional && clientSession != null) {
for (MongoOperation op : operations) {
op.getOperator(db).apply(clientSession);
}
return;
}

// Non-transactional: auto-rollback on failure
List<MongoOperation> successfulOps = new ArrayList<>();
for (MongoOperation op : operations) {
try {
op.getOperator(db).apply(clientSession);
successfulOps.add(op);
} catch (Exception e) {
rollbackSuccessfulOperations(db, successfulOps, clientSession);
throw e;
}
}
}

private void rollbackSuccessfulOperations(MongoDatabase db, List<MongoOperation> successfulOps, ClientSession clientSession) {
for (int i = successfulOps.size() - 1; i >= 0; i--) {
MongoOperation op = successfulOps.get(i);
if (op.getRollback() != null) {
try {
op.getRollback().getOperator(db).apply(clientSession);
} catch (Exception rollbackEx) {
logger.warn("Rollback failed for operation: {}", op, rollbackEx);
}
}
}
}

private void executeRollbackOperations(MongoDatabase db, MongoApplyPayload payload, ClientSession clientSession) {
if (payload == null) {
return;
}

List<MongoOperation> operations = payload.getOperations();
for (int i = operations.size() - 1; i >= 0; i--) {
MongoOperation op = operations.get(i);
if (op.getRollback() != null) {
op.getRollback().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 + "}";
}
}
Loading
Loading