Skip to content

Commit

Permalink
feat!: version 2 overhaul (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandonogueira authored Mar 4, 2024
1 parent 4fd1c00 commit 725f52c
Show file tree
Hide file tree
Showing 22 changed files with 583 additions and 374 deletions.
54 changes: 20 additions & 34 deletions src/main/java/ox/engine/Ox.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,43 +37,34 @@
public final class Ox {

private static final Logger LOG = LoggerFactory.getLogger(Ox.class);

private boolean simulate = false;
private final MongoClient mongo;
private final String scanPackage;
private final OxConfig config;
private final MongoDBConnector mongoConnector;

private Ox(MongoClient mongo,
String scanPackage,
String databaseName,
boolean createVersioningCollectionIfMissing) {

this.mongo = mongo;
this.scanPackage = scanPackage;
this.mongoConnector =
new MongoDBConnector(MongoDBConnectorConfig
.create()
.setMongoClient(mongo)
.setDatabaseName(databaseName)
.createCollectionIfDontExists(createVersioningCollectionIfMissing));

private Ox(OxConfig config) {
this.config = config;
this.mongoConnector = new MongoDBConnector(
MongoDBConnectorConfig.fromOxConfig(config)
);
}

public static Ox setUp(
MongoClient mongo,
String scanPackage,
String databaseName,
boolean createVersioningCollectionIfMissing) {

return new Ox(mongo, scanPackage, databaseName, createVersioningCollectionIfMissing);
OxConfig config
) {
return new Ox(config);
}

public static Ox setUp(
MongoClient mongo,
String scanPackage,
String databaseName) {

return new Ox(mongo, scanPackage, databaseName, true);
OxConfig oxConfig = new OxConfigBuilder()
.mongo(mongo)
.scanPackage(scanPackage)
.databaseName(databaseName)
.build();

return new Ox(oxConfig);
}

/**
Expand Down Expand Up @@ -111,7 +102,7 @@ public void down(int version) throws InvalidMongoConfiguration {
}

private void validateExecution() throws InvalidMongoConfiguration {
if (!simulate && mongo == null) {
if (!config.dryRun() && config.mongo() == null) {
throw new InvalidMongoConfiguration("Invalid Mongo Configuration. Please fix it and try again.");
}
}
Expand Down Expand Up @@ -156,15 +147,15 @@ private void executeEachMigrate(List<ResolvedMigration> migrations,
ExecutionMode mode,
Integer desiredVersion) {
Integer currentVersion;
if (!simulate) {
if (!config.dryRun()) {
currentVersion = mongoConnector.retrieveDatabaseCurrentVersion();
} else {
currentVersion = 0;
}

try {
OxEnvironment env = new OxEnvironment();
env.setSimulate(simulate);
env.dryRun(config.dryRun());
env.setMongoConnector(mongoConnector);

LOG.info("[Ox] MongoDB Database Current Version: " + currentVersion);
Expand Down Expand Up @@ -227,7 +218,7 @@ private void runMigrationUpIfApplies(Integer desiredVersion, OxEnvironment env,

public List<ResolvedMigration> getMigrationsList() {
try {
List<ResolvedMigration> resolvedMigrations = resolveMigrations(scanPackage);
List<ResolvedMigration> resolvedMigrations = resolveMigrations(config.scanPackage());
return CollectionUtils.sortResolvedMigrations(resolvedMigrations);
} catch (IOException e) {
LOG.error("[Ox] Error updating MONGODB Database Schema", e);
Expand All @@ -246,11 +237,6 @@ public Integer databaseVersion() throws InvalidMongoConfiguration {
return mongoConnector.retrieveDatabaseCurrentVersion();
}

public Ox simulate() {
simulate = true;
return this;
}

private enum ExecutionMode {
UP, DOWN
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/ox/engine/OxConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ox.engine;

import com.mongodb.MongoClient;

public record OxConfig(
MongoClient mongo,
String databaseName,
String scanPackage,
boolean createMigrationCollection,
String migrationCollectionName,
boolean failOnMissingCollection,
boolean dryRun
) {
public static OxConfigBuilder builder() {
return new OxConfigBuilder();
}
}

77 changes: 77 additions & 0 deletions src/main/java/ox/engine/OxConfigBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package ox.engine;

import com.mongodb.MongoClient;
import ox.Configuration;

public class OxConfigBuilder {
private boolean createMigrationCollection = true;
private String migrationCollectionName = Configuration.SCHEMA_VERSION_COLLECTION_NAME;
private boolean failOnMissingCollection = false;
private MongoClient mongo;
private String databaseName;
private String scanPackage;
private boolean dryRun = false;

/**
* By default, Ox will create the migration collection if it does not exists.
* <p/>
* If you want to disable this behavior, call this method
*/
public OxConfigBuilder disableMigrationCollectionCreation() {
this.createMigrationCollection = false;
return this;
}

public OxConfigBuilder migrationCollectionName(String migrationCollectionName) {
this.migrationCollectionName = migrationCollectionName;
return this;
}

/**
* if true, Ox will throw an exception if the collections targeted by the OxActions or Migrations do not exist
* <p/>
* Defaults to false
* <p/>
* Attention: This is a global setting, it will affect all OxActions and Migrations.
* If false, Ox will ignore the missing collection and continue the migration process.
*/
public OxConfigBuilder failOnMissingCollection(boolean failOnMissingCollection) {
this.failOnMissingCollection = failOnMissingCollection;
return this;
}

public OxConfigBuilder mongo(MongoClient mongo) {
this.mongo = mongo;
return this;
}

public OxConfigBuilder databaseName(String databaseName) {
this.databaseName = databaseName;
return this;
}

public OxConfigBuilder scanPackage(String scanPackage) {
this.scanPackage = scanPackage;
return this;
}

/**
* Enables dry run mode. Ox will only simulate the migration process, it will not execute the migrations
*/
public OxConfigBuilder dryRun() {
this.dryRun = true;
return this;
}

public OxConfig build() {
return new OxConfig(
mongo,
databaseName,
scanPackage,
createMigrationCollection,
migrationCollectionName,
failOnMissingCollection,
dryRun
);
}
}
11 changes: 11 additions & 0 deletions src/main/java/ox/engine/exception/DatabaseNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ox.engine.exception;

public class DatabaseNotFoundException extends OxRuntimeException {
public DatabaseNotFoundException() {
super("database.notfound.exception");
}

public DatabaseNotFoundException(String message) {
super(message);
}
}
11 changes: 11 additions & 0 deletions src/main/java/ox/engine/exception/MissingCollectionException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ox.engine.exception;

public class MissingCollectionException extends OxRuntimeException {
public MissingCollectionException() {
super("collection.not.found");
}

public MissingCollectionException(String msg) {
super(msg);
}
}
2 changes: 0 additions & 2 deletions src/main/java/ox/engine/internal/CreateIndexAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
public class CreateIndexAction extends OxAction {

private static final Logger LOG = LoggerFactory.getLogger(CreateIndexAction.class);

private final Map<String, OrderingType> attributes = new LinkedHashMap<>();

private final String indexName;
Expand Down Expand Up @@ -151,7 +150,6 @@ protected void validateAction() throws InvalidMigrateActionException {
if (attributes.size() > 1 && ttlIndex) {
throw new InvalidMigrateActionException("TTL indexes can not have more than 1 attribute");
}

}

@Override
Expand Down
Loading

0 comments on commit 725f52c

Please sign in to comment.