From 194e07eb812ee9a508035bee8a8d456c94431264 Mon Sep 17 00:00:00 2001 From: Vitalii Makarchenkov Date: Thu, 23 May 2024 18:30:01 +0300 Subject: [PATCH] Fix incorrect error --- .../ext/mongodb/database/MongoConnection.java | 2 +- .../database/MongoLiquibaseDatabase.java | 20 ++----------- .../database/MongoLiquibaseDatabaseUtil.java | 30 +++++++++++++++++++ 3 files changed, 33 insertions(+), 19 deletions(-) create mode 100644 src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabaseUtil.java diff --git a/src/main/java/liquibase/ext/mongodb/database/MongoConnection.java b/src/main/java/liquibase/ext/mongodb/database/MongoConnection.java index a3559ae2..fdae3fdc 100644 --- a/src/main/java/liquibase/ext/mongodb/database/MongoConnection.java +++ b/src/main/java/liquibase/ext/mongodb/database/MongoConnection.java @@ -77,7 +77,7 @@ public boolean supports(String url) { return isMongodbConnection; } - private static void showErrorMessageIfSomeRequiredDependenciesAreNotPresent(boolean isMongodbConnection) { + public static void showErrorMessageIfSomeRequiredDependenciesAreNotPresent(boolean isMongodbConnection) { if (isMongodbConnection) { final String errorMessagePrefix = "The required dependencies (JAR files) are not available on the classpath:"; String errorMessage = errorMessagePrefix; diff --git a/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabase.java b/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabase.java index cf4bda1c..d1577670 100644 --- a/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabase.java +++ b/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabase.java @@ -20,13 +20,10 @@ * #L% */ -import com.mongodb.MongoException; -import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; import liquibase.CatalogAndSchema; import liquibase.Scope; import liquibase.changelog.ChangeLogHistoryServiceFactory; -import liquibase.database.DatabaseConnection; import liquibase.exception.DatabaseException; import liquibase.exception.LiquibaseException; import liquibase.executor.Executor; @@ -36,9 +33,6 @@ import liquibase.nosql.database.AbstractNoSqlDatabase; import lombok.NoArgsConstructor; import lombok.Setter; -import org.bson.BsonDocument; -import org.bson.BsonInt64; -import org.bson.conversions.Bson; import static liquibase.nosql.executor.NoSqlExecutor.EXECUTOR_NAME; @@ -127,17 +121,7 @@ public Boolean getSupportsValidator() { @Override public void checkDatabaseConnection() throws DatabaseException { - DatabaseConnection connection = getConnection(); - try { - if (connection instanceof MongoConnection) { - MongoClient mongoClient = ((MongoConnection) connection).getMongoClient(); - Bson command = new BsonDocument("ping", new BsonInt64(1)); - MongoDatabase adminDb = mongoClient.getDatabase(ADMIN_DATABASE_NAME); - adminDb.runCommand(command); - } - } catch (MongoException e) { - throw new DatabaseException(e); - } + MongoConnection.showErrorMessageIfSomeRequiredDependenciesAreNotPresent(true); + MongoLiquibaseDatabaseUtil.sendPingSignal(getConnection()); } - } diff --git a/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabaseUtil.java b/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabaseUtil.java new file mode 100644 index 00000000..76784957 --- /dev/null +++ b/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabaseUtil.java @@ -0,0 +1,30 @@ +package liquibase.ext.mongodb.database; + +import com.mongodb.MongoException; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoDatabase; +import liquibase.database.DatabaseConnection; +import liquibase.exception.DatabaseException; +import org.bson.BsonDocument; +import org.bson.BsonInt64; +import org.bson.conversions.Bson; + +import static liquibase.ext.mongodb.database.MongoLiquibaseDatabase.ADMIN_DATABASE_NAME; + +public class MongoLiquibaseDatabaseUtil { + private MongoLiquibaseDatabaseUtil() { + } + + public static void sendPingSignal(DatabaseConnection connection) throws DatabaseException { + try { + if (connection instanceof MongoConnection) { + MongoClient mongoClient = ((MongoConnection) connection).getMongoClient(); + Bson command = new BsonDocument("ping", new BsonInt64(1)); + MongoDatabase adminDb = mongoClient.getDatabase(ADMIN_DATABASE_NAME); + adminDb.runCommand(command); + } + } catch (MongoException e) { + throw new DatabaseException(e); + } + } +}