-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup createDatabase() code in AbstractJdbcSource (#24343)
* Cleanup createDatabase() code Move methods from AbstractJdbcSource Moved methods until createDataSource outside of the abstract class * Quick cleanups/renames * Add tests
- Loading branch information
Showing
8 changed files
with
114 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...rs/source-jdbc/src/main/java/io/airbyte/integrations/source/jdbc/JdbcDataSourceUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.airbyte.integrations.source.jdbc; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import io.airbyte.commons.map.MoreMaps; | ||
import io.airbyte.db.jdbc.JdbcUtils; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class JdbcDataSourceUtils { | ||
|
||
public static String DEFAULT_JDBC_PARAMETERS_DELIMITER = "&"; | ||
/** | ||
* Validates for duplication parameters | ||
* | ||
* @param customParameters custom connection properties map as specified by each Jdbc source | ||
* @param defaultParameters connection properties map as specified by each Jdbc source | ||
* @throws IllegalArgumentException | ||
*/ | ||
public static void assertCustomParametersDontOverwriteDefaultParameters(final Map<String, String> customParameters, | ||
final Map<String, String> defaultParameters) { | ||
for (final String key : defaultParameters.keySet()) { | ||
if (customParameters.containsKey(key) && !Objects.equals(customParameters.get(key), defaultParameters.get(key))) { | ||
throw new IllegalArgumentException("Cannot overwrite default JDBC parameter " + key); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves connection_properties from config and also validates if custom jdbc_url parameters | ||
* overlap with the default properties | ||
* | ||
* @param config A configuration used to check Jdbc connection | ||
* @return A mapping of connection properties | ||
*/ | ||
public static Map<String, String> getConnectionProperties(final JsonNode config) { | ||
final Map<String, String> customProperties = JdbcUtils.parseJdbcParameters(config, JdbcUtils.JDBC_URL_PARAMS_KEY); | ||
final Map<String, String> defaultProperties = JdbcDataSourceUtils.getDefaultConnectionProperties(config); | ||
assertCustomParametersDontOverwriteDefaultParameters(customProperties, defaultProperties); | ||
return MoreMaps.merge(customProperties, defaultProperties); | ||
} | ||
|
||
/** | ||
* Retrieves default connection_properties from config | ||
* | ||
* TODO: make this method abstract and add parity features to destination connectors | ||
* | ||
* @param config A configuration used to check Jdbc connection | ||
* @return A mapping of the default connection properties | ||
*/ | ||
private static Map<String, String> getDefaultConnectionProperties(final JsonNode config) { | ||
// NOTE that Postgres returns an empty map for some reason? | ||
return JdbcUtils.parseJdbcParameters(config, "connection_properties", DEFAULT_JDBC_PARAMETERS_DELIMITER); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...ource-jdbc/src/test/java/io/airbyte/integrations/source/jdbc/JdbcDataSourceUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.airbyte.integrations.source.jdbc; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import io.airbyte.commons.json.Jsons; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class JdbcDataSourceUtilsTest { | ||
|
||
@Test | ||
void test() { | ||
final String validConfigString = "{\"jdbc_url_params\":\"key1=val1&key3=key3\",\"connection_properties\":\"key1=val1&key2=val2\"}"; | ||
final JsonNode validConfig = Jsons.deserialize(validConfigString); | ||
final Map<String, String> connectionProperties = JdbcDataSourceUtils.getConnectionProperties(validConfig); | ||
final List<String> validKeys = List.of("key1", "key2", "key3"); | ||
validKeys.forEach(key -> assertTrue(connectionProperties.containsKey(key))); | ||
|
||
// For an invalid config, there is a conflict betweeen the values of keys in jdbc_url_params and connection_properties | ||
final String invalidConfigString = "{\"jdbc_url_params\":\"key1=val2&key3=key3\",\"connection_properties\":\"key1=val1&key2=val2\"}"; | ||
final JsonNode invalidConfig = Jsons.deserialize(invalidConfigString); | ||
final Exception exception = assertThrows(IllegalArgumentException.class, () -> { | ||
JdbcDataSourceUtils.getConnectionProperties(invalidConfig); | ||
}); | ||
|
||
final String expectedMessage = "Cannot overwrite default JDBC parameter key1"; | ||
assertThat(expectedMessage.equals(exception.getMessage())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters