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

fix: Mismatch between timezone-aware and naive datetimes in start date and bookmarks is now correctly handled #2613

Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 18 additions & 1 deletion singer_sdk/streams/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,23 @@ def _write_replication_key_signpost(
state = self.get_context_state(context)
write_replication_key_signpost(state, value)

def _parse_datetime(self, value: str) -> datetime.datetime: # noqa: PLR6301
"""Parse a datetime string.

Args:
value: The datetime string.

Returns:
The parsed datetime, timezone-aware preferred.
"""
result = datetime_fromisoformat(value)

# Ensure datetime is timezone-aware
if not result.tzinfo:
result = result.astimezone()

return result

def compare_start_date(self, value: str, start_date_value: str) -> str:
"""Compare a bookmark value to a start date and return the most recent value.

Expand All @@ -384,7 +401,7 @@ def compare_start_date(self, value: str, start_date_value: str) -> str:
The most recent value between the bookmark and start date.
"""
if self.is_timestamp_replication_key:
return max(value, start_date_value, key=datetime_fromisoformat)
return max(value, start_date_value, key=self._parse_datetime)

return value

Expand Down
12 changes: 12 additions & 0 deletions tests/core/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ def test_stream_apply_catalog(stream: Stream):
parse(CONFIG_START_DATE).replace(tzinfo=datetime.timezone.utc),
id="datetime-repl-key-old-bookmark",
),
pytest.param(
"test",
None,
"2021-01-02T00:00:00-08:00",
datetime.datetime(
2021,
1,
2,
tzinfo=datetime.timezone(datetime.timedelta(hours=-8)),
),
id="datetime-repl-key-recent-bookmark-tz-aware",
),
pytest.param(
"unix_ts",
None,
Expand Down
Loading