Skip to content
Open
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
40 changes: 32 additions & 8 deletions flyway-core/src/main/java/org/flywaydb/core/MongoFlyway.java
Original file line number Diff line number Diff line change
Expand Up @@ -669,14 +669,35 @@ public void setTargetAsString(String target) {
*
* Providing a MongoClient through this method will override the client created by using
* the mongo uri. Use this method to retain the control over MongoClient instance.
*
* The databaseName will be supplied from a flyway.mongoUri property.
*
* @param client The MongoClient to use. Must have the necessary privileges to execute ddl.
*/
public void setMongoClient(MongoClient client) {
setMongoClient(client, null);
}

/**
* Sets the MongoClient to use. Must have the necessary privileges to execute ddl.
*
* Providing a MongoClient through this method will override the client created by using
* the mongo uri. Use this method to retain the control over MongoClient instance.
*
* @param client The MongoClient to use. Must have the necessary privileges to execute ddl.
*/
public void setMongoClient(MongoClient client, String databaseName) {
if (this.client != null) {
this.client.close();
}
if (databaseName != null) {
this.databaseName = databaseName;
}
this.client = client;
createdMongoClient = false;
}


/**
* Sets the MongoClientURI to use.
*
Expand All @@ -685,13 +706,15 @@ public void setMongoClient(MongoClient client) {
* @param uri MongoClient URI used to connect to a MongoDB database server.
*/
public void setMongoClientUri(String uri) {
MongoClientURI mongoUri = new MongoClientURI(uri);
this.databaseName = mongoUri.getDatabase();
if (databaseName == null) {
throw new FlywayException("Cannot find database from Mongo URI!");
}
this.client = new MongoClient(mongoUri);
createdMongoClient = true;
if (client == null) {
MongoClientURI mongoUri = new MongoClientURI(uri);
this.client = new MongoClient(mongoUri);
createdMongoClient = true;
this.databaseName = mongoUri.getDatabase();
if (databaseName == null) {
throw new FlywayException("Cannot find database from Mongo URI!");
}
}
}

/**
Expand Down Expand Up @@ -1046,7 +1069,8 @@ public void configure(Properties properties) {
if (StringUtils.hasText(uriProp)) {
setMongoClientUri(uriProp);
} else {
LOG.warn("Incomplete MongoDB configuration! flyway.mongoUri must be set.");
LOG.warn("Incomplete MongoDB configuration! flyway.mongoUri must be set unless you supply your" +
"own externally created mongoClient.");
}

String locationsProp = props.remove("flyway.locations");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,4 +578,22 @@ public void failed() {
LogFactory.setLogCreator(null);
}
}

@Test
public void validExternalMongoClient() {

Properties props = new Properties();
props.setProperty("flyway.locations", "db.migrations.mongo");
props.setProperty("flyway.validateOnMigrate", "false");
// Do not set flywway.mongoUri
//props.setProperty("flyway.mongoUri", getMongoUri());

MongoFlyway flyway = new MongoFlyway();
flyway.configure(props);
// Set mongo client so that flyway does not close it.
flyway.setMongoClient(getMongoClient(), getDatabaseName());
flyway.migrate();


}
}