Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: version 2 overhaul #25

Merged
merged 2 commits into from
Mar 4, 2024
Merged
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
38 changes: 9 additions & 29 deletions src/main/java/ox/engine/internal/CreateIndexAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.model.IndexOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ox.engine.exception.IndexAlreadyExistsException;
Expand All @@ -13,6 +14,7 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class CreateIndexAction extends OxAction {

Expand All @@ -25,10 +27,6 @@ public class CreateIndexAction extends OxAction {
* Unique index?
*/
private boolean unique;
/**
* DropDups. Deprecated after MongoDB 3.0
*/
private boolean dropDups;
/**
* Recreate the index if not equals?
*/
Expand Down Expand Up @@ -110,18 +108,6 @@ public CreateIndexAction markAsTTL(long expireAfterSeconds) {
return this;
}

/**
* This method sets a flag to active the "dropDups" MongoDB CreateIndex Option.
* <p/>
* With this option activated, any existing duplicated values will be deleted by MongoDB automatically.
* <p/>
* If this option is not set and there are duplicated options, MongoDB will throw an exception trying to create the index.
*/
public CreateIndexAction dropDups() {
this.dropDups = true;
return this;
}

/**
* Setting this option, Ox will first verify if there is an existing index with the same name
* and with different attributes or attributes order.
Expand Down Expand Up @@ -173,26 +159,20 @@ void runAction(MongoDBConnector mongoDBConnector, MongoClient mongo, String data
}
} else {
BasicDBObject keys = parseAttributesToDBObject();
BasicDBObject opts = generateCreateIndexOptions();
IndexOptions opts = generateCreateIndexOptions();
mongoDBConnector.createIndex(collection, keys, opts);
}

}

private BasicDBObject generateCreateIndexOptions() {
BasicDBObject opts = new BasicDBObject();
opts.append("background", true);
opts.append("name", indexName);

if (unique) {
opts.append("unique", true);
if (dropDups) {
opts.append("dropDups", true);
}
}
private IndexOptions generateCreateIndexOptions() {
IndexOptions opts = new IndexOptions();
opts.background(true);
opts.name(indexName);
opts.unique(unique);

if (ttlIndex) {
opts.append("expireAfterSeconds", ttlIndexExpireAfterSeconds);
opts.expireAfter(ttlIndexExpireAfterSeconds, TimeUnit.SECONDS);
}

return opts;
Expand Down
79 changes: 31 additions & 48 deletions src/main/java/ox/engine/internal/MongoDBConnector.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ox.engine.internal;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.*;
import com.mongodb.client.*;
import com.mongodb.client.model.CreateCollectionOptions;
import com.mongodb.client.model.IndexOptions;
Expand Down Expand Up @@ -45,7 +45,7 @@ private MongoDatabase database() {
return mongo().getDatabase(config.getDatabaseName());
}

private boolean collectionExists(String collectionName) {
public boolean collectionExists(String collectionName) {
MongoIterable<String> collections = database().listCollectionNames();
for (String collection : collections) {
if (collection.equals(collectionName)) {
Expand Down Expand Up @@ -151,48 +151,39 @@ private void validateCollection(OxAction action) {
}

public void insertMigrationVersion(Integer version) {
BasicDBObject dbObject =
new BasicDBObject(
Configuration.MIGRATION_COLLECTION_VERSION_ATTRIBUTE,
version);
Document doc = new Document()
.append(Configuration.MIGRATION_COLLECTION_VERSION_ATTRIBUTE, version)
.append("date", new Date());

dbObject.append("date", new Date());
MongoCollection<Document> db = database()
.getCollection(config.getMigrationCollectionName());

config.getMongo()
.getDB(config.getDatabaseName())
.getCollection(config.getMigrationCollectionName())
.insert(dbObject);
db.insertOne(doc);
}

public void removeMigrationVersion(Integer version) {
DBObject dbObject =
new BasicDBObject(
Configuration.MIGRATION_COLLECTION_VERSION_ATTRIBUTE,
version);
config.getMongo()
.getDB(config.getDatabaseName())
Document doc = new Document()
.append(Configuration.MIGRATION_COLLECTION_VERSION_ATTRIBUTE, version);

database()
.getCollection(config.getMigrationCollectionName())
.remove(dbObject);
.deleteOne(doc);
}

/**
* Verify if the collection contains an index
* with the same name and different
* attributes or attributes not equally ordered
*
* @param indexAttributes
* @param indexName
* @param collection
* @return
*/
public boolean verifyIfHasSameNameAndDifferentAttributes(
Map<String, OrderingType> indexAttributes,
String indexName,
String collection) {

List<DBObject> indexInfo = config.getMongo().getDB(config.getDatabaseName()).getCollection(collection).getIndexInfo();
ListIndexesIterable<Document> indexesIterable = database().getCollection(collection).listIndexes();
List<Document> indexList = indexesIterable.into(new ArrayList<>());

for (DBObject current : indexInfo) {
for (Document current : indexList) {
String remoteIndexName = (String) current.get("name");
if (!verifyIfHasSameAttributesWithSameOrder(indexAttributes, current)
&& verifyIndexesHaveSameName(indexName, remoteIndexName)) {
Expand All @@ -218,13 +209,14 @@ protected boolean verifyIfIndexExists(
String indexName,
String collection) {

List<DBObject> indexInfo = config.getMongo().getDB(config.getDatabaseName()).getCollection(collection).getIndexInfo();
ListIndexesIterable<Document> indexesIterable = database().getCollection(collection).listIndexes();
List<Document> indexList = indexesIterable.into(new ArrayList<>());

if (indexInfo.isEmpty()) {
if (indexList.isEmpty()) {
return false;
}

for (DBObject current : indexInfo) {
for (Document current : indexList) {

if (verifyIfHasSameAttributesWithSameOrder(indexAttributes, current)) {
return true;
Expand All @@ -248,7 +240,7 @@ private boolean verifyIndexesHaveSameName(String indexName, String currentIndexN
return false;
}

private boolean verifyIfHasSameAttributesWithSameOrder(Map<String, OrderingType> indexAttributes, DBObject current) {
private boolean verifyIfHasSameAttributesWithSameOrder(Map<String, OrderingType> indexAttributes, Document current) {
if (indexAttributes != null) {
Map<String, OrderingType> existingAttributes = identifyIndexAttributesAndOrdering(current);

Expand All @@ -268,9 +260,9 @@ private boolean verifyIfHasSameAttributesWithSameOrder(Map<String, OrderingType>
* @param current The DBObject retrieved from the IndexInfo List.
* @return A map containing the attributes and ordering
*/
private Map<String, OrderingType> identifyIndexAttributesAndOrdering(DBObject current) {
private Map<String, OrderingType> identifyIndexAttributesAndOrdering(Document current) {
Map<String, OrderingType> existingAttributes = new LinkedHashMap<>();
DBObject indexAttrs = (DBObject) current.get("key");
Document indexAttrs = (Document) current.get("key");

if (indexAttrs != null) {
Set<String> attrKeySet = indexAttrs.keySet();
Expand Down Expand Up @@ -302,35 +294,26 @@ public void dropIndexByName(String collection, String indexName) {
return;
}

config.getMongo().getDB(config.getDatabaseName()).getCollection(collection).dropIndex(indexName);
database().getCollection(collection).dropIndex(indexName);
}

public void createIndex(String collection, BasicDBObject indexDefinition, BasicDBObject indexOptions) {
public void createIndex(String collection, Bson indexDefinition, IndexOptions indexOptions) {
LOG.info("Creating index... ");
config.getMongo().getDB(config.getDatabaseName()).getCollection(collection).createIndex(indexDefinition, indexOptions);
database().getCollection(collection).createIndex(indexDefinition, indexOptions);
}

public boolean verifyIfMigrateWasAlreadyExecuted(Integer version) {

DBCollection versionCollection = config.getMongo()
.getDB(config.getDatabaseName())
.getCollection(config.getMigrationCollectionName());
MongoCollection<Document> versionCollection = database().getCollection(config.getMigrationCollectionName());

versionCollection.setReadPreference(ReadPreference.primary());

BasicDBObject dbObject = new BasicDBObject(Configuration.MIGRATION_COLLECTION_VERSION_ATTRIBUTE, version);

long count = versionCollection.count(dbObject);
Document doc = new Document(Configuration.MIGRATION_COLLECTION_VERSION_ATTRIBUTE, version);
long count = versionCollection.countDocuments(doc);

return count > 0;
}

public DB getMongoDatabase() {
return config.getMongo().getDB(config.getDatabaseName());
}

public boolean verifyIfCollectionExists(String collectionName) {
return getMongoDatabase().collectionExists(collectionName);
public MongoDatabase getMongoDatabase() {
return database();
}

public void removeCollection(String collectionName) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ox/engine/internal/OxEnvironment.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ox.engine.internal;

import com.mongodb.DB;
import com.mongodb.client.MongoDatabase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -37,7 +37,7 @@ public void execute(OxAction oxAction) {
*
* @return the connected database
*/
public DB getMongoDatabase() {
public MongoDatabase getMongoDatabase() {
return mongoConnector.getMongoDatabase();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected void validateAction() throws InvalidMigrateActionException {

@Override
void runAction(MongoDBConnector mongoDBConnector, MongoClient mongo, String databaseName) {
if (mongoDBConnector.verifyIfCollectionExists(collection)) {
if (mongoDBConnector.collectionExists(collection)) {
mongoDBConnector.removeCollection(collection);
}
}
Expand Down
27 changes: 5 additions & 22 deletions src/main/java/ox/engine/internal/RemoveIndexAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,11 @@

private static final Logger LOG = LoggerFactory.getLogger(RemoveIndexAction.class);
private final String indexName;
private boolean ifExists;

public RemoveIndexAction(String indexName) {
this.indexName = indexName;
}

/**
* Removes the index only if it exists.
* If false (default value), should throw an error if the index do not exists.
*
* @return
*/
public RemoveIndexAction ifExists() {
ifExists = true;
return this;
}

/**
* Set the collection that the index will be removed.
*
Expand Down Expand Up @@ -56,17 +44,12 @@

boolean doesItExists = mongoDBConnector.verifyIfIndexExists(null, indexName, collection);

if (ifExists) {
if (doesItExists) {
LOG.info("[Ox] Index exists! Removing... Index name: " + indexName);
mongo.getDB(databaseName).getCollection(collection).dropIndex(indexName);
} else {
LOG.warn("[Ox] Ignoring Index Removal Action " +
"because no index was found with name: " + indexName);
}
} else {
LOG.info("[Ox] Removing index... (Existing or not!). Index name: " + indexName);
if (doesItExists) {
LOG.info("[Ox] Index exists! Removing... Index name: " + indexName);
mongo.getDB(databaseName).getCollection(collection).dropIndex(indexName);
} else {
LOG.warn("[Ox] Ignoring Index Removal Action " +

Check warning on line 51 in src/main/java/ox/engine/internal/RemoveIndexAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ox/engine/internal/RemoveIndexAction.java#L51

Added line #L51 was not covered by tests
"because no index was found with name: " + indexName);
}

}
Expand Down
61 changes: 1 addition & 60 deletions src/test/java/ox/engine/OxMigrationTest.java
Original file line number Diff line number Diff line change
@@ -1,76 +1,17 @@
package ox.engine;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import ox.engine.exception.InvalidMongoConfiguration;
import ox.engine.exception.InvalidMongoDatabaseConfiguration;
import ox.engine.internal.MongoDBConnector;

@RunWith(MockitoJUnitRunner.class)
public class OxMigrationTest {

@Mock
private MongoDBConnector mongoConnector;

@Mock
private MongoClient mongo;

@Mock
private DB db;

@Mock
private DBCollection coll;

@Test
public void runMigrationsTest() throws InvalidMongoConfiguration {
Mockito.when(mongo.getDB(Mockito.anyString())).thenReturn(db);

Mockito.when(db.getCollection(Mockito.anyString())).thenReturn(coll);

OxConfig config = OxConfig.builder()
.mongo(mongo)
.databaseName("myDB")
.scanPackage("ox.db.migrations")
.dryRun()
.build();

Ox engine = Ox.setUp(config);
engine.up();
}

@Test
public void runDownMigrationTest() throws InvalidMongoConfiguration {

Mockito.when(mongo.getDB(Mockito.anyString())).thenReturn(db);
Mockito.when(db.getCollection(Mockito.anyString())).thenReturn(coll);
Mockito.when(coll.count(Mockito.any(DBObject.class))).thenReturn(1L);

OxConfig config = OxConfig.builder()
.mongo(mongo)
.databaseName("myDB")
.scanPackage("ox.db.migrations")
.dryRun()
.build();

Ox
.setUp(config)
.down();
}

@Test(expected = InvalidMongoDatabaseConfiguration.class)
public void validateInvalidMongoInstance() throws InvalidMongoConfiguration {
Ox.setUp(
null,
"ox.db.migrations",
"myDB")
.up();
Ox.setUp(null, "ox.db.migrations", "myDB").up();
}

}
Loading
Loading