-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Some more cleanup in abstract classes in preparation of splitting #24164
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,23 +4,66 @@ | |
|
||
package io.airbyte.integrations.source.relationaldb; | ||
|
||
import static io.airbyte.protocol.models.v0.CatalogHelpers.fieldsToJsonSchema; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.google.common.collect.Lists; | ||
import io.airbyte.protocol.models.CommonField; | ||
import io.airbyte.protocol.models.Field; | ||
import io.airbyte.protocol.models.JsonSchemaType; | ||
import io.airbyte.protocol.models.v0.AirbyteCatalog; | ||
import io.airbyte.protocol.models.v0.AirbyteStream; | ||
import io.airbyte.protocol.models.v0.CatalogHelpers; | ||
import io.airbyte.protocol.models.v0.ConfiguredAirbyteCatalog; | ||
import io.airbyte.protocol.models.v0.ConfiguredAirbyteStream; | ||
import io.airbyte.protocol.models.v0.SyncMode; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Contains utilities and helper classes for discovering schemas in database sources. | ||
*/ | ||
public class DbSourceDiscoverUtil { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(DbSourceDiscoverUtil.class); | ||
|
||
// In case of user manually modified source table schema but did not refresh it and save into the | ||
// catalog - it can lead to sync failure. This method compare actual schema vs catalog schema | ||
public static <DataType> void logSourceSchemaChange(final Map<String, TableInfo<CommonField<DataType>>> fullyQualifiedTableNameToInfo, | ||
final ConfiguredAirbyteCatalog catalog, | ||
final Function<DataType, JsonSchemaType> airbyteTypeConverter) { | ||
for (final ConfiguredAirbyteStream airbyteStream : catalog.getStreams()) { | ||
final AirbyteStream stream = airbyteStream.getStream(); | ||
final String fullyQualifiedTableName = DbSourceDiscoverUtil.getFullyQualifiedTableName(stream.getNamespace(), | ||
stream.getName()); | ||
if (!fullyQualifiedTableNameToInfo.containsKey(fullyQualifiedTableName)) { | ||
continue; | ||
} | ||
final TableInfo<CommonField<DataType>> table = fullyQualifiedTableNameToInfo.get(fullyQualifiedTableName); | ||
final List<Field> fields = table.getFields() | ||
.stream() | ||
.map(commonField -> toField(commonField, airbyteTypeConverter)) | ||
.distinct() | ||
.collect(Collectors.toList()); | ||
final JsonNode currentJsonSchema = fieldsToJsonSchema(fields); | ||
|
||
final JsonNode catalogSchema = stream.getJsonSchema(); | ||
if (!catalogSchema.equals(currentJsonSchema)) { | ||
LOGGER.warn( | ||
"Source schema changed for table {}! Actual schema: {}. Catalog schema: {}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: extra space between |
||
fullyQualifiedTableName, | ||
currentJsonSchema, | ||
catalogSchema); | ||
} | ||
} | ||
} | ||
|
||
public static <DataType> AirbyteCatalog convertTableInfosToAirbyteCatalog(final List<TableInfo<CommonField<DataType>>> tableInfos, | ||
final Map<String, List<String>> fullyQualifiedTableNameToPrimaryKeys, | ||
final Function<DataType, JsonSchemaType> airbyteTypeConverter) { | ||
|
@@ -71,7 +114,7 @@ public static String getFullyQualifiedTableName(final String nameSpace, final St | |
return nameSpace != null ? nameSpace + "." + tableName : tableName; | ||
} | ||
|
||
public static <DataType> Field toField(final CommonField<DataType> commonField, final Function<DataType, JsonSchemaType> airbyteTypeConverter) { | ||
private static <DataType> Field toField(final CommonField<DataType> commonField, final Function<DataType, JsonSchemaType> airbyteTypeConverter) { | ||
if (airbyteTypeConverter.apply(commonField.getType()) == JsonSchemaType.OBJECT && commonField.getProperties() != null | ||
&& !commonField.getProperties().isEmpty()) { | ||
final var properties = commonField.getProperties().stream().map(commField -> toField(commField, airbyteTypeConverter)).toList(); | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm good with leaving testing of this class to blackbox at the connector level for now. But we should unit test this at some point.
Also, making these methods static is appropriate here, but I always have run into an issue trying to test static methods.