Skip to content

Commit

Permalink
delete jongo and com.mongodb.DB support
Browse files Browse the repository at this point in the history
  • Loading branch information
cybuch committed Aug 9, 2020
1 parent 4ef9e71 commit c21834c
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 153 deletions.
61 changes: 28 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ With Maven
<dependency>
<groupId>com.github.cybuch</groupId>
<artifactId>mongobeej</artifactId>
<version>0.17</version>
<version>1.0.0</version>
</dependency>
```
With Gradle
```groovy
compile 'com.github.cybuch:mongobeej:0.17'
compile 'com.github.cybuch:mongobeej:1.0.0'
```

### Migrating from Mongobee
Expand All @@ -38,9 +38,22 @@ In this case the migration process will be executed automatically on startup.

```java
@Bean
public Mongobee mongobee(){
public Mongobee mongobee() {
Mongobee runner = new Mongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME");
runner.setDbName("yourDbName"); // host must be set if not set in URI
runner.setDbName("yourDbName"); // db name must be set if not set in URI
runner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // the package to be scanned for changesets
return runner;
}
```

or

```java
@Bean
public Mongobee mongobee(MongoClient mongoClient) {
Mongobee runner = new MongobeemongoClient);
runner.setDbName("yourDbName"); // db name must be set
runner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // the package to be scanned for changesets
return runner;
Expand All @@ -53,7 +66,7 @@ Using mongobee without a spring context has similar configuration but you have t

```java
Mongobee runner = new Mongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME");
runner.setDbName("yourDbName"); // host must be set if not set in URI
runner.setDbName("yourDbName"); // db name must be set if not set in URI
runner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // package to scan for changesets
runner.execute(); // ------> starts migration changesets
Expand All @@ -71,7 +84,7 @@ MongoDB URI format:
```
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database[.collection]][?options]]
```
[More about URI](http://mongodb.github.io/mongo-java-driver/3.5/javadoc/)
[More about URI](https://docs.mongodb.com/manual/reference/connection-string/)


### Creating change logs
Expand All @@ -85,7 +98,7 @@ package com.example.yourapp.changelogs;
public class DatabaseChangelog {

@ChangeSet(order = "001", id = "someChangeId", author = "testAuthor")
public void importantWorkToDo(DB db){
public void importantWorkToDo(MongoTemplate mongoTemplate) {
// task implementation
}
}
Expand Down Expand Up @@ -134,34 +147,16 @@ public void someChange2(MongoDatabase db) {
mycollection.insertOne(doc);
}

@ChangeSet(order = "003", id = "someChangeWithDb", author = "testAuthor")
public void someChange3(DB db) {
// This is deprecated in mongo-java-driver 3.x, use MongoDatabase instead
// type: com.mongodb.DB : original MongoDB driver v. 2.x, operations allowed by driver are possible
// example:
DBCollection mycollection = db.getCollection("mycollection");
BasicDBObject doc = new BasicDBObject().append("test", "1");
mycollection .insert(doc);
}

@ChangeSet(order = "004", id = "someChangeWithJongo", author = "testAuthor")
public void someChange4(Jongo jongo) {
// type: org.jongo.Jongo : Jongo driver can be used, used for simpler notation
// example:
MongoCollection mycollection = jongo.getCollection("mycollection");
mycollection.insert("{test : 1}");
}

@ChangeSet(order = "005", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
public void someChange5(MongoTemplate mongoTemplate) {
@ChangeSet(order = "003", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
public void someChange3(MongoTemplate mongoTemplate) {
// type: org.springframework.data.mongodb.core.MongoTemplate
// Spring Data integration allows using MongoTemplate in the ChangeSet
// example:
mongoTemplate.save(myEntity);
}

@ChangeSet(order = "006", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
public void someChange5(MongoTemplate mongoTemplate, Environment environment) {
@ChangeSet(order = "004", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
public void someChange4(MongoTemplate mongoTemplate, Environment environment) {
// type: org.springframework.data.mongodb.core.MongoTemplate
// type: org.springframework.core.env.Environment
// Spring Data integration allows using MongoTemplate and Environment in the ChangeSet
Expand All @@ -177,7 +172,7 @@ _Example 1_: annotated change set will be invoked for a `dev` profile
```java
@Profile("dev")
@ChangeSet(author = "testuser", id = "myDevChangest", order = "01")
public void devEnvOnly(DB db){
public void devEnvOnly(MongoTemplate mongoTemplate) {
// ...
}
```
Expand All @@ -187,7 +182,7 @@ _Example 2_: all change sets in a changelog will be invoked for a `test` profile
@Profile("test")
public class ChangelogForTestEnv{
@ChangeSet(author = "testuser", id = "myTestChangest", order = "01")
public void testingEnvOnly(DB db){
public void testingEnvOnly(MongoTemplate mongoTemplate) {
// ...
}
}
Expand Down Expand Up @@ -226,13 +221,13 @@ You can exclude mongo-java-driver from **mongobeeJ** and use your dependency on
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.0</version>
<version>3.12.7</version>
</dependency>

<dependency>
<groupId>com.github.mongobeej</groupId>
<artifactId>mongobeej</artifactId>
<version>0.17</version>
<version>1.0.0</version>
<exclusions>
<exclusion>
<groupId>org.mongodb</groupId>
Expand Down
9 changes: 2 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>com.github.cybuch</groupId>
<artifactId>mongobeej</artifactId>
<version>0.18-SNAPSHOT</version>
<version>1.0.0</version>

<licenses>
<license>
Expand Down Expand Up @@ -54,7 +54,7 @@
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.9.1</version>
<version>3.12.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
Expand All @@ -71,11 +71,6 @@
<artifactId>reflections</artifactId>
<version>0.9.11</version>
</dependency>
<dependency>
<groupId>org.jongo</groupId>
<artifactId>jongo</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
Expand Down
42 changes: 10 additions & 32 deletions src/main/java/com/github/mongobeej/Mongobee.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import com.github.mongobeej.exception.MongobeeConnectionException;
import com.github.mongobeej.exception.MongobeeException;
import com.github.mongobeej.utils.ChangeService;
import com.mongodb.*;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import org.jongo.Jongo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
Expand Down Expand Up @@ -41,19 +43,15 @@ public class Mongobee implements InitializingBean {
private static final long DEFAULT_CHANGE_LOG_LOCK_WAIT_TIME = 5L;
private static final long DEFAULT_CHANGE_LOG_LOCK_POLL_RATE = 10L;
private static final boolean DEFAULT_THROW_EXCEPTION_IF_CANNOT_OBTAIN_LOCK = false;

private ChangeEntryDao dao;

private boolean enabled = true;
private String changeLogsScanPackage;
private MongoClientURI mongoClientURI;
private MongoClient legacyMongoClient;
private com.mongodb.client.MongoClient mongoClient;
private String dbName;
private Environment springEnvironment;

private MongoTemplate mongoTemplate;
private Jongo jongo;


/**
Expand Down Expand Up @@ -159,7 +157,9 @@ public void execute() throws MongobeeException {

validateConfig();

if (this.legacyMongoClient != null) {
if (mongoClient != null) {
dao.connectMongoDb(mongoClient, dbName);
} else if (this.legacyMongoClient != null) {
dao.connectMongoDb(this.legacyMongoClient, dbName);
} else {
dao.connectMongoDb(this.mongoClientURI, dbName);
Expand Down Expand Up @@ -188,7 +188,7 @@ private void executeMigration() throws MongobeeConnectionException, MongobeeExce

for (Class<?> changelogClass : service.fetchChangeLogs()) {

Object changelogInstance = null;
Object changelogInstance;
try {
changelogInstance = changelogClass.getConstructor().newInstance();
List<Method> changesetMethods = service.fetchChangeSets(changelogInstance.getClass());
Expand All @@ -198,11 +198,11 @@ private void executeMigration() throws MongobeeConnectionException, MongobeeExce

try {
if (dao.isNewChange(changeEntry)) {
executeChangeSetMethod(changesetMethod, changelogInstance, dao.getDb(), dao.getMongoDatabase());
executeChangeSetMethod(changesetMethod, changelogInstance, dao.getMongoDatabase());
dao.save(changeEntry);
logger.info(changeEntry + " applied");
} else if (service.isRunAlwaysChangeSet(changesetMethod)) {
executeChangeSetMethod(changesetMethod, changelogInstance, dao.getDb(), dao.getMongoDatabase());
executeChangeSetMethod(changesetMethod, changelogInstance, dao.getMongoDatabase());
logger.info(changeEntry + " reapplied");
} else {
logger.info(changeEntry + " passed over");
Expand All @@ -223,20 +223,9 @@ private void executeMigration() throws MongobeeConnectionException, MongobeeExce
private Object executeChangeSetMethod(
Method changeSetMethod,
Object changeLogInstance,
DB db,
MongoDatabase mongoDatabase
) throws IllegalAccessException, InvocationTargetException, MongobeeChangeSetException {
if (changeSetMethod.getParameterTypes().length == 1
&& changeSetMethod.getParameterTypes()[0].equals(DB.class)) {
logger.debug("method with DB argument");

return changeSetMethod.invoke(changeLogInstance, db);
} else if (changeSetMethod.getParameterTypes().length == 1
&& changeSetMethod.getParameterTypes()[0].equals(Jongo.class)) {
logger.debug("method with Jongo argument");

return changeSetMethod.invoke(changeLogInstance, jongo != null ? jongo : new Jongo(db));
} else if (changeSetMethod.getParameterTypes().length == 1
&& changeSetMethod.getParameterTypes()[0].equals(MongoTemplate.class)) {
logger.debug("method with MongoTemplate argument");

Expand Down Expand Up @@ -455,17 +444,6 @@ public Mongobee setMongoTemplate(MongoTemplate mongoTemplate) {
return this;
}

/**
* Sets pre-configured {@link MongoTemplate} instance to use by the Mongobee
*
* @param jongo {@link Jongo} instance
* @return Mongobee object for fluent interface
*/
public Mongobee setJongo(Jongo jongo) {
this.jongo = jongo;
return this;
}

/**
* Overwrites a default mongobeej changelog collection hardcoded in DEFAULT_CHANGELOG_COLLECTION_NAME.
* <p>
Expand Down
42 changes: 24 additions & 18 deletions src/main/java/com/github/mongobeej/dao/ChangeEntryDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import com.github.mongobeej.exception.MongobeeConfigurationException;
import com.github.mongobeej.exception.MongobeeConnectionException;
import com.github.mongobeej.exception.MongobeeLockException;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
Expand All @@ -21,8 +21,8 @@ public class ChangeEntryDao {
private static final Logger logger = LoggerFactory.getLogger("Mongobee dao");

private MongoDatabase mongoDatabase;
private DB db; // only for Jongo driver compatibility - do not use in other contexts
private MongoClient mongoClient;
private com.mongodb.client.MongoClient mongoClient;
private MongoClient legacyMongoClient;
private ChangeEntryIndexDao indexDao;
private String changelogCollectionName;
private boolean waitForLock;
Expand Down Expand Up @@ -52,34 +52,35 @@ public MongoDatabase getMongoDatabase() {
return mongoDatabase;
}

/**
* @return com.mongodb.DB
* @deprecated implemented only for Jongo driver compatibility and backward compatibility - do not use in other contexts
*/
public DB getDb() {
return db;
public MongoDatabase connectMongoDb(com.mongodb.client.MongoClient mongoClient, String dbName) throws MongobeeConfigurationException {
if (!hasText(dbName)) {
throw new MongobeeConfigurationException("DB name is not set. Should be defined in MongoDB URI or via setter");
} else {
this.mongoClient = mongoClient;
mongoDatabase = mongoClient.getDatabase(dbName);
ensureChangeLogCollectionIndex(mongoDatabase.getCollection(changelogCollectionName));
initializeLock();
return mongoDatabase;
}
}


@Deprecated
public MongoDatabase connectMongoDb(MongoClient mongo, String dbName) throws MongobeeConfigurationException {
if (!hasText(dbName)) {
throw new MongobeeConfigurationException("DB name is not set. Should be defined in MongoDB URI or via setter");
} else {

this.mongoClient = mongo;

db = mongo.getDB(dbName); // for Jongo driver and backward compatibility (constructor has required parameter Jongo(DB) )
this.legacyMongoClient = mongo;
mongoDatabase = mongo.getDatabase(dbName);

ensureChangeLogCollectionIndex(mongoDatabase.getCollection(changelogCollectionName));
initializeLock();
return mongoDatabase;
}
}

public MongoDatabase connectMongoDb(MongoClientURI mongoClientURI, String dbName)
throws MongobeeConfigurationException, MongobeeConnectionException {

final MongoClient mongoClient = new MongoClient(mongoClientURI);
throws MongobeeConfigurationException {
final com.mongodb.client.MongoClient mongoClient = MongoClients.create(mongoClientURI.toString());
final String database = (!hasText(dbName)) ? mongoClientURI.getDatabase() : dbName;
return this.connectMongoDb(mongoClient, database);
}
Expand Down Expand Up @@ -166,7 +167,12 @@ private void ensureChangeLogCollectionIndex(MongoCollection<Document> collection
}

public void close() {
this.mongoClient.close();
if (legacyMongoClient != null) {
legacyMongoClient.close();
}
if (mongoClient != null) {
mongoClient.close();
}
}

private void initializeLock() {
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/github/mongobeej/MongobeeBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.mongobeej.dao.ChangeEntryDao;
import com.github.mongobeej.dao.ChangeEntryIndexDao;
import com.github.mongobeej.utils.MongoEnvironmentCreator;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoDatabase;
import org.junit.Before;
Expand Down Expand Up @@ -31,6 +32,10 @@ public void init() throws Exception {
mongoDatabase = mongoEnvironment.getMongoDatabase();
when(changeEntryDao.connectMongoDb(any(MongoClientURI.class), anyString()))
.thenReturn(mongoDatabase);
when(changeEntryDao.connectMongoDb(any(com.mongodb.client.MongoClient.class), anyString()))
.thenReturn(mongoDatabase);
when(changeEntryDao.connectMongoDb(any(MongoClient.class), anyString()))
.thenReturn(mongoDatabase);
when(changeEntryDao.getMongoDatabase()).thenReturn(mongoDatabase);
when(changeEntryDao.acquireProcessLock()).thenReturn(true);
doCallRealMethod().when(changeEntryDao).save(any(ChangeEntry.class));
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/com/github/mongobeej/MongobeeProfileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@RunWith(MockitoJUnitRunner.class)
public class MongobeeProfileTest extends MongobeeBaseTest {
private static final int CHANGELOG_COUNT = 13;
private static final int CHANGELOG_COUNT = 10;

@Test
public void shouldRunDevProfileAndNonAnnotated() throws Exception {
Expand Down Expand Up @@ -133,6 +133,5 @@ public void shouldRunAllChangeSets() throws Exception {
@After
public void cleanUp() {
mongobee.setMongoTemplate(null);
mongobee.setJongo(null);
}
}
Loading

0 comments on commit c21834c

Please sign in to comment.