Skip to content

Commit

Permalink
Merge pull request #517 from liquibase/DAT-16600
Browse files Browse the repository at this point in the history
[DAT-16600] Implement method to send ping signal to DB server for connect command
  • Loading branch information
vitaliimak authored May 23, 2024
2 parents cea5be9 + 3f13e60 commit 247abba
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
* #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;
import liquibase.executor.ExecutorService;
Expand All @@ -32,6 +36,9 @@
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;

Expand Down Expand Up @@ -118,5 +125,19 @@ public Boolean getSupportsValidator() {
return MongoConfiguration.SUPPORTS_VALIDATOR.getCurrentValue();
}

@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);
}
}

}

0 comments on commit 247abba

Please sign in to comment.