From 8abdbab764e9eefae393a32847ec0d5a8210128f Mon Sep 17 00:00:00 2001 From: Vitalii Makarchenkov Date: Fri, 1 Mar 2024 16:04:53 +0200 Subject: [PATCH 1/3] Add user friendly message if required dependencies are not available --- .../ext/mongodb/database/MongoConnection.java | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/main/java/liquibase/ext/mongodb/database/MongoConnection.java b/src/main/java/liquibase/ext/mongodb/database/MongoConnection.java index 2ea652b2..42fe3195 100644 --- a/src/main/java/liquibase/ext/mongodb/database/MongoConnection.java +++ b/src/main/java/liquibase/ext/mongodb/database/MongoConnection.java @@ -27,6 +27,7 @@ import liquibase.GlobalConfiguration; import liquibase.Scope; import liquibase.exception.DatabaseException; +import liquibase.exception.UnexpectedLiquibaseException; import liquibase.ext.mongodb.configuration.MongoConfiguration; import liquibase.ext.mongodb.statement.BsonUtils; import liquibase.logging.Logger; @@ -68,7 +69,38 @@ public boolean supports(String url) { if (url == null) { return false; } - return url.toLowerCase().startsWith("mongodb"); + + boolean isMongodbConnection = url.toLowerCase().startsWith("mongodb"); + + if (isMongodbConnection) { + final String errorMessagePrefix = "The required dependencies JAR files are not available on the classpath:"; + String errorMessage = errorMessagePrefix; + + try { + Class.forName("com.mongodb.ConnectionString"); + } catch (ClassNotFoundException e) { + errorMessage += "\n- mongodb-driver-core.jar"; + } + + try { + Class.forName("com.mongodb.client.MongoClients"); + } catch (ClassNotFoundException e) { + errorMessage += "\n- mongodb-driver-sync.jar"; + } + + try { + Class.forName("org.bson.Transformer"); + } catch (ClassNotFoundException e) { + errorMessage += "\n- bson.jar"; + } + + if (!errorMessage.equals(errorMessagePrefix)) { + errorMessage += "\nDownload the required dependencies and place them in the 'liquibase/lib' folder"; + throw new UnexpectedLiquibaseException(errorMessage); + } + } + + return isMongodbConnection; } @Override From 2b0c0ac8a77b53398f5ec09f974b9eaaeb1c6ec7 Mon Sep 17 00:00:00 2001 From: Vitalii Makarchenkov Date: Fri, 1 Mar 2024 16:37:42 +0200 Subject: [PATCH 2/3] Fix error message --- .../java/liquibase/ext/mongodb/database/MongoConnection.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/liquibase/ext/mongodb/database/MongoConnection.java b/src/main/java/liquibase/ext/mongodb/database/MongoConnection.java index 42fe3195..53f7d472 100644 --- a/src/main/java/liquibase/ext/mongodb/database/MongoConnection.java +++ b/src/main/java/liquibase/ext/mongodb/database/MongoConnection.java @@ -73,7 +73,7 @@ public boolean supports(String url) { boolean isMongodbConnection = url.toLowerCase().startsWith("mongodb"); if (isMongodbConnection) { - final String errorMessagePrefix = "The required dependencies JAR files are not available on the classpath:"; + final String errorMessagePrefix = "The required dependencies (JAR files) are not available on the classpath:"; String errorMessage = errorMessagePrefix; try { From a8606446171ef01a81ff14732f304494a0d04ae3 Mon Sep 17 00:00:00 2001 From: Vitalii Makarchenkov Date: Mon, 18 Mar 2024 12:56:10 +0200 Subject: [PATCH 3/3] Fix code --- .../ext/mongodb/database/MongoConnection.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/liquibase/ext/mongodb/database/MongoConnection.java b/src/main/java/liquibase/ext/mongodb/database/MongoConnection.java index 53f7d472..a3559ae2 100644 --- a/src/main/java/liquibase/ext/mongodb/database/MongoConnection.java +++ b/src/main/java/liquibase/ext/mongodb/database/MongoConnection.java @@ -70,8 +70,14 @@ public boolean supports(String url) { return false; } - boolean isMongodbConnection = url.toLowerCase().startsWith("mongodb"); + boolean isMongodbConnection = url.toLowerCase().startsWith("mongodb"); + showErrorMessageIfSomeRequiredDependenciesAreNotPresent(isMongodbConnection); + + return isMongodbConnection; + } + + private static void showErrorMessageIfSomeRequiredDependenciesAreNotPresent(boolean isMongodbConnection) { if (isMongodbConnection) { final String errorMessagePrefix = "The required dependencies (JAR files) are not available on the classpath:"; String errorMessage = errorMessagePrefix; @@ -99,8 +105,6 @@ public boolean supports(String url) { throw new UnexpectedLiquibaseException(errorMessage); } } - - return isMongodbConnection; } @Override