Skip to content

Commit

Permalink
Migration for timezone aware timestamps (#2587)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedrazb authored May 28, 2024
1 parent 821290c commit 000b839
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
4 changes: 4 additions & 0 deletions connectors/protocol/connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
nested_get_from_dict,
next_run,
parse_datetime_string,
with_utc_tz,
)

__all__ = [
Expand Down Expand Up @@ -596,6 +597,9 @@ def _property_as_datetime(self, key):
value = self.get(key)
if value is not None:
value = parse_datetime_string(value) # pyright: ignore
# Ensure the datetime is in UTC for backward compatibility with historically naive timestamps.
# This guarantees that job scheduling logic with offset-aware timestamps works correctly.
value = with_utc_tz(value)
return value

@property
Expand Down
40 changes: 37 additions & 3 deletions tests/protocol/test_connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1736,9 +1736,11 @@ def test_feature_enabled(features_json, feature_enabled):
features = Features(features_json)

assert all(
features.feature_enabled(feature)
if enabled
else not features.feature_enabled(feature)
(
features.feature_enabled(feature)
if enabled
else not features.feature_enabled(feature)
)
for feature, enabled in feature_enabled.items()
)

Expand Down Expand Up @@ -2259,3 +2261,35 @@ async def test_get_connector_by_index():
es_client.client.search.assert_awaited_once()
assert connector.id == doc["_id"]
assert connector.index_name == index_name


@pytest.mark.parametrize(
"indexed_timestamp, expected_datetime",
[
("2024-05-28T12:34:56", datetime(2024, 5, 28, 12, 34, 56, tzinfo=timezone.utc)),
(
"2024-05-28T12:34:56+00:00",
datetime(2024, 5, 28, 12, 34, 56, tzinfo=timezone.utc),
),
(
"2024-05-28T12:34:56+02:00",
datetime(2024, 5, 28, 10, 34, 56, tzinfo=timezone.utc),
),
],
)
def test_property_as_datetime(indexed_timestamp, expected_datetime):
connector = Connector(
elastic_index=Mock(),
doc_source={
"_id": "test-tz-aware-timestamp-migration",
"_source": {
"last_sync_scheduled_at": indexed_timestamp,
"last_incremental_sync_scheduled_at": indexed_timestamp,
"last_access_control_sync_scheduled_at": indexed_timestamp,
},
},
)

assert connector.last_sync_scheduled_at == expected_datetime
assert connector.last_incremental_sync_scheduled_at == expected_datetime
assert connector.last_access_control_sync_scheduled_at == expected_datetime

0 comments on commit 000b839

Please sign in to comment.