From af7c46bb39899794997499ab1aa3bd10e2377b46 Mon Sep 17 00:00:00 2001 From: Mykhailo Savchenko Date: Wed, 6 Nov 2024 11:55:38 +0200 Subject: [PATCH] DAT-18491: checks on existing catalog and/or schema specified in connection URL added. --- .../database/DatabricksDatabase.java | 56 ++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java b/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java index bf3d274b..9f5101d9 100644 --- a/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java +++ b/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java @@ -10,12 +10,15 @@ import liquibase.structure.DatabaseObject; import liquibase.structure.core.Catalog; import liquibase.structure.core.Schema; -import org.apache.commons.lang3.StringUtils; import lombok.Setter; import java.math.BigInteger; +import java.sql.DatabaseMetaData; import java.sql.ResultSet; +import java.sql.SQLException; import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class DatabricksDatabase extends AbstractJdbcDatabase { @@ -363,4 +366,55 @@ public void setConnection(DatabaseConnection conn) { super.setConnection(dbConn); } + @Override + public void checkDatabaseConnection() throws DatabaseException { + DatabricksConnection connection = (DatabricksConnection) getConnection(); + String url = connection.getURL(); + String usedCatalog = findPropertyInUrl("ConnCatalog", url); + String usedSchema = findPropertyInUrl("ConnSchema", url); + try { + verifySchemaAndCatalog(usedCatalog, usedSchema, connection.getMetaData()); + } catch (SQLException e) { + Scope.getCurrentScope().getLog(getClass()).info("Error checking database connection with URL=" + url, e); + } + } + + private String findPropertyInUrl(String matchingProperty, String url) { + Matcher matcher = Pattern.compile(matchingProperty + "=(.*?)(;|$)").matcher(url); + return matcher.find() ? matcher.group(1) : null; + } + + private void verifySchemaAndCatalog(String usedCatalog, String usedSchema, DatabaseMetaData metaData) throws SQLException, DatabaseException { + if (usedCatalog == null && usedSchema == null) { + return; + } + if (usedCatalog != null && usedSchema != null) { + ResultSet schemasAlikeUsed = metaData.getSchemas(usedCatalog, usedSchema); + while (schemasAlikeUsed.next()) { + if (schemasAlikeUsed.getString(1).equals(usedSchema)) { + return; + } + } + } + //default schema might work and be expected + if (usedCatalog != null) { + ResultSet catalogs = metaData.getCatalogs(); + while (catalogs.next()) { + if (catalogs.getString(1).equals(usedCatalog)) { + return; + } + } + } + //default catalog might work and be expected + if (usedSchema != null) { + ResultSet schemas = metaData.getSchemas(); + while (schemas.next()) { + if (schemas.getString(1).equals(usedSchema)) { + return; + } + } + } + throw new DatabaseException(String.format("Please specify existing %s in connection url", Arrays.asList("ConnCatalog", "ConnSchema"))); + } + }