Skip to content

Commit

Permalink
merged 4.0beta into main
Browse files Browse the repository at this point in the history
  • Loading branch information
molivasdat committed Jul 16, 2020
2 parents 9e4ed8c + cb76916 commit a53e6c7
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 47 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-mongodb</artifactId>
<packaging>jar</packaging>
<version>3.10.1-SNAPSHOT</version>
<version>4.0.0-SNAPSHOT</version>
<name>Liquibase MongoDB Extension</name>
<description>Liquibase extension for MongoDB</description>
<url>http://www.liquibase.org</url>
Expand Down Expand Up @@ -37,7 +37,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.surefire.plugin.version>2.22.2</maven.surefire.plugin.version>
<maven-failsafe-plugin.version>2.22.2</maven-failsafe-plugin.version>
<liquibase.version>3.10.1</liquibase.version>
<liquibase.version>4.0.0</liquibase.version>
<jupiter.version>5.6.2</jupiter.version>
<jupiter.surefire.version>1.3.2</jupiter.surefire.version>
<mongodb-driver.version>3.12.6</mongodb-driver.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.bson.Document;

import static java.util.Objects.nonNull;
import static liquibase.util.StringUtils.isNotEmpty;
import static liquibase.util.StringUtil.isNotEmpty;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ChangeSetUtils {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import liquibase.Scope;
import liquibase.changelog.AbstractChangeLogHistoryService;
import liquibase.changelog.ChangeSet;
import liquibase.changelog.RanChangeSet;
Expand All @@ -37,7 +38,6 @@
import liquibase.ext.mongodb.statement.CountCollectionByNameStatement;
import liquibase.ext.mongodb.statement.DropCollectionStatement;
import liquibase.logging.LogService;
import liquibase.logging.LogType;
import org.bson.Document;
import org.bson.conversions.Bson;

Expand Down Expand Up @@ -99,7 +99,7 @@ public void reset() {
public boolean hasDatabaseChangeLogTable() {
if (hasDatabaseChangeLogTable == null) {
try {
final Executor executor = ExecutorService.getInstance().getExecutor(getDatabase());
final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", getDatabase());
hasDatabaseChangeLogTable =
executor.queryForLong(new CountCollectionByNameStatement(getDatabase().getDatabaseChangeLogTableName())) == 1L;
} catch (LiquibaseException e) {
Expand All @@ -114,7 +114,7 @@ public void init() throws DatabaseException {
return;
}

final Executor executor = ExecutorService.getInstance().getExecutor(getDatabase());
final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", getDatabase());

final boolean createdTable = hasDatabaseChangeLogTable();

Expand All @@ -127,12 +127,12 @@ public void init() throws DatabaseException {
new CreateChangeLogCollectionStatement(getDatabase().getDatabaseChangeLogTableName());

// If there is no table in the database for recording change history create one.
LogService.getLog(getClass()).info(LogType.LOG, "Creating database history collection with name: " +
Scope.getCurrentScope().getLog(getClass()).info("Creating database history collection with name: " +
getDatabase().getLiquibaseCatalogName() + "." + getDatabase().getDatabaseChangeLogTableName());

executor.execute(createChangeLogCollectionStatement);

LogService.getLog(getClass()).info(LogType.LOG, "Created database history collection : " +
Scope.getCurrentScope().getLog(getClass()).info("Created database history collection : " +
createChangeLogCollectionStatement.toJs());
}

Expand Down Expand Up @@ -168,7 +168,7 @@ protected void replaceChecksum(final ChangeSet changeSet) throws DatabaseExcepti
((MongoLiquibaseDatabase) getDatabase()).getConnection().getDb().getCollection(getDatabaseChangeLogTableName())
.updateOne(filter, update);

ExecutorService.getInstance().getExecutor(getDatabase())
Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", getDatabase())
.comment(String.format("Replace checksum executed. Changeset: [filename: %s, id: %s, author: %s]",
changeSet.getFilePath(), changeSet.getId(), changeSet.getAuthor()));

Expand Down Expand Up @@ -278,14 +278,14 @@ public void clearAllCheckSums() throws DatabaseException {
((MongoLiquibaseDatabase) getDatabase()).getConnection().getDb().getCollection(getDatabaseChangeLogTableName())
.updateMany(CLEAR_CHECKSUM_FILTER, CLEAR_CHECKSUM_UPDATE);

ExecutorService.getInstance().getExecutor(getDatabase()).comment("Clear all checksums executed");
Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", getDatabase()).comment("Clear all checksums executed");
}

@Override
public void destroy() {

try {
final Executor executor = ExecutorService.getInstance().getExecutor(getDatabase());
final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", getDatabase());

executor.comment("Dropping Collection Database Change Log: " + getDatabaseChangeLogTableName());
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* #L%
*/

import liquibase.Scope;
import liquibase.command.CommandResult;
import liquibase.command.core.DropAllCommand;
import liquibase.database.Database;
Expand Down Expand Up @@ -49,7 +50,7 @@ protected CommandResult run() throws Exception {
LockService lockService = LockServiceFactory.getInstance().getLockService(database);
try {
lockService.waitForLock();
final Executor executor = ExecutorService.getInstance().getExecutor(database);
final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
executor.execute(new DropAllCollectionsStatement());

} catch (DatabaseException e) {
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/liquibase/ext/mongodb/executor/MongoExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Projections;
import liquibase.Scope;
import liquibase.change.ColumnConfig;
import liquibase.changelog.ChangeLogHistoryServiceFactory;
import liquibase.database.Database;
Expand All @@ -31,8 +32,6 @@
import liquibase.ext.mongodb.database.MongoConnection;
import liquibase.ext.mongodb.statement.AbstractMongoDocumentStatement;
import liquibase.ext.mongodb.statement.AbstractMongoStatement;
import liquibase.logging.LogService;
import liquibase.logging.LogType;
import liquibase.servicelocator.LiquibaseService;
import liquibase.sql.visitor.SqlVisitor;
import liquibase.statement.SqlStatement;
Expand All @@ -51,6 +50,7 @@

import static java.util.Collections.emptyList;
import static liquibase.ext.mongodb.database.MongoLiquibaseDatabase.DATABASE_CHANGE_LOG_LOCK_TABLE_NAME;
import static liquibase.servicelocator.PrioritizedService.PRIORITY_DATABASE;

@NoArgsConstructor
@LiquibaseService
Expand Down Expand Up @@ -152,7 +152,7 @@ public void execute(final SqlStatement sql, final List<SqlVisitor> sqlVisitors)
try {
ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(this.database).clearAllCheckSums();
} catch (LiquibaseException e) {
LogService.getLog(getClass()).severe("Execution exception ", e);
Scope.getCurrentScope().getLog(getClass()).severe("Execution exception ", e);
}
} else {
throw new IllegalArgumentException();
Expand All @@ -175,7 +175,7 @@ public int update(final SqlStatement sql, final List<SqlVisitor> sqlVisitors) {

@Override
public void comment(final String message) {
LogService.getLog(getClass()).debug(message);
Scope.getCurrentScope().getLog(getClass()).fine(message);
}

@Override
Expand All @@ -194,13 +194,11 @@ private static boolean isClearChecksumStatement(final SqlStatement sqlStatement)

@Override
public String getName() {
// TODO Auto-generated method stub
return null;
return "mongodb";
}

@Override
public int getPriority() {
// TODO Auto-generated method stub
return 0;
return PRIORITY_DATABASE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* #L%
*/

import liquibase.Scope;
import liquibase.configuration.GlobalConfiguration;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.database.Database;
Expand All @@ -34,8 +35,6 @@
import liquibase.ext.mongodb.statement.FindAllStatement;
import liquibase.lockservice.DatabaseChangeLogLock;
import liquibase.lockservice.LockService;
import liquibase.logging.LogService;
import liquibase.logging.LogType;
import liquibase.statement.SqlStatement;
import lombok.Getter;
import org.bson.Document;
Expand Down Expand Up @@ -84,7 +83,7 @@ public void init() throws DatabaseException {

if (!hasDatabaseChangeLogLockTable()) {
try {
final Executor executor = ExecutorService.getInstance().getExecutor(getDatabase());
final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", Scope.getCurrentScope().getDatabase());
executor.comment("Create Database Lock Collection");

final CreateChangelogLockCollectionStatement createChangeLogLockCollectionStatement =
Expand All @@ -94,11 +93,11 @@ public void init() throws DatabaseException {

database.commit();

LogService.getLog(getClass()).debug(LogType.LOG, "Created database lock collection: " + createChangeLogLockCollectionStatement.toJs());
Scope.getCurrentScope().getLog(getClass()).fine("Created database lock collection: " + createChangeLogLockCollectionStatement.toJs());
} catch (DatabaseException e) {
if ((e.getMessage() != null) && e.getMessage().contains("exists")) {
//hit a race condition where the table got created by another node.
LogService.getLog(getClass()).debug(LogType.LOG, "Database lock collection already appears to exist " +
Scope.getCurrentScope().getLog(getClass()).fine("Database lock collection already appears to exist " +
"due to exception: " + e.getMessage() + ". Continuing on");
} else {
throw e;
Expand All @@ -121,7 +120,7 @@ public void waitForLock() throws LockException {
while (!locked && (new Date().getTime() < timeToGiveUp)) {
locked = acquireLock();
if (!locked) {
LogService.getLog(getClass()).info(LogType.LOG, "Waiting for changelog lock....");
Scope.getCurrentScope().getLog(getClass()).info("Waiting for changelog lock....");
try {
Thread.sleep(getChangeLogLockRecheckTime() * 1000);
} catch (InterruptedException e) {
Expand Down Expand Up @@ -150,7 +149,7 @@ public void waitForLock() throws LockException {
public boolean acquireLock() throws LockException {

try {
final Executor executor = ExecutorService.getInstance().getExecutor(getDatabase());
final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", Scope.getCurrentScope().getDatabase());
database.rollback();
this.init();

Expand All @@ -174,7 +173,7 @@ public boolean acquireLock() throws LockException {
}

database.commit();
LogService.getLog(getClass()).info(LogType.LOG, coreBundle.getString("successfully.acquired.change.log.lock"));
Scope.getCurrentScope().getLog(getClass()).info(coreBundle.getString("successfully.acquired.change.log.lock"));

this.hasChangeLogLock = true;
this.database.setCanCacheLiquibaseTableInfo(true);
Expand All @@ -187,7 +186,7 @@ public boolean acquireLock() throws LockException {
try {
database.rollback();
} catch (DatabaseException e) {
LogService.getLog(getClass()).severe(LogType.LOG, "Error on acquire change log lock Rollback.", e);
Scope.getCurrentScope().getLog(getClass()).severe("Error on acquire change log lock Rollback.", e);
}
}
}
Expand All @@ -198,7 +197,7 @@ public void releaseLock() throws LockException {
try {
if (this.hasDatabaseChangeLogLockTable()) {

final Executor executor = ExecutorService.getInstance().getExecutor(getDatabase());
final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", getDatabase());
executor.comment("Release Database Lock");

database.rollback();
Expand All @@ -223,10 +222,10 @@ public void releaseLock() throws LockException {
} finally {
try {
database.setCanCacheLiquibaseTableInfo(false);
LogService.getLog(getClass()).info(LogType.LOG, "Successfully released change log lock");
Scope.getCurrentScope().getLog(getClass()).info("Successfully released change log lock");
database.rollback();
} catch (DatabaseException e) {
LogService.getLog(getClass()).severe(LogType.LOG, "Error on released change log lock Rollback.", e);
Scope.getCurrentScope().getLog(getClass()).severe("Error on released change log lock Rollback.", e);
}
}
}
Expand All @@ -242,7 +241,7 @@ public DatabaseChangeLogLock[] listLocks() throws LockException {
getDatabaseChangeLogLockTableName()
);

final Executor executor = ExecutorService.getInstance().getExecutor(getDatabase());
final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", getDatabase());
@SuppressWarnings("unchecked")
List<MongoChangeLogLock> rows = (List<MongoChangeLogLock>) executor.queryForList(stmt, Document.class).stream()
.map(d -> MongoChangeLogLock.from((Document)d)).collect(Collectors.toList());
Expand All @@ -269,7 +268,7 @@ public void reset() {
@Override
public void destroy() {
try {
final Executor executor = ExecutorService.getInstance().getExecutor(getDatabase());
final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", getDatabase());

executor.comment("Dropping Collection Database Change Log Lock: " + getDatabaseChangeLogLockTableName());
{
Expand Down Expand Up @@ -321,7 +320,7 @@ public void setChangeLogLockWaitTime(long changeLogLockWaitTime) {
private boolean hasDatabaseChangeLogLockTable() throws DatabaseException {
if (hasDatabaseChangeLogLockTable == null) {
try {
final Executor executor = ExecutorService.getInstance().getExecutor(getDatabase());
final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", getDatabase());
hasDatabaseChangeLogLockTable =
executor.queryForLong(new CountDocumentsInCollectionStatement(getDatabase().getDatabaseChangeLogLockTableName())) == 1L;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.util.List;

import static java.util.Optional.ofNullable;
import static liquibase.util.StringUtils.trimToNull;
import static liquibase.util.StringUtil.trimToNull;
import static lombok.AccessLevel.PRIVATE;
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;

Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/META-INF/services/liquibase.change.Change
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
liquibase.ext.mongodb.change.AdminCommandChange
liquibase.ext.mongodb.change.CreateCollectionChange
liquibase.ext.mongodb.change.CreateIndexChange
liquibase.ext.mongodb.change.InsertManyChange
liquibase.ext.mongodb.change.InsertOneChange
liquibase.ext.mongodb.change.RunCommandChange
10 changes: 5 additions & 5 deletions src/test/java/liquibase/ext/AbstractMongoIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@
* #L%
*/

import liquibase.Scope;
import liquibase.database.DatabaseFactory;
import liquibase.exception.DatabaseException;
import liquibase.executor.ExecutorService;
import liquibase.ext.mongodb.TestUtils;
import liquibase.ext.mongodb.database.MongoConnection;
import liquibase.ext.mongodb.database.MongoLiquibaseDatabase;
import liquibase.ext.mongodb.executor.MongoExecutor;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;

import java.util.function.Consumer;

import static liquibase.ext.mongodb.TestUtils.getMongoConnection;

@Slf4j
public abstract class AbstractMongoIntegrationTest {

protected static MongoConnection mongoConnection;
Expand All @@ -58,13 +58,13 @@ protected void setUp() throws DatabaseException {

database = (MongoLiquibaseDatabase) DatabaseFactory.getInstance().findCorrectDatabaseImplementation(mongoConnection);
database.setConnection(mongoConnection);
log.debug("database is initialized...");
Scope.getCurrentScope().getLog(TestUtils.class).fine("database is initialized...");

mongoExecutor = new MongoExecutor();
mongoExecutor.setDatabase(database);
log.debug("mongoExecutor is initialized...");
Scope.getCurrentScope().getLog(TestUtils.class).fine("mongoExecutor is initialized...");

ExecutorService.getInstance().setExecutor(database, mongoExecutor);
Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor(database, mongoExecutor);

database.getConnection().getDb().listCollectionNames()
.forEach((Consumer<? super String>) c -> mongoConnection.getDb().getCollection(c).drop());
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/liquibase/ext/mongodb/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

import liquibase.Liquibase;
import liquibase.Scope;
import liquibase.changelog.ChangeLogParameters;
import liquibase.changelog.ChangeSet;
import liquibase.changelog.DatabaseChangeLog;
Expand All @@ -33,7 +34,6 @@
import liquibase.util.file.FilenameUtils;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;

import java.util.List;
Expand All @@ -44,7 +44,6 @@
import static lombok.AccessLevel.PRIVATE;
import static org.assertj.core.api.Assertions.assertThat;

@Slf4j
@NoArgsConstructor(access = PRIVATE)
public final class TestUtils {
public static final String BUILD_INFO_1 = "{ buildInfo: 1 }";
Expand All @@ -66,7 +65,6 @@ public static List<String> getCollections(final MongoConnection connection) {
public static MongoConnection getMongoConnection(final String propertyFile) {
final Properties properties = new Properties();
properties.load(TestUtils.class.getClassLoader().getResourceAsStream(propertyFile));
log.debug("Actual connection properties:\n{}", properties);

return getMongoConnection(properties);
}
Expand Down
Loading

0 comments on commit a53e6c7

Please sign in to comment.