-
Notifications
You must be signed in to change notification settings - Fork 32
fix(sql): Add fallback to source_defined_primary_key in CatalogProvider
#627
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
065e737
fix(sql): Add fallback to source_defined_primary_key in CatalogProvider
devin-ai-integration[bot] 821848b
refactor(sql): Simplify primary key fallback logic with one-liner
devin-ai-integration[bot] 565aa64
refactor(test): Parametrize catalog provider tests to reduce duplication
devin-ai-integration[bot] 07de856
docs(sql): Expand docstring for get_primary_keys to explain fallback …
devin-ai-integration[bot] b4aa7df
fix(format): Apply ruff formatting to docstring changes
devin-ai-integration[bot] 65e8e87
feat(sql): Prioritize source_defined_primary_key and return None when…
devin-ai-integration[bot] be8d806
feat(sql): Add guard statements for primary key validation in merge o…
devin-ai-integration[bot] 8d1ecae
fix(format): Break long docstring line to meet line length requirements
devin-ai-integration[bot] 625cd1e
fix(cherry-pick me): improve messaging for 'could not import module' …
aaronsteers 74240ab
docs(sql): Clarify that get_primary_keys returns column names, not va…
devin-ai-integration[bot] 7bf86ab
Merge branch 'devin/1751064114-fix-primary-key-fallback' of https://g…
aaronsteers 10f39d2
remove extra space
aaronsteers 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 hidden or 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 hidden or 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,160 @@ | ||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
|
|
||
| from airbyte_cdk.models import AirbyteStream, ConfiguredAirbyteCatalog, ConfiguredAirbyteStream | ||
| from airbyte_cdk.sql.shared.catalog_providers import CatalogProvider | ||
|
|
||
|
|
||
| class TestCatalogProvider: | ||
| """Test cases for CatalogProvider.get_primary_keys() method.""" | ||
|
|
||
| def test_get_primary_keys_uses_configured_primary_key_when_set(self): | ||
| """Test that configured primary_key is used when set.""" | ||
| stream = AirbyteStream( | ||
| name="test_stream", | ||
| json_schema={"type": "object", "properties": {"id": {"type": "string"}}}, | ||
| supported_sync_modes=["full_refresh"], | ||
| source_defined_primary_key=[["source_id"]], | ||
| ) | ||
| configured_stream = ConfiguredAirbyteStream( | ||
| stream=stream, | ||
| sync_mode="full_refresh", | ||
| destination_sync_mode="overwrite", | ||
| primary_key=[["configured_id"]], | ||
| ) | ||
| catalog = ConfiguredAirbyteCatalog(streams=[configured_stream]) | ||
|
|
||
| provider = CatalogProvider(catalog) | ||
| result = provider.get_primary_keys("test_stream") | ||
|
|
||
| assert result == ["configured_id"] | ||
|
|
||
| def test_get_primary_keys_falls_back_to_source_defined_when_configured_empty(self): | ||
| """Test that source_defined_primary_key is used when primary_key is empty.""" | ||
| stream = AirbyteStream( | ||
| name="test_stream", | ||
| json_schema={"type": "object", "properties": {"id": {"type": "string"}}}, | ||
| supported_sync_modes=["full_refresh"], | ||
| source_defined_primary_key=[["source_id"]], | ||
| ) | ||
| configured_stream = ConfiguredAirbyteStream( | ||
| stream=stream, | ||
| sync_mode="full_refresh", | ||
| destination_sync_mode="overwrite", | ||
| primary_key=[], # Empty configured primary key | ||
| ) | ||
| catalog = ConfiguredAirbyteCatalog(streams=[configured_stream]) | ||
|
|
||
| provider = CatalogProvider(catalog) | ||
| result = provider.get_primary_keys("test_stream") | ||
|
|
||
| assert result == ["source_id"] | ||
|
|
||
| def test_get_primary_keys_falls_back_to_source_defined_when_configured_none(self): | ||
| """Test that source_defined_primary_key is used when primary_key is None.""" | ||
| stream = AirbyteStream( | ||
| name="test_stream", | ||
| json_schema={"type": "object", "properties": {"id": {"type": "string"}}}, | ||
| supported_sync_modes=["full_refresh"], | ||
| source_defined_primary_key=[["source_id"]], | ||
| ) | ||
| configured_stream = ConfiguredAirbyteStream( | ||
| stream=stream, | ||
| sync_mode="full_refresh", | ||
| destination_sync_mode="overwrite", | ||
| primary_key=None, # None configured primary key | ||
| ) | ||
| catalog = ConfiguredAirbyteCatalog(streams=[configured_stream]) | ||
|
|
||
| provider = CatalogProvider(catalog) | ||
| result = provider.get_primary_keys("test_stream") | ||
|
|
||
| assert result == ["source_id"] | ||
|
|
||
| def test_get_primary_keys_returns_empty_when_both_empty(self): | ||
| """Test that empty list is returned when both primary keys are empty.""" | ||
| stream = AirbyteStream( | ||
| name="test_stream", | ||
| json_schema={"type": "object", "properties": {"id": {"type": "string"}}}, | ||
| supported_sync_modes=["full_refresh"], | ||
| source_defined_primary_key=[], # Empty source-defined primary key | ||
| ) | ||
| configured_stream = ConfiguredAirbyteStream( | ||
| stream=stream, | ||
| sync_mode="full_refresh", | ||
| destination_sync_mode="overwrite", | ||
| primary_key=[], # Empty configured primary key | ||
| ) | ||
| catalog = ConfiguredAirbyteCatalog(streams=[configured_stream]) | ||
|
|
||
| provider = CatalogProvider(catalog) | ||
| result = provider.get_primary_keys("test_stream") | ||
|
|
||
| assert result == [] | ||
|
|
||
| def test_get_primary_keys_returns_empty_when_both_none(self): | ||
| """Test that empty list is returned when both primary keys are None.""" | ||
| stream = AirbyteStream( | ||
| name="test_stream", | ||
| json_schema={"type": "object", "properties": {"id": {"type": "string"}}}, | ||
| supported_sync_modes=["full_refresh"], | ||
| source_defined_primary_key=None, # None source-defined primary key | ||
| ) | ||
| configured_stream = ConfiguredAirbyteStream( | ||
| stream=stream, | ||
| sync_mode="full_refresh", | ||
| destination_sync_mode="overwrite", | ||
| primary_key=None, # None configured primary key | ||
| ) | ||
| catalog = ConfiguredAirbyteCatalog(streams=[configured_stream]) | ||
|
|
||
| provider = CatalogProvider(catalog) | ||
| result = provider.get_primary_keys("test_stream") | ||
|
|
||
| assert result == [] | ||
|
|
||
| def test_get_primary_keys_handles_composite_keys_from_source_defined(self): | ||
| """Test that composite primary keys work correctly with source-defined fallback.""" | ||
| stream = AirbyteStream( | ||
| name="test_stream", | ||
| json_schema={ | ||
| "type": "object", | ||
| "properties": {"id1": {"type": "string"}, "id2": {"type": "string"}}, | ||
| }, | ||
| supported_sync_modes=["full_refresh"], | ||
| source_defined_primary_key=[["id1"], ["id2"]], # Composite primary key | ||
| ) | ||
| configured_stream = ConfiguredAirbyteStream( | ||
| stream=stream, | ||
| sync_mode="full_refresh", | ||
| destination_sync_mode="overwrite", | ||
| primary_key=[], # Empty configured primary key | ||
| ) | ||
| catalog = ConfiguredAirbyteCatalog(streams=[configured_stream]) | ||
|
|
||
| provider = CatalogProvider(catalog) | ||
| result = provider.get_primary_keys("test_stream") | ||
|
|
||
| assert result == ["id1", "id2"] | ||
|
|
||
| def test_get_primary_keys_normalizes_case_for_source_defined(self): | ||
| """Test that primary keys from source-defined are normalized to lowercase.""" | ||
| stream = AirbyteStream( | ||
| name="test_stream", | ||
| json_schema={"type": "object", "properties": {"ID": {"type": "string"}}}, | ||
| supported_sync_modes=["full_refresh"], | ||
| source_defined_primary_key=[["ID"]], # Uppercase primary key | ||
| ) | ||
| configured_stream = ConfiguredAirbyteStream( | ||
| stream=stream, | ||
| sync_mode="full_refresh", | ||
| destination_sync_mode="overwrite", | ||
| primary_key=[], # Empty configured primary key | ||
| ) | ||
| catalog = ConfiguredAirbyteCatalog(streams=[configured_stream]) | ||
|
|
||
| provider = CatalogProvider(catalog) | ||
| result = provider.get_primary_keys("test_stream") | ||
|
|
||
| assert result == ["id"] |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.