From 95344da195e167ff5bb7e84698b5509d7416b95a Mon Sep 17 00:00:00 2001 From: Alexandru Slobodcicov Date: Sun, 7 Feb 2021 11:25:22 +0200 Subject: [PATCH] Issue 67: Supports validator read directly from database --- .../AdjustChangeLogCollectionStatement.java | 11 +---------- .../mongodb/changelog/MongoHistoryService.java | 7 +++---- .../AdjustChangeLogLockCollectionStatement.java | 11 +---------- .../mongodb/lockservice/MongoLockService.java | 7 +++---- .../changelog/AbstractNoSqlHistoryService.java | 12 +++++++++--- .../lockservice/AbstractNoSqlLockService.java | 9 +++++---- .../AdjustChangeLogCollectionStatementIT.java | 13 ++++++++----- .../changelog/MongoHistoryServiceTest.java | 2 -- ...AdjustChangeLogLockCollectionStatementIT.java | 16 ++++++++++------ .../lockservice/MongoLockServiceTest.java | 1 - 10 files changed, 40 insertions(+), 49 deletions(-) diff --git a/src/main/java/liquibase/ext/mongodb/changelog/AdjustChangeLogCollectionStatement.java b/src/main/java/liquibase/ext/mongodb/changelog/AdjustChangeLogCollectionStatement.java index 69791c67..ce0140c7 100644 --- a/src/main/java/liquibase/ext/mongodb/changelog/AdjustChangeLogCollectionStatement.java +++ b/src/main/java/liquibase/ext/mongodb/changelog/AdjustChangeLogCollectionStatement.java @@ -30,8 +30,6 @@ import java.util.ArrayList; import java.util.List; -import static java.lang.Boolean.TRUE; - public class AdjustChangeLogCollectionStatement extends RunCommandStatement { public static final String UI = "ui_"; @@ -39,17 +37,10 @@ public class AdjustChangeLogCollectionStatement extends RunCommandStatement { @Getter private final String collectionName; - @Getter - private final Boolean supportsValidator; public AdjustChangeLogCollectionStatement(final String collectionName) { - this(collectionName, TRUE); - } - - public AdjustChangeLogCollectionStatement(final String collectionName, Boolean supportsValidator) { super(String.format(OPTIONS, collectionName)); this.collectionName = collectionName; - this.supportsValidator = supportsValidator; } @Override @@ -62,7 +53,7 @@ public void execute(final MongoLiquibaseDatabase database) { adjustIndexes(database); - if (TRUE.equals(supportsValidator)) { + if (database.getSupportsValidator()) { super.execute(database); } } diff --git a/src/main/java/liquibase/ext/mongodb/changelog/MongoHistoryService.java b/src/main/java/liquibase/ext/mongodb/changelog/MongoHistoryService.java index 578adf49..68e11b05 100644 --- a/src/main/java/liquibase/ext/mongodb/changelog/MongoHistoryService.java +++ b/src/main/java/liquibase/ext/mongodb/changelog/MongoHistoryService.java @@ -55,7 +55,7 @@ import static java.util.Objects.nonNull; import static liquibase.plugin.Plugin.PRIORITY_SPECIALIZED; -public class MongoHistoryService extends AbstractNoSqlHistoryService { +public class MongoHistoryService extends AbstractNoSqlHistoryService { private final Logger log = Scope.getCurrentScope().getLog(getClass()); @@ -97,15 +97,14 @@ protected void createRepository() throws DatabaseException { @Override protected void adjustRepository() throws DatabaseException { - if (((MongoLiquibaseDatabase) getDatabase()).getAdjustTrackingTablesOnStartup()) { + if (getNoSqlDatabase().getAdjustTrackingTablesOnStartup()) { this.getLogger().info("Adjusting database history Collection with name: " + getDatabase().getConnection().getCatalog() + "." + getDatabaseChangeLogTableName()); this.getLogger().info("Adjusted database history Collection with name: " + getDatabase().getConnection().getCatalog() + "." + getDatabaseChangeLogTableName()); - getExecutor().execute(new AdjustChangeLogCollectionStatement(getDatabaseChangeLogTableName(), - ((MongoLiquibaseDatabase) getDatabase()).getSupportsValidator())); + getExecutor().execute(new AdjustChangeLogCollectionStatement(getDatabaseChangeLogTableName())); } else { this.getLogger().info("Skipped Adjusting database history Collection with name: " diff --git a/src/main/java/liquibase/ext/mongodb/lockservice/AdjustChangeLogLockCollectionStatement.java b/src/main/java/liquibase/ext/mongodb/lockservice/AdjustChangeLogLockCollectionStatement.java index 6d3e2521..1840be5a 100644 --- a/src/main/java/liquibase/ext/mongodb/lockservice/AdjustChangeLogLockCollectionStatement.java +++ b/src/main/java/liquibase/ext/mongodb/lockservice/AdjustChangeLogLockCollectionStatement.java @@ -25,7 +25,6 @@ import lombok.Getter; import org.bson.Document; -import static java.lang.Boolean.TRUE; import static java.util.Optional.ofNullable; public class AdjustChangeLogLockCollectionStatement extends RunCommandStatement { @@ -37,17 +36,9 @@ public class AdjustChangeLogLockCollectionStatement extends RunCommandStatement @Getter private final String collectionName; - @Getter - private final Boolean supportsValidator; - public AdjustChangeLogLockCollectionStatement(final String collectionName) { - this(collectionName, TRUE); - } - - public AdjustChangeLogLockCollectionStatement(final String collectionName, Boolean supportsValidator) { super(String.format(OPTIONS, collectionName)); this.collectionName = collectionName; - this.supportsValidator = supportsValidator; } @Override @@ -57,7 +48,7 @@ public String getCommandName() { @Override public void execute(final MongoLiquibaseDatabase database) { - if(TRUE.equals(supportsValidator)) { + if(database.getSupportsValidator()) { super.execute(database); } } diff --git a/src/main/java/liquibase/ext/mongodb/lockservice/MongoLockService.java b/src/main/java/liquibase/ext/mongodb/lockservice/MongoLockService.java index b43af9b1..f61b399c 100644 --- a/src/main/java/liquibase/ext/mongodb/lockservice/MongoLockService.java +++ b/src/main/java/liquibase/ext/mongodb/lockservice/MongoLockService.java @@ -40,7 +40,7 @@ import static java.lang.Boolean.FALSE; -public class MongoLockService extends AbstractNoSqlLockService { +public class MongoLockService extends AbstractNoSqlLockService { private final Logger log = Scope.getCurrentScope().getLog(getClass()); @@ -94,12 +94,11 @@ protected void createRepository() throws DatabaseException { @Override protected void adjustRepository() throws DatabaseException { - if (((MongoLiquibaseDatabase)getDatabase()).getAdjustTrackingTablesOnStartup()) { + if (getDatabase().getAdjustTrackingTablesOnStartup()) { this.getLogger().info("Adjusting database Lock Collection with name: " + getDatabase().getConnection().getCatalog() + "." + getDatabaseChangeLogLockTableName()); - getExecutor().execute(new AdjustChangeLogLockCollectionStatement(getDatabaseChangeLogLockTableName(), - ((MongoLiquibaseDatabase)getDatabase()).getSupportsValidator())); + getExecutor().execute(new AdjustChangeLogLockCollectionStatement(getDatabaseChangeLogLockTableName())); this.getLogger().info("Adjusted database Lock Collection with name: " + getDatabase().getConnection().getCatalog() + "." + getDatabaseChangeLogLockTableName()); diff --git a/src/main/java/liquibase/nosql/changelog/AbstractNoSqlHistoryService.java b/src/main/java/liquibase/nosql/changelog/AbstractNoSqlHistoryService.java index a05b3f43..631d6d00 100644 --- a/src/main/java/liquibase/nosql/changelog/AbstractNoSqlHistoryService.java +++ b/src/main/java/liquibase/nosql/changelog/AbstractNoSqlHistoryService.java @@ -20,7 +20,6 @@ * #L% */ -import liquibase.Liquibase; import liquibase.Scope; import liquibase.changelog.AbstractChangeLogHistoryService; import liquibase.changelog.ChangeSet; @@ -29,6 +28,7 @@ import liquibase.exception.DatabaseHistoryException; import liquibase.exception.UnexpectedLiquibaseException; import liquibase.executor.ExecutorService; +import liquibase.ext.mongodb.database.MongoLiquibaseDatabase; import liquibase.logging.Logger; import liquibase.nosql.executor.NoSqlExecutor; import lombok.Getter; @@ -44,7 +44,7 @@ import static java.util.Objects.isNull; import static liquibase.plugin.Plugin.PRIORITY_SPECIALIZED; -public abstract class AbstractNoSqlHistoryService extends AbstractChangeLogHistoryService { +public abstract class AbstractNoSqlHistoryService extends AbstractChangeLogHistoryService { @Getter private List ranChangeSetList; @@ -83,6 +83,11 @@ public boolean isServiceInitialized() { return serviceInitialized; } + @SuppressWarnings("unchecked") + public D getNoSqlDatabase() { + return (D) getDatabase(); + } + public NoSqlExecutor getExecutor() { return (NoSqlExecutor) Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor(NoSqlExecutor.EXECUTOR_NAME, getDatabase()); } @@ -232,6 +237,7 @@ public boolean tagExists(final String tag) throws DatabaseException { /** * TODO: Raise with Liquibase why is this one not used instead of {@link liquibase.statement.core.UpdateStatement} * in {@link liquibase.Liquibase#clearCheckSums()} + * * @throws DatabaseException in case of a failure */ @Override @@ -262,7 +268,7 @@ public void destroy() { } protected abstract Logger getLogger(); - + protected abstract Boolean existsRepository() throws DatabaseException; protected abstract void createRepository() throws DatabaseException; diff --git a/src/main/java/liquibase/nosql/lockservice/AbstractNoSqlLockService.java b/src/main/java/liquibase/nosql/lockservice/AbstractNoSqlLockService.java index 1d4234fd..18f20231 100644 --- a/src/main/java/liquibase/nosql/lockservice/AbstractNoSqlLockService.java +++ b/src/main/java/liquibase/nosql/lockservice/AbstractNoSqlLockService.java @@ -45,9 +45,9 @@ import static java.util.Objects.isNull; import static liquibase.plugin.Plugin.PRIORITY_SPECIALIZED; -public abstract class AbstractNoSqlLockService implements LockService { +public abstract class AbstractNoSqlLockService implements LockService { - private AbstractNoSqlDatabase database; + private D database; private boolean hasChangeLogLock; @@ -74,11 +74,12 @@ public int getPriority() { } @Override + @SuppressWarnings("unchecked") public void setDatabase(final Database database) { - this.database = (AbstractNoSqlDatabase) database; + this.database = (D) database; } - public Database getDatabase() { + public D getDatabase() { return database; } diff --git a/src/test/java/liquibase/ext/mongodb/changelog/AdjustChangeLogCollectionStatementIT.java b/src/test/java/liquibase/ext/mongodb/changelog/AdjustChangeLogCollectionStatementIT.java index 4b0d833b..f7879bb5 100644 --- a/src/test/java/liquibase/ext/mongodb/changelog/AdjustChangeLogCollectionStatementIT.java +++ b/src/test/java/liquibase/ext/mongodb/changelog/AdjustChangeLogCollectionStatementIT.java @@ -35,7 +35,6 @@ void executeToJSTest() { assertThat(adjustChangeLogCollectionStatement.getCommandName()).isEqualTo("runCommand"); assertThat(adjustChangeLogCollectionStatement.getCollectionName()).isEqualTo(LOG_COLLECTION_NAME); - assertThat(adjustChangeLogCollectionStatement.getSupportsValidator()).isTrue(); assertThat(adjustChangeLogCollectionStatement.toJs()).isEqualTo("db.runCommand({" + "\"collMod\": \"historyLogCollection\", \"validator\": {" + "\"$jsonSchema\": {\"bsonType\": \"object\", \"description\": \"Database Change Log Table.\", " + @@ -93,7 +92,8 @@ void executeTest() { assertThat(indexes.get(0).get("name")).isEqualTo("_id_"); // with explicit supportsValidator=false should not change validators should add indexes only - new AdjustChangeLogCollectionStatement(LOG_COLLECTION_NAME, FALSE).execute(database); + database.setSupportsValidator(FALSE); + new AdjustChangeLogCollectionStatement(LOG_COLLECTION_NAME).execute(database); final Document collectionInfoExplicitNoAdjustment = connection.getDatabase().listCollections().filter(Filters.eq("name", LOG_COLLECTION_NAME)).first(); @@ -112,7 +112,8 @@ void executeTest() { .returns(1, i -> ((Document) i.get("key")).get("id")); // with explicit supportsValidator=true validator should be changed indexes remain same - new AdjustChangeLogCollectionStatement(LOG_COLLECTION_NAME, TRUE).execute(database); + database.setSupportsValidator(TRUE); + new AdjustChangeLogCollectionStatement(LOG_COLLECTION_NAME).execute(database); final Document collectionInfoAdjusted = connection.getDatabase().listCollections().filter(Filters.eq("name", LOG_COLLECTION_NAME)).first(); @@ -138,8 +139,9 @@ void insertDataTest() { assertThat(findAllStatement.queryForList(database)).isEmpty(); // Create collection + database.setSupportsValidator(TRUE); new CreateChangeLogCollectionStatement(LOG_COLLECTION_NAME).execute(database); - new AdjustChangeLogCollectionStatement(LOG_COLLECTION_NAME, TRUE).execute(database); + new AdjustChangeLogCollectionStatement(LOG_COLLECTION_NAME).execute(database); assertThat(findAllStatement.queryForList(database)).isEmpty(); // Minimal not all required fields @@ -276,8 +278,9 @@ void insertDataNoValidatorTest() { assertThat(findAllStatement.queryForList(database)).isEmpty(); // Create collection + database.setSupportsValidator(FALSE); new CreateChangeLogCollectionStatement(LOG_COLLECTION_NAME).execute(database); - new AdjustChangeLogCollectionStatement(LOG_COLLECTION_NAME, FALSE).execute(database); + new AdjustChangeLogCollectionStatement(LOG_COLLECTION_NAME).execute(database); assertThat(findAllStatement.queryForList(database)).isEmpty(); final Document options = new Document(); diff --git a/src/test/java/liquibase/ext/mongodb/changelog/MongoHistoryServiceTest.java b/src/test/java/liquibase/ext/mongodb/changelog/MongoHistoryServiceTest.java index 5508533c..e6ea7fc9 100644 --- a/src/test/java/liquibase/ext/mongodb/changelog/MongoHistoryServiceTest.java +++ b/src/test/java/liquibase/ext/mongodb/changelog/MongoHistoryServiceTest.java @@ -152,7 +152,6 @@ void init() { verify(executorMock, times(1)).queryForLong(any()); verify(executorMock, times(1)).execute(any(CreateChangeLogCollectionStatement.class)); verify(executorMock, times(1)).execute(any(AdjustChangeLogCollectionStatement.class)); - assertThat(adjustChangeLogCollectionStatementArgumentCaptor.getValue().getSupportsValidator()).isTrue(); verifyNoMoreInteractions(executorMock); assertThat(historyService.getHasDatabaseChangeLogTable()).isTrue(); @@ -199,7 +198,6 @@ void initWhenExistsRepositoryValidatorUnsupported() { verify(executorMock, times(1)).queryForLong(any()); verify(executorMock, times(1)).execute(any(AdjustChangeLogCollectionStatement.class)); - assertThat(adjustChangeLogCollectionStatementArgumentCaptor.getValue().getSupportsValidator()).isFalse(); verifyNoMoreInteractions(executorMock); assertThat(historyService.getHasDatabaseChangeLogTable()).isTrue(); diff --git a/src/test/java/liquibase/ext/mongodb/lockservice/AdjustChangeLogLockCollectionStatementIT.java b/src/test/java/liquibase/ext/mongodb/lockservice/AdjustChangeLogLockCollectionStatementIT.java index c8e3544a..c2fcefd3 100644 --- a/src/test/java/liquibase/ext/mongodb/lockservice/AdjustChangeLogLockCollectionStatementIT.java +++ b/src/test/java/liquibase/ext/mongodb/lockservice/AdjustChangeLogLockCollectionStatementIT.java @@ -40,7 +40,6 @@ void executeToJSTest() { assertThat(adjustChangeLogLockCollectionStatement.getCommandName()).isEqualTo("adjustChangeLogLockCollection"); assertThat(adjustChangeLogLockCollectionStatement.getCollectionName()).isEqualTo(LOCK_COLLECTION_NAME); - assertThat(adjustChangeLogLockCollectionStatement.getSupportsValidator()).isTrue(); assertThat(adjustChangeLogLockCollectionStatement.toJs()).isEqualTo( "db.adjustChangeLogLockCollection({\"collMod\": \"lockCollection\", \"validator\": " + "{\"$jsonSchema\": {\"bsonType\": \"object\", \"description\": \"Database Lock Collection\", " + @@ -85,7 +84,8 @@ void executeTest() { .returns(TRUE, c -> ((Document) c.get("options")).isEmpty()); // With explicit supportsValidator=false should not be changed - new AdjustChangeLogLockCollectionStatement(LOCK_COLLECTION_NAME, FALSE).execute(database); + database.setSupportsValidator(FALSE); + new AdjustChangeLogLockCollectionStatement(LOCK_COLLECTION_NAME).execute(database); final Document collectionInfoExplicitNoAdjustment = connection.getDatabase().listCollections().filter(Filters.eq("name", LOCK_COLLECTION_NAME)).first(); @@ -94,7 +94,8 @@ void executeTest() { .returns(TRUE, c -> ((Document) c.get("options")).isEmpty()); // With explicit supportsValidator=true should be changed - new AdjustChangeLogLockCollectionStatement(LOCK_COLLECTION_NAME, TRUE).execute(database); + database.setSupportsValidator(TRUE); + new AdjustChangeLogLockCollectionStatement(LOCK_COLLECTION_NAME).execute(database); indexes.clear(); connection.getDatabase().getCollection(LOCK_COLLECTION_NAME).listIndexes().into(indexes); @@ -120,7 +121,8 @@ void insertDataTest() { // Create collection new CreateChangeLogLockCollectionStatement(LOCK_COLLECTION_NAME).execute(database); - new AdjustChangeLogLockCollectionStatement(LOCK_COLLECTION_NAME, TRUE).execute(database); + database.setSupportsValidator(TRUE); + new AdjustChangeLogLockCollectionStatement(LOCK_COLLECTION_NAME).execute(database); assertThat(findAllStatement.queryForList(database)).isEmpty(); final Document options = new Document(); @@ -206,7 +208,8 @@ void insertDataNoValidatorTest() { // Create collection new CreateChangeLogLockCollectionStatement(LOCK_COLLECTION_NAME).execute(database); - new AdjustChangeLogLockCollectionStatement(LOCK_COLLECTION_NAME, FALSE).execute(database); + database.setSupportsValidator(FALSE); + new AdjustChangeLogLockCollectionStatement(LOCK_COLLECTION_NAME).execute(database); assertThat(findAllStatement.queryForList(database)).isEmpty(); final Document options = new Document(); @@ -226,7 +229,8 @@ void insertEntityTest() { // Create collection new CreateChangeLogLockCollectionStatement(LOCK_COLLECTION_NAME).execute(database); - new AdjustChangeLogLockCollectionStatement(LOCK_COLLECTION_NAME, TRUE).execute(database); + database.setSupportsValidator(TRUE); + new AdjustChangeLogLockCollectionStatement(LOCK_COLLECTION_NAME).execute(database); assertThat(findAllStatement.queryForList(database)).isEmpty(); final Document options = new Document(); diff --git a/src/test/java/liquibase/ext/mongodb/lockservice/MongoLockServiceTest.java b/src/test/java/liquibase/ext/mongodb/lockservice/MongoLockServiceTest.java index 944c7913..306aa081 100644 --- a/src/test/java/liquibase/ext/mongodb/lockservice/MongoLockServiceTest.java +++ b/src/test/java/liquibase/ext/mongodb/lockservice/MongoLockServiceTest.java @@ -142,7 +142,6 @@ void init() { verify(executorMock, times(1)).queryForLong(any(CountCollectionByNameStatement.class)); verify(executorMock, times(1)).execute(any(CreateChangeLogLockCollectionStatement.class)); verify(executorMock, times(1)).execute(any(AdjustChangeLogLockCollectionStatement.class)); - assertThat(adjustChangeLogLockCollectionStatementArgumentCaptor.getValue().getSupportsValidator()).isTrue(); verifyNoMoreInteractions(executorMock); assertThat(lockService.getHasDatabaseChangeLogLockTable()).isTrue();