Skip to content

Commit 15dd6ff

Browse files
authored
refactor!: derive CouchbaseAuditStore directly from CouchbaseTargetSystem (#748)
1 parent dfbc8a0 commit 15dd6ff

File tree

5 files changed

+35
-12
lines changed

5 files changed

+35
-12
lines changed

cli/flamingock-cli/src/main/java/io/flamingock/cli/service/AuditService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import io.flamingock.internal.core.context.SimpleContext;
4242
import io.flamingock.internal.core.store.AuditStore;
4343
import io.flamingock.targetsystem.dynamodb.DynamoDBTargetSystem;
44+
import io.flamingock.targetsystem.couchbase.CouchbaseTargetSystem;
4445
import io.flamingock.targetystem.mongodb.sync.MongoDBSyncTargetSystem;
4546
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
4647

@@ -180,7 +181,7 @@ private AuditStore<?> createCouchbaseAuditStore(Context context) {
180181
// Create Couchbase cluster
181182
Cluster couchbaseCluster = CouchbaseClusterFactory.createCouchbaseCluster(couchbaseConfig);
182183

183-
return new CouchbaseAuditStore(couchbaseCluster, couchbaseConfig.getBucketName())
184+
return CouchbaseAuditStore.from(new CouchbaseTargetSystem("couchbase", couchbaseCluster, couchbaseConfig.getBucketName()))
184185
.withAuditRepositoryName(couchbaseConfig.getTable());
185186
}
186187

community/flamingock-auditstore-couchbase/src/main/java/io/flamingock/community/couchbase/driver/CouchbaseAuditStore.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.flamingock.internal.core.store.CommunityAuditStore;
3030
import io.flamingock.internal.common.core.context.ContextResolver;
3131
import io.flamingock.community.couchbase.internal.CouchbaseAuditPersistence;
32+
import io.flamingock.targetsystem.couchbase.CouchbaseTargetSystem;
3233

3334
public class CouchbaseAuditStore implements CommunityAuditStore {
3435

@@ -45,11 +46,25 @@ public class CouchbaseAuditStore implements CommunityAuditStore {
4546
private boolean autoCreate = true;
4647

4748

48-
public CouchbaseAuditStore(Cluster cluster, String bucketName) {
49+
private CouchbaseAuditStore(Cluster cluster, String bucketName) {
4950
this.cluster = cluster;
5051
this.bucketName = bucketName;
5152
}
5253

54+
/**
55+
* Creates a {@link CouchbaseAuditStore} using the same Couchbase cluster and
56+
* bucket configured in the given {@link CouchbaseTargetSystem}.
57+
* <p>
58+
* Only the underlying Couchbase instance (cluster + bucket name) is reused.
59+
* No additional target-system configuration is carried over.
60+
*
61+
* @param targetSystem the target system from which to derive the cluster and bucket
62+
* @return a new audit store bound to the same Couchbase instance as the target system
63+
*/
64+
public static CouchbaseAuditStore from(CouchbaseTargetSystem targetSystem) {
65+
return new CouchbaseAuditStore(targetSystem.getCluster(), targetSystem.getBucketName());
66+
}
67+
5368
public CouchbaseAuditStore withScopeName(String scopeName) {
5469
this.scopeName = scopeName;
5570
return this;

community/flamingock-auditstore-couchbase/src/test/java/io/flamingock/community/couchbase/CouchbaseAuditStoreTest.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ void happyPath() {
8888
//Given-When
8989
Bucket bucket = cluster.bucket(BUCKET_NAME);
9090
Collection testCollection = bucket.defaultCollection();
91+
CouchbaseTargetSystem couchbaseTargetSystem = new CouchbaseTargetSystem("couchbase", cluster, BUCKET_NAME);
9192

9293
try (MockedStatic<Deserializer> mocked = Mockito.mockStatic(Deserializer.class)) {
9394
mocked.when(Deserializer::readPreviewPipelineFromFile).thenReturn(PipelineTestHelper.getPreviewPipeline(
@@ -97,8 +98,8 @@ void happyPath() {
9798
);
9899

99100
FlamingockFactory.getCommunityBuilder()
100-
.setAuditStore(new CouchbaseAuditStore(cluster, BUCKET_NAME))
101-
.addTargetSystem(new CouchbaseTargetSystem("couchbase", cluster, BUCKET_NAME))
101+
.setAuditStore(CouchbaseAuditStore.from(couchbaseTargetSystem))
102+
.addTargetSystem(couchbaseTargetSystem)
102103
.addDependency(testCollection) // for test purpose only
103104
.build()
104105
.run();
@@ -134,6 +135,7 @@ void failedWithRollback() {
134135
//Given-When
135136
Bucket bucket = cluster.bucket(BUCKET_NAME);
136137
Collection testCollection = bucket.defaultCollection();
138+
CouchbaseTargetSystem couchbaseTargetSystem = new CouchbaseTargetSystem("couchbase", cluster, BUCKET_NAME);
137139

138140
try (MockedStatic<Deserializer> mocked = Mockito.mockStatic(Deserializer.class)) {
139141
mocked.when(Deserializer::readPreviewPipelineFromFile).thenReturn(PipelineTestHelper.getPreviewPipeline(
@@ -144,8 +146,8 @@ void failedWithRollback() {
144146

145147
assertThrows(PipelineExecutionException.class, () -> {
146148
FlamingockFactory.getCommunityBuilder()
147-
.setAuditStore(new CouchbaseAuditStore(cluster, BUCKET_NAME))
148-
.addTargetSystem(new CouchbaseTargetSystem("couchbase", cluster, BUCKET_NAME))
149+
.setAuditStore(CouchbaseAuditStore.from(couchbaseTargetSystem))
150+
.addTargetSystem(couchbaseTargetSystem)
149151
.addDependency(testCollection) // for test purpose only
150152
.build()
151153
.run();
@@ -179,6 +181,7 @@ void failedWithoutRollback() {
179181
//Given-When
180182
Bucket bucket = cluster.bucket(BUCKET_NAME);
181183
Collection testCollection = bucket.defaultCollection();
184+
CouchbaseTargetSystem couchbaseTargetSystem = new CouchbaseTargetSystem("couchbase", cluster, BUCKET_NAME);
182185

183186
try (MockedStatic<Deserializer> mocked = Mockito.mockStatic(Deserializer.class)) {
184187
mocked.when(Deserializer::readPreviewPipelineFromFile).thenReturn(PipelineTestHelper.getPreviewPipeline(
@@ -189,8 +192,8 @@ void failedWithoutRollback() {
189192

190193
assertThrows(PipelineExecutionException.class, () -> {
191194
FlamingockFactory.getCommunityBuilder()
192-
.setAuditStore(new CouchbaseAuditStore(cluster, BUCKET_NAME))
193-
.addTargetSystem(new CouchbaseTargetSystem("couchbase", cluster, BUCKET_NAME))
195+
.setAuditStore(CouchbaseAuditStore.from(couchbaseTargetSystem))
196+
.addTargetSystem(couchbaseTargetSystem)
194197
.addDependency(testCollection) // for test purpose only
195198
.build()
196199
.run();

core/target-systems/couchbase-target-system/src/main/java/io/flamingock/targetsystem/couchbase/CouchbaseTargetSystem.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public Bucket getBucket() {
5858
return this.bucket;
5959
}
6060

61+
public String getBucketName() {
62+
return this.bucketName;
63+
}
64+
6165
public TransactionManager<TransactionAttemptContext> getTxManager() {
6266
return txWrapper.getTxManager();
6367
}

legacy/mongock-importer-couchbase/src/test/java/io/flamingock/importer/mongock/couchbase/CouchbaseImporterTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void GIVEN_allMongockChangeUnitsAlreadyExecuted_WHEN_migratingToFlamingockCommun
118118
CouchbaseTargetSystem targetSystem = new CouchbaseTargetSystem("couchbase-target-system", cluster, FLAMINGOCK_BUCKET_NAME);
119119

120120
Runner flamingock = FlamingockFactory.getCommunityBuilder()
121-
.setAuditStore(new CouchbaseAuditStore(cluster, FLAMINGOCK_BUCKET_NAME)
121+
.setAuditStore(CouchbaseAuditStore.from(targetSystem)
122122
.withScopeName(FLAMINGOCK_SCOPE_NAME)
123123
.withAuditRepositoryName(FLAMINGOCK_COLLECTION_NAME))
124124
.addTargetSystem(targetSystem)
@@ -145,7 +145,7 @@ void GIVEN_someChangeUnitsAlreadyExecuted_WHEN_migratingToFlamingockCommunity_TH
145145
CouchbaseTargetSystem targetSystem = new CouchbaseTargetSystem("couchbase-target-system", cluster, FLAMINGOCK_BUCKET_NAME);
146146

147147
Runner flamingock = FlamingockFactory.getCommunityBuilder()
148-
.setAuditStore(new CouchbaseAuditStore(cluster, FLAMINGOCK_BUCKET_NAME)
148+
.setAuditStore(CouchbaseAuditStore.from(targetSystem)
149149
.withScopeName(FLAMINGOCK_SCOPE_NAME)
150150
.withAuditRepositoryName(FLAMINGOCK_COLLECTION_NAME))
151151
.addTargetSystem(targetSystem)
@@ -166,7 +166,7 @@ void GIVEN_mongockAuditHistoryEmpty_WHEN_migratingToFlamingockCommunity_THEN_sho
166166
CouchbaseTargetSystem targetSystem = new CouchbaseTargetSystem("couchbase-target-system", cluster, FLAMINGOCK_BUCKET_NAME);
167167

168168
Runner flamingock = FlamingockFactory.getCommunityBuilder()
169-
.setAuditStore(new CouchbaseAuditStore(cluster, FLAMINGOCK_BUCKET_NAME)
169+
.setAuditStore(CouchbaseAuditStore.from(targetSystem)
170170
.withScopeName(FLAMINGOCK_SCOPE_NAME)
171171
.withAuditRepositoryName(FLAMINGOCK_COLLECTION_NAME))
172172
.addTargetSystem(targetSystem)
@@ -226,4 +226,4 @@ private static JsonObject createAuditObject(String value) {
226226
return doc;
227227
}
228228

229-
}
229+
}

0 commit comments

Comments
 (0)