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

Feature/replication keys #25

Merged
merged 7 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
42 changes: 42 additions & 0 deletions elx/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,45 @@ def select(self, streams: Optional[List[str]]) -> "Catalog":
)

return catalog

def set_replication_keys(self, replication_keys: Optional[dict]) -> "Catalog":
"""
Set the replication key for streams and updates the catalog.

Args:
keys (Optional[dict]): Dictionary stream replication_key value-pairs.
E.g. {"stream_one": "updated_at", "stream_two": "modified_at"}

Returns:
Catalog: A new catalog with updated replication settings.
"""
# Make a copy of the existing catalog.
catalog = self.copy(deep=True)

# Loop over the streams
for stream in catalog.streams:
# If the stream is specified in `replication_keys` dictionary
if stream.tap_stream_id in replication_keys:
# Set the replication method to INCREMENTAL
stream.replication_method = "INCREMENTAL"

# Update the replication key value
stream.replication_key = replication_keys[stream.tap_stream_id]
BernardWez marked this conversation as resolved.
Show resolved Hide resolved

# Add the replication key to stream metadata
stream.upsert_metadata(
breadcrumb=[],
metadata={
"valid-replication-keys": [
replication_keys[stream.tap_stream_id]
]
},
)
BernardWez marked this conversation as resolved.
Show resolved Hide resolved

# Set inclusion of replication property metadata to `automatic`
stream.upsert_metadata(
breadcrumb=["properties", replication_keys[stream.tap_stream_id]],
metadata={"inclusion": "automatic"},
)

return catalog
8 changes: 7 additions & 1 deletion elx/tap.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ def __init__(
executable: str | None = None,
config: dict = {},
deselected: List[str] = None,
replication_keys: dict = {},
):
super().__init__(spec, executable, config)
self.deselected = deselected
self.replication_keys = replication_keys

def discover(self, config_path: Path) -> dict:
"""
Expand All @@ -45,7 +47,11 @@ def catalog(self) -> Catalog:
with json_temp_file(self.config) as config_path:
catalog = self.discover(config_path)
catalog = Catalog(**catalog)
return catalog.deselect(patterns=self.deselected)
catalog = catalog.deselect(patterns=self.deselected)
catalog = catalog.set_replication_keys(
replication_keys=self.replication_keys
)
return catalog

@contextlib.asynccontextmanager
@require_install
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/tap.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ def tap() -> Generator[Tap, None, None]:
executable="tap-mock-fixture",
spec="git+https://github.com/quantile-taps/tap-mock-fixture.git",
config={},
replication_keys={"users": "updated_at"},
)
8 changes: 8 additions & 0 deletions tests/test_elx/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,11 @@ def test_catalog_valid_replication_keys(tap: Tap):

# Checks that value of `valid-replication-keys` equals to the replication-key
assert replication_keys == [DEFAULT_CATALOG["streams"][1]["replication_key"]]


def test_catalog_set_stream_replication_key(tap: Tap):
"""If we define a replication key, the catalog should be updated."""
catalog = tap.catalog

assert catalog.streams[1].replication_method == "INCREMENTAL"
assert catalog.streams[1].replication_key == "updated_at"
Loading