Skip to content

Commit 859dc67

Browse files
committed
Simplify database selection logic
1 parent 7551ca6 commit 859dc67

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/connector/snowflake/AbstractSnowflakeConnector.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
2020
import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
21+
import static org.apache.hadoop.util.Preconditions.checkNotNull;
2122

2223
import com.google.common.base.CharMatcher;
2324
import com.google.common.base.Joiner;
@@ -85,7 +86,9 @@ public final Handle open(@Nonnull ConnectorArguments arguments)
8586
DataSource dataSource = new SimpleDriverDataSource(newDriver(arguments), url, properties);
8687
if (arguments.isAssessment()) {
8788
JdbcHandle handle = new JdbcHandle(dataSource);
88-
setCurrentDatabase("SNOWFLAKE", handle.getJdbcTemplate());
89+
JdbcTemplate template = handle.getJdbcTemplate();
90+
String actualDatabase = template.queryForObject("USE DATABASE SNOWFLAKE;", String.class);
91+
checkNotNull(actualDatabase);
8992
return handle;
9093
} else {
9194
String databaseName =
@@ -204,16 +207,16 @@ private void setCurrentDatabase(@Nonnull String databaseName, @Nonnull JdbcTempl
204207
}
205208

206209
String sanitizeDatabaseName(@Nonnull String databaseName) throws MetadataDumperUsageException {
207-
CharMatcher doubleQuoteMatcher = CharMatcher.is('"');
208-
String trimmedName = doubleQuoteMatcher.trimFrom(databaseName);
209-
int charLengthWithQuotes = databaseName.length() + 2;
210-
int maxDatabaseCharLength = 255;
211-
if (charLengthWithQuotes > 255) {
210+
int lengthWithQuotes = databaseName.length() + 2;
211+
int maxLength = 255;
212+
if (lengthWithQuotes > maxLength) {
212213
throw new MetadataDumperUsageException(
213214
String.format(
214215
"The provided database name has %d characters, which is longer than the maximum allowed number %d for Snowflake identifiers.",
215-
charLengthWithQuotes, maxDatabaseCharLength));
216+
lengthWithQuotes, maxLength));
216217
}
218+
CharMatcher doubleQuoteMatcher = CharMatcher.is('"');
219+
String trimmedName = doubleQuoteMatcher.trimFrom(databaseName);
217220
if (doubleQuoteMatcher.matchesAnyOf(trimmedName)) {
218221
throw new MetadataDumperUsageException(
219222
"Database name has incorrectly placed double quote(s). Aborting query.");

0 commit comments

Comments
 (0)