From af7c46bb39899794997499ab1aa3bd10e2377b46 Mon Sep 17 00:00:00 2001 From: Mykhailo Savchenko Date: Wed, 6 Nov 2024 11:55:38 +0200 Subject: [PATCH 1/6] 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"))); + } + } From a0442cc246c9831be933a3816c180bee5558d069 Mon Sep 17 00:00:00 2001 From: Mykhailo Savchenko Date: Wed, 6 Nov 2024 14:42:47 +0200 Subject: [PATCH 2/6] DAT-18491: review changes - reused existing getConnectionCatalogName, getConnectionSchemaName instead of manually parsing. Removed ombigious check. --- .../database/DatabricksDatabase.java | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java b/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java index 9f5101d9..2a3e6daa 100644 --- a/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java +++ b/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java @@ -370,8 +370,8 @@ public void setConnection(DatabaseConnection conn) { public void checkDatabaseConnection() throws DatabaseException { DatabricksConnection connection = (DatabricksConnection) getConnection(); String url = connection.getURL(); - String usedCatalog = findPropertyInUrl("ConnCatalog", url); - String usedSchema = findPropertyInUrl("ConnSchema", url); + String usedCatalog = getConnectionCatalogName(); + String usedSchema = getConnectionSchemaName(); try { verifySchemaAndCatalog(usedCatalog, usedSchema, connection.getMetaData()); } catch (SQLException e) { @@ -379,11 +379,6 @@ public void checkDatabaseConnection() throws DatabaseException { } } - 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; @@ -405,15 +400,6 @@ private void verifySchemaAndCatalog(String usedCatalog, String usedSchema, Datab } } } - //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"))); } From dcf7ce15a9572c83ffb3ce2dfc7087409dded6b4 Mon Sep 17 00:00:00 2001 From: Mykhailo Savchenko Date: Wed, 6 Nov 2024 14:44:09 +0200 Subject: [PATCH 3/6] DAT-18491: unused imports cleaned --- .../liquibase/ext/databricks/database/DatabricksDatabase.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java b/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java index 2a3e6daa..03685863 100644 --- a/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java +++ b/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java @@ -17,8 +17,6 @@ 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 { From 2f84df4a790885368e7552b261fa8a78031e7d82 Mon Sep 17 00:00:00 2001 From: Mykhailo Savchenko Date: Wed, 6 Nov 2024 14:46:12 +0200 Subject: [PATCH 4/6] DAT-18491: removed url logging --- .../liquibase/ext/databricks/database/DatabricksDatabase.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java b/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java index 03685863..d6ae4aaf 100644 --- a/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java +++ b/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java @@ -367,13 +367,12 @@ public void setConnection(DatabaseConnection conn) { @Override public void checkDatabaseConnection() throws DatabaseException { DatabricksConnection connection = (DatabricksConnection) getConnection(); - String url = connection.getURL(); String usedCatalog = getConnectionCatalogName(); String usedSchema = getConnectionSchemaName(); try { verifySchemaAndCatalog(usedCatalog, usedSchema, connection.getMetaData()); } catch (SQLException e) { - Scope.getCurrentScope().getLog(getClass()).info("Error checking database connection with URL=" + url, e); + Scope.getCurrentScope().getLog(getClass()).info("Error checking database connection with URL", e); } } From 826aa0314ca61e49592aeef1064a6cc7f45ff08b Mon Sep 17 00:00:00 2001 From: Mykhailo Savchenko Date: Wed, 6 Nov 2024 15:31:03 +0200 Subject: [PATCH 5/6] DAT-18491: simplified catalog and schema checks. Default catalog and schema should be present anyway if credentials and path to Database are correct. --- .../database/DatabricksDatabase.java | 33 +++++-------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java b/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java index d6ae4aaf..6b1919c1 100644 --- a/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java +++ b/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java @@ -367,37 +367,20 @@ public void setConnection(DatabaseConnection conn) { @Override public void checkDatabaseConnection() throws DatabaseException { DatabricksConnection connection = (DatabricksConnection) getConnection(); - String usedCatalog = getConnectionCatalogName(); - String usedSchema = getConnectionSchemaName(); try { - verifySchemaAndCatalog(usedCatalog, usedSchema, connection.getMetaData()); - } catch (SQLException e) { - Scope.getCurrentScope().getLog(getClass()).info("Error checking database connection with URL", e); - } - } - - 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); + String catalogName = getConnectionCatalogName(); + String schemaName = getConnectionSchemaName(); + ResultSet schemasAlikeUsed = connection.getMetaData().getSchemas(catalogName, schemaName); 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)) { + if (schemasAlikeUsed.getString(1).equals(schemaName)) { return; } } + throw new DatabaseException(String.format("Please specify existing schema and catalog in connection url. " + + "Current connection points to '%s.%s'", catalogName, schemaName)); + } catch (SQLException e) { + Scope.getCurrentScope().getLog(getClass()).info("Error checking database connection", e); } - throw new DatabaseException(String.format("Please specify existing %s in connection url", Arrays.asList("ConnCatalog", "ConnSchema"))); } } From 03722719d1b0efa8face608c2cb553f038ce36f3 Mon Sep 17 00:00:00 2001 From: Mykhailo Savchenko Date: Wed, 6 Nov 2024 16:01:03 +0200 Subject: [PATCH 6/6] DAT-18491: removed unused import --- .../liquibase/ext/databricks/database/DatabricksDatabase.java | 1 - 1 file changed, 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 6b1919c1..60833af7 100644 --- a/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java +++ b/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java @@ -13,7 +13,6 @@ import lombok.Setter; import java.math.BigInteger; -import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; import java.util.*;