Skip to content

Commit 8fe7d0b

Browse files
François LAROCHEtchlyah
authored andcommitted
#22 allow custom variables in migrations
1 parent cd6d9ee commit 8fe7d0b

5 files changed

Lines changed: 59 additions & 19 deletions

File tree

src/main/java/com/github/couchmove/Couchmove.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,21 @@ public Couchmove(Bucket bucket, Cluster cluster) {
6161
* @param changePath absolute or relative path of the migration folder containing {@link ChangeLog}
6262
*/
6363
public Couchmove(Bucket bucket, Cluster cluster, String changePath) {
64+
this(bucket, cluster, changePath, Collections.emptyMap());
65+
}
66+
67+
/**
68+
* Initialize a {@link Couchmove} instance
69+
*
70+
* @param bucket Couchbase {@link Bucket} to execute the migrations on
71+
* @param cluster Couchbase {@link Cluster} to execute N1ql Requests and insert FTS indexes
72+
* @param changePath absolute or relative path of the migration folder containing {@link ChangeLog}
73+
* @param customVariables custom variables that can be used in N1ql requests
74+
*/
75+
public Couchmove(Bucket bucket, Cluster cluster, String changePath, Map<String, String> customVariables) {
6476
logger.info("Connected to bucket '{}'", collectionOrBucketName = bucket.name());
6577
lockService = new ChangeLockService(bucket, cluster);
66-
dbService = new ChangeLogDBService(bucket, cluster);
78+
dbService = new ChangeLogDBService(bucket, cluster, customVariables);
6779
fileService = new ChangeLogFileService(changePath);
6880
}
6981

@@ -85,9 +97,20 @@ public Couchmove(Collection collection, Cluster cluster) {
8597
* @param changePath absolute or relative path of the migration folder containing {@link ChangeLog}
8698
*/
8799
public Couchmove(Collection collection, Cluster cluster, String changePath) {
100+
this(collection, cluster, changePath, Collections.emptyMap());
101+
}
102+
103+
/**
104+
* Initialize a {@link Couchmove} instance
105+
*
106+
* @param collection Couchbase {@link Collection} to execute the migrations on
107+
* @param cluster Couchbase {@link Cluster} to execute N1ql Requets and insert FTS indexes
108+
* @param changePath absolute or relative path of the migration folder containing {@link ChangeLog}
109+
*/
110+
public Couchmove(Collection collection, Cluster cluster, String changePath, Map<String, String> customVariables) {
88111
logger.info("Connected to collection '{}'", collectionOrBucketName = collection.name());
89112
lockService = new ChangeLockService(collection, cluster);
90-
dbService = new ChangeLogDBService(collection, cluster);
113+
dbService = new ChangeLogDBService(collection, cluster, customVariables);
91114
fileService = new ChangeLogFileService(changePath);
92115
}
93116

src/main/java/com/github/couchmove/repository/CouchbaseRepositoryImpl.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import static com.couchbase.client.java.manager.query.GetAllQueryIndexesOptions.getAllQueryIndexesOptions;
4747
import static com.couchbase.client.java.manager.query.WatchQueryIndexesOptions.watchQueryIndexesOptions;
4848
import static com.couchbase.client.java.manager.search.UpsertSearchIndexOptions.upsertSearchIndexOptions;
49-
import static com.google.common.collect.ImmutableMap.of;
5049
import static java.lang.String.format;
5150
import static lombok.AccessLevel.PACKAGE;
5251
import static org.slf4j.LoggerFactory.getLogger;
@@ -79,11 +78,18 @@ public class CouchbaseRepositoryImpl<E extends CouchbaseEntity> implements Couch
7978

8079
private final Class<E> entityClass;
8180

81+
private final Map<String, String> variables;
82+
8283
public CouchbaseRepositoryImpl(Cluster cluster, Collection collection, Class<E> entityClass) {
84+
this(cluster, collection, entityClass, Collections.emptyMap());
85+
}
86+
87+
public CouchbaseRepositoryImpl(Cluster cluster, Collection collection, Class<E> entityClass, Map<String, String> customVariables) {
8388
this.cluster = cluster;
8489
this.bucket = cluster.bucket(collection.bucketName());
8590
this.collection = getOrCreate(collection);
8691
this.entityClass = entityClass;
92+
this.variables = createVariables(customVariables, this.collection);
8793
}
8894

8995
private Collection getOrCreate(Collection collection) {
@@ -112,21 +118,35 @@ private Collection getOrCreate(Collection collection) {
112118
return collection;
113119
}
114120

121+
private Map<String, String> createVariables(Map<String, String> customVariables, Collection collection) {
122+
Map<String, String> result = new HashMap<>(customVariables);
123+
if(collection != null) {
124+
result.put(BUCKET_PARAM, collection.bucketName());
125+
result.put(SCOPE_PARAM, collection.scopeName());
126+
}
127+
return Collections.unmodifiableMap(result);
128+
}
129+
115130
public CouchbaseRepositoryImpl(Cluster cluster, Bucket bucket, Class<E> entityClass) {
131+
this(cluster, bucket, entityClass, Collections.emptyMap());
132+
}
133+
134+
public CouchbaseRepositoryImpl(Cluster cluster, Bucket bucket, Class<E> entityClass, Map<String, String> customVariables) {
116135
this.cluster = cluster;
117136
this.bucket = bucket;
118137
this.collection = bucket.defaultCollection();
119138
this.entityClass = entityClass;
139+
this.variables = createVariables(customVariables, this.collection);
120140
}
121141

122142
@Override
123143
public CouchbaseRepositoryImpl<E> withCollection(String collection) {
124-
return new CouchbaseRepositoryImpl<>(cluster, bucket.scope(this.collection.scopeName()).collection(collection), entityClass);
144+
return new CouchbaseRepositoryImpl<>(cluster, bucket.scope(this.collection.scopeName()).collection(collection), entityClass, variables);
125145
}
126146

127147
@Override
128148
public CouchbaseRepositoryImpl<E> withCollection(String scope, String collection) {
129-
return new CouchbaseRepositoryImpl<>(cluster, bucket.scope(scope).collection(collection), entityClass);
149+
return new CouchbaseRepositoryImpl<>(cluster, bucket.scope(scope).collection(collection), entityClass, variables);
130150
}
131151

132152
@Override
@@ -275,10 +295,7 @@ public boolean isEventingFunctionExists(String name) {
275295
}
276296

277297
String injectParameters(String statement) {
278-
return StrSubstitutor.replace(statement, of(
279-
BUCKET_PARAM, getBucketName(),
280-
SCOPE_PARAM, getScopeName()
281-
));
298+
return StrSubstitutor.replace(statement, variables);
282299
}
283300

284301
@Override

src/main/java/com/github/couchmove/service/ChangeLogDBService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ public class ChangeLogDBService {
3030

3131
private final CouchbaseRepository<ChangeLog> repository;
3232

33-
public ChangeLogDBService(Bucket bucket, Cluster cluster) {
34-
this.repository = new CouchbaseRepositoryImpl<>(cluster, bucket, ChangeLog.class);
33+
public ChangeLogDBService(Bucket bucket, Cluster cluster, Map<String, String> customVariables) {
34+
this.repository = new CouchbaseRepositoryImpl<>(cluster, bucket, ChangeLog.class, customVariables);
3535
}
3636

37-
public ChangeLogDBService(com.couchbase.client.java.Collection collection, Cluster cluster) {
38-
this.repository = new CouchbaseRepositoryImpl<>(cluster, collection, ChangeLog.class);
37+
public ChangeLogDBService(com.couchbase.client.java.Collection collection, Cluster cluster, Map<String, String> customVariables) {
38+
this.repository = new CouchbaseRepositoryImpl<>(cluster, collection, ChangeLog.class, customVariables);
3939
}
4040

4141
ChangeLogDBService(CouchbaseRepository<ChangeLog> repository) {

src/test/java/com/github/couchmove/CouchmoveIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class CouchmoveIT extends BaseIT {
5454
@BeforeEach
5555
public void init() {
5656
changeLogRepository = new CouchbaseRepositoryImpl<>(getCluster(), getBucket(), ChangeLog.class);
57-
changeLogDBService = new ChangeLogDBService(getBucket(), getCluster());
57+
changeLogDBService = new ChangeLogDBService(getBucket(), getCluster(), Collections.emptyMap());
5858
userRepository = new CouchbaseRepositoryImpl<>(getCluster(), getBucket(), User.class);
5959
}
6060

src/test/java/com/github/couchmove/repository/CouchbaseRepositoryIT.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ static void init() {
4848

4949
static Stream<Arguments> repositoryParams() {
5050
return Stream.of(
51-
arguments("Bucket", new CouchbaseRepositoryImpl<>(getCluster(), getBucket(), ChangeLog.class)),
52-
arguments("Collection", new CouchbaseRepositoryImpl<>(getCluster(), getBucket().collection("collection"), ChangeLog.class))
51+
arguments("Bucket", new CouchbaseRepositoryImpl<>(getCluster(), getBucket(), ChangeLog.class, Collections.singletonMap("myCollection", "test"))),
52+
arguments("Collection", new CouchbaseRepositoryImpl<>(getCluster(), getBucket().collection("collection"), ChangeLog.class, Collections.singletonMap("myCollection", "test")))
5353
);
5454
}
5555

@@ -181,9 +181,9 @@ public void should_check_fts_index_not_exists(String description, CouchbaseRepos
181181
@ParameterizedTest(name = "{0}")
182182
@MethodSource("repositoryParams")
183183
public void should_inject_params(String description, CouchbaseRepository<ChangeLog> repository) {
184-
String format = "SELECT * FROM `%s`.`%s`.test";
185-
String statement = format(format, "${bucket}", "${scope}");
186-
assertThat(((CouchbaseRepositoryImpl) repository).injectParameters(statement)).isEqualTo(format(format, getBucket().name(), repository.getScopeName()));
184+
String format = "SELECT * FROM `%s`.`%s`.%s";
185+
String statement = format(format, "${bucket}", "${scope}", "${myCollection}");
186+
assertThat(((CouchbaseRepositoryImpl) repository).injectParameters(statement)).isEqualTo(format(format, getBucket().name(), repository.getScopeName(), "test"));
187187
}
188188

189189
@ParameterizedTest(name = "{0}")

0 commit comments

Comments
 (0)