Skip to content

Commit b76c159

Browse files
committed
fix: remove deprecated use of MongoDatabase.listCollectionNames()
1 parent 32280b2 commit b76c159

6 files changed

Lines changed: 37 additions & 54 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77

88

99
group = "io.flamingock"
10-
version = "1.1.0-SNAPSHOT"
10+
version = "1.3.0-SNAPSHOT"
1111

1212
val flamingockVersion = "1.2.0-beta.2"//only for test
1313
val templateApiVersion = "1.3.1"

src/main/java/io/flamingock/template/mongodb/model/operator/CreateCollectionOperator.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.flamingock.template.mongodb.model.operator;
1717

18+
import com.mongodb.MongoCommandException;
1819
import com.mongodb.client.ClientSession;
1920
import com.mongodb.client.MongoDatabase;
2021
import io.flamingock.template.mongodb.model.MongoOperation;
@@ -27,11 +28,15 @@ public CreateCollectionOperator(MongoDatabase mongoDatabase, MongoOperation oper
2728

2829
@Override
2930
public void applyInternal(ClientSession clientSession) {
30-
if (DatabaseInspector.collectionExists(mongoDatabase, op.getCollection())) {
31-
logger.info("Collection '{}' already exists, skipping createCollection", op.getCollection());
32-
return;
31+
try {
32+
mongoDatabase.createCollection(op.getCollection());
33+
} catch (MongoCommandException e) {
34+
if (e.getErrorCode() == 48) { // NamespaceExists
35+
logger.info("Collection '{}' already exists, skipping createCollection", op.getCollection());
36+
return;
37+
}
38+
throw e;
3339
}
34-
mongoDatabase.createCollection(op.getCollection());
3540
}
3641

3742
}

src/main/java/io/flamingock/template/mongodb/model/operator/CreateViewOperator.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.flamingock.template.mongodb.model.operator;
1717

18+
import com.mongodb.MongoCommandException;
1819
import com.mongodb.client.ClientSession;
1920
import com.mongodb.client.MongoDatabase;
2021
import com.mongodb.client.model.CreateViewOptions;
@@ -34,12 +35,16 @@ public CreateViewOperator(MongoDatabase mongoDatabase, MongoOperation operation)
3435

3536
@Override
3637
protected void applyInternal(ClientSession clientSession) {
37-
if (DatabaseInspector.collectionExists(mongoDatabase, op.getCollection())) {
38-
logger.info("View '{}' already exists, skipping createView", op.getCollection());
39-
return;
38+
try {
39+
CreateViewOptions options = CreateViewOptionsMapper.map(op.getOptions());
40+
mongoDatabase.createView(op.getCollection(), getViewOn(), getPipeline(), options);
41+
} catch (MongoCommandException e) {
42+
if (e.getErrorCode() == 48) { // NamespaceExists
43+
logger.info("View '{}' already exists, skipping createView", op.getCollection());
44+
return;
45+
}
46+
throw e;
4047
}
41-
CreateViewOptions options = CreateViewOptionsMapper.map(op.getOptions());
42-
mongoDatabase.createView(op.getCollection(), getViewOn(), getPipeline(), options);
4348
}
4449

4550
private String getViewOn() {

src/main/java/io/flamingock/template/mongodb/model/operator/DatabaseInspector.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ final class DatabaseInspector {
3030
private DatabaseInspector() {
3131
}
3232

33-
static boolean collectionExists(MongoDatabase database, String collectionName) {
34-
return database.listCollectionNames()
35-
.into(new ArrayList<>())
36-
.contains(collectionName);
37-
}
38-
3933
static boolean indexExistsByName(MongoDatabase database, String collection, String indexName) {
4034
List<Document> indexes = database.getCollection(collection)
4135
.listIndexes()

src/main/java/io/flamingock/template/mongodb/model/operator/RenameCollectionOperator.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,6 @@ public RenameCollectionOperator(MongoDatabase mongoDatabase, MongoOperation oper
3131
@Override
3232
protected void applyInternal(ClientSession clientSession) {
3333
String targetName = getTarget();
34-
boolean sourceExists = DatabaseInspector.collectionExists(mongoDatabase, op.getCollection());
35-
boolean targetExists = DatabaseInspector.collectionExists(mongoDatabase, targetName);
36-
37-
if (!sourceExists && targetExists) {
38-
logger.info("Collection '{}' already renamed to '{}', skipping renameCollection", op.getCollection(), targetName);
39-
return;
40-
}
41-
42-
if (!sourceExists) {
43-
logger.warn("Source collection '{}' does not exist and target '{}' is also absent — skipping renameCollection",
44-
op.getCollection(), targetName);
45-
return;
46-
}
47-
4834
MongoNamespace target = new MongoNamespace(mongoDatabase.getName(), targetName);
4935
RenameCollectionOptions options = RenameCollectionOptionsMapper.map(op.getOptions());
5036
mongoDatabase.getCollection(op.getCollection()).renameCollection(target, options);

src/test/java/io/flamingock/template/mongodb/operations/RenameCollectionOperatorTest.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.flamingock.template.mongodb.operations;
1717

18+
import io.flamingock.template.mongodb.MongoTemplateExecutionException;
1819
import io.flamingock.template.mongodb.model.MongoOperation;
1920
import io.flamingock.template.mongodb.model.operator.RenameCollectionOperator;
2021
import org.junit.jupiter.api.BeforeEach;
@@ -26,9 +27,6 @@
2627
import java.util.List;
2728
import java.util.Map;
2829

29-
import io.flamingock.template.mongodb.MongoTemplateExecutionException;
30-
31-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3230
import static org.junit.jupiter.api.Assertions.assertFalse;
3331
import static org.junit.jupiter.api.Assertions.assertThrows;
3432
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -50,12 +48,7 @@ void renameCollectionTest() {
5048
mongoDatabase.createCollection(ORIGINAL_NAME);
5149
assertTrue(collectionExists(ORIGINAL_NAME), "Original collection should exist before rename");
5250

53-
MongoOperation operation = new MongoOperation();
54-
operation.setType("renameCollection");
55-
operation.setCollection(ORIGINAL_NAME);
56-
Map<String, Object> params = new HashMap<>();
57-
params.put("target", RENAMED_NAME);
58-
operation.setParameters(params);
51+
MongoOperation operation = buildRenameOperation();
5952

6053
RenameCollectionOperator operator = new RenameCollectionOperator(mongoDatabase, operation);
6154
operator.apply(null);
@@ -65,8 +58,8 @@ void renameCollectionTest() {
6558
}
6659

6760
@Test
68-
@DisplayName("WHEN renameCollection operator is applied twice THEN second call succeeds silently")
69-
void renameCollectionIdempotentTest() {
61+
@DisplayName("WHEN renameCollection is applied twice THEN second call fails because source no longer exists")
62+
void renameCollectionFailsOnSecondAttemptTest() {
7063
mongoDatabase.createCollection(ORIGINAL_NAME);
7164
assertTrue(collectionExists(ORIGINAL_NAME), "Original collection should exist before rename");
7265

@@ -77,27 +70,28 @@ void renameCollectionIdempotentTest() {
7770
assertFalse(collectionExists(ORIGINAL_NAME), "Original should not exist after rename");
7871
assertTrue(collectionExists(RENAMED_NAME), "Renamed should exist after rename");
7972

80-
// Second apply should not throw (source gone, target exists → already renamed)
81-
assertDoesNotThrow(() -> operator.apply(null));
82-
assertTrue(collectionExists(RENAMED_NAME), "Renamed should still exist after second apply");
73+
// Second apply MUST fail: source is gone. Rename is a transformation, not an end-state —
74+
// re-running it is not a safe no-op.
75+
assertThrows(MongoTemplateExecutionException.class, () -> operator.apply(null));
76+
assertTrue(collectionExists(RENAMED_NAME), "Renamed should still exist after failed second apply");
8377
}
8478

8579
@Test
86-
@DisplayName("WHEN source is gone and target exists THEN operation is skipped as already renamed")
87-
void renameCollectionAlreadyRenamedTest() {
80+
@DisplayName("WHEN source is gone and target exists THEN operation fails (cannot assume it was this rename)")
81+
void renameCollectionFailsWhenSourceMissingButTargetExistsTest() {
8882
mongoDatabase.createCollection(RENAMED_NAME);
8983
assertFalse(collectionExists(ORIGINAL_NAME), "Original should not exist");
9084
assertTrue(collectionExists(RENAMED_NAME), "Target should already exist");
9185

9286
MongoOperation operation = buildRenameOperation();
9387
RenameCollectionOperator operator = new RenameCollectionOperator(mongoDatabase, operation);
94-
assertDoesNotThrow(() -> operator.apply(null));
95-
assertTrue(collectionExists(RENAMED_NAME), "Target should still exist after skipped rename");
88+
assertThrows(MongoTemplateExecutionException.class, () -> operator.apply(null));
89+
assertTrue(collectionExists(RENAMED_NAME), "Target should still exist after failed rename");
9690
}
9791

9892
@Test
99-
@DisplayName("WHEN both source and target exist THEN operation throws MongoTemplateExecutionException")
100-
void renameCollectionBothExistTest() {
93+
@DisplayName("WHEN both source and target exist THEN operation fails with MongoTemplateExecutionException")
94+
void renameCollectionFailsWhenBothExistTest() {
10195
mongoDatabase.createCollection(ORIGINAL_NAME);
10296
mongoDatabase.createCollection(RENAMED_NAME);
10397
assertTrue(collectionExists(ORIGINAL_NAME), "Source should exist");
@@ -109,15 +103,14 @@ void renameCollectionBothExistTest() {
109103
}
110104

111105
@Test
112-
@DisplayName("WHEN neither source nor target exists THEN operation is skipped gracefully")
113-
void renameCollectionNeitherExistsTest() {
106+
@DisplayName("WHEN neither source nor target exists THEN operation fails because source is missing")
107+
void renameCollectionFailsWhenNeitherExistsTest() {
114108
assertFalse(collectionExists(ORIGINAL_NAME), "Source should not exist");
115109
assertFalse(collectionExists(RENAMED_NAME), "Target should not exist");
116110

117111
MongoOperation operation = buildRenameOperation();
118112
RenameCollectionOperator operator = new RenameCollectionOperator(mongoDatabase, operation);
119-
// Should skip silently — both absent means nothing to rename and nothing was renamed
120-
assertDoesNotThrow(() -> operator.apply(null));
113+
assertThrows(MongoTemplateExecutionException.class, () -> operator.apply(null));
121114
assertFalse(collectionExists(ORIGINAL_NAME), "Source should still not exist");
122115
assertFalse(collectionExists(RENAMED_NAME), "Target should still not exist");
123116
}

0 commit comments

Comments
 (0)