Skip to content

Commit

Permalink
DAT-18491: checks on existing catalog and/or schema specified in conn…
Browse files Browse the repository at this point in the history
…ection URL added.
  • Loading branch information
Mykhailo Savchenko committed Nov 6, 2024
1 parent fbae6b5 commit af7c46b
Showing 1 changed file with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")));
}

}

0 comments on commit af7c46b

Please sign in to comment.