Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DAT-18491: Added verification of user specified schema and catalog inconnection uri #211

Merged
merged 7 commits into from
Nov 11, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")));
}

}
Loading