com.liquibase.ext.tools.MongoshExecutor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liquibase-commercial-mongodb Show documentation
Show all versions of liquibase-commercial-mongodb Show documentation
Liquibase Commercial Extension for MongoDB
The newest version!
package com.liquibase.ext.tools;
import com.datical.liquibase.ext.tools.AbstractNativeToolExecutor;
import com.liquibase.ext.change.MongoshChange;
import com.liquibase.ext.change.MongoshFileChange;
import com.liquibase.ext.statement.MongoshStatement;
import liquibase.Scope;
import liquibase.change.Change;
import liquibase.change.core.EmptyChange;
import liquibase.changelog.ChangeSet;
import liquibase.database.Database;
import liquibase.exception.DatabaseException;
import liquibase.exception.ValidationErrors;
import liquibase.ext.mongodb.database.MongoLiquibaseDatabase;
import liquibase.logging.Logger;
import liquibase.sql.Sql;
import liquibase.sql.visitor.SqlVisitor;
import liquibase.sqlgenerator.SqlGeneratorFactory;
import liquibase.statement.SqlStatement;
import java.util.List;
import java.util.ResourceBundle;
public class MongoshExecutor extends AbstractNativeToolExecutor {
private static ResourceBundle mongoshBundle = ResourceBundle.getBundle("liquibase/i18n/liquibase-mongosh");
protected static final String MSG_UNABLE_TO_VALIDATE_CHANGE_SET = mongoshBundle.getString("unable.to.validate.changeset");
private static String name = "mongosh";
public MongoshExecutor() {
super(name);
}
@Override
public String getName() {
return name;
}
@Override
public int getPriority() {
return PRIORITY_SPECIALIZED;
}
@Override
public boolean supports(Database database) {
return database instanceof MongoLiquibaseDatabase;
}
@Override
protected void validateChange(ChangeSet changeSet,
ValidationErrors validationErrors,
Change change,
String type) {
if (!(change instanceof MongoshChange || change instanceof MongoshFileChange || change instanceof EmptyChange)) {
String details = "In changeset '" + changeSet.getId() + "::" + changeSet.getAuthor() +
"' there is an unsupported change type '" + change.toString() + "'";
String message = String.format(MSG_UNABLE_TO_VALIDATE_CHANGE_SET, details, type, name, MongoshChange.CHANGE_NAME,
MongoshFileChange.CHANGE_NAME);
validationErrors.addError(message);
}
}
@Override
public void execute(SqlStatement noSqlStatement, List sqlVisitors) throws DatabaseException {
final Logger log = Scope.getCurrentScope().getLog(getClass());
if (!(noSqlStatement instanceof MongoshStatement)) {
log.warning("statement should be type or subtype of MongoshStatement, but is " + noSqlStatement.getClass() + ". Learn more at https://docs.liquibase.com/mongodb");
}
log.info("Executing with the '" + getName() + "' executor");
Sql[] nosqlStatements = SqlGeneratorFactory.getInstance().generateSql(noSqlStatement, database);
try {
MongoshRunner runner = new MongoshRunner(changeSet, null); //Validate mongosh executable exists
runner.executeCommand(database);
log.info("Successfully validated mongosh");
runner = new MongoshRunner(changeSet, nosqlStatements);
runner.executeCommand(database);
log.info(String.format("Success! Changeset '%s' by '%s' deployed by mongosh", changeSet.getId(), changeSet.getAuthor()));
} catch (Exception e) {
log.warning(String.format("%nChangeset '%s' by '%s' failed to deploy with 'mongosh'. " +
"Please check mongosh logs",
changeSet.getId(), changeSet.getAuthor()) + ". Learn more at https://docs.liquibase.com/mongodb");
throw new DatabaseException(e);
}
}
}