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

Issue 67: Supports validator read directly from database #94

Merged
merged 1 commit into from
Feb 7, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,17 @@
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_";
public static String OPTIONS = "{ collMod: \"%s\"," + CreateChangeLogCollectionStatement.VALIDATOR + "}";

@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
Expand All @@ -62,7 +53,7 @@ public void execute(final MongoLiquibaseDatabase database) {

adjustIndexes(database);

if (TRUE.equals(supportsValidator)) {
if (database.getSupportsValidator()) {
super.execute(database);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MongoLiquibaseDatabase> {

private final Logger log = Scope.getCurrentScope().getLog(getClass());

Expand Down Expand Up @@ -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: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -57,7 +48,7 @@ public String getCommandName() {

@Override
public void execute(final MongoLiquibaseDatabase database) {
if(TRUE.equals(supportsValidator)) {
if(database.getSupportsValidator()) {
super.execute(database);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

import static java.lang.Boolean.FALSE;

public class MongoLockService extends AbstractNoSqlLockService {
public class MongoLockService extends AbstractNoSqlLockService<MongoLiquibaseDatabase> {

private final Logger log = Scope.getCurrentScope().getLog(getClass());

Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* #L%
*/

import liquibase.Liquibase;
import liquibase.Scope;
import liquibase.changelog.AbstractChangeLogHistoryService;
import liquibase.changelog.ChangeSet;
Expand All @@ -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;
Expand All @@ -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<D extends MongoLiquibaseDatabase> extends AbstractChangeLogHistoryService {

@Getter
private List<RanChangeSet> ranChangeSetList;
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -262,7 +268,7 @@ public void destroy() {
}

protected abstract Logger getLogger();

protected abstract Boolean existsRepository() throws DatabaseException;

protected abstract void createRepository() throws DatabaseException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<D extends AbstractNoSqlDatabase> implements LockService {

private AbstractNoSqlDatabase database;
private D database;

private boolean hasChangeLogLock;

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.\", " +
Expand Down Expand Up @@ -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();

Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\", " +
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down