Skip to content

Commit 9d45658

Browse files
committed
handle case when no state is passed
1 parent f359981 commit 9d45658

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

airbyte_cdk/sources/declarative/migrations/legacy_to_per_partition_state_migration.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,20 @@ def should_migrate(self, stream_state: Mapping[str, Any]) -> bool:
7878
"<cursor_field>" : "<cursor_value>"
7979
}
8080
"""
81-
if stream_state:
82-
for key, value in stream_state.items():
83-
if isinstance(value, dict):
84-
keys = list(value.keys())
85-
if len(keys) != 1:
86-
# The input partitioned state should only have one key
87-
return False
88-
if keys[0] != self._cursor_field:
89-
# Unexpected key. Found {keys[0]}. Expected {self._cursor.cursor_field}
90-
return False
91-
# it is expected the internal value to be a dictionary according to docstring
92-
else:
93-
return False
81+
if not stream_state:
82+
return False
83+
for key, value in stream_state.items():
84+
# it is expected the internal value to be a dictionary according to docstring
85+
if not isinstance(value, dict):
86+
return False
87+
keys = list(value.keys())
88+
if len(keys) != 1:
89+
# The input partitioned state should only have one key
90+
return False
91+
if keys[0] != self._cursor_field:
92+
# Unexpected key. Found {keys[0]}. Expected {self._cursor.cursor_field}
93+
return False
94+
9495
return True
9596

9697
def migrate(self, stream_state: Mapping[str, Any]) -> Mapping[str, Any]:

unit_tests/sources/declarative/migrations/test_legacy_to_per_partition_migration.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ def test_migrate_a_valid_legacy_state_to_per_partition():
193193
{"last_changed": "2022-12-27T08:34:39+00:00"},
194194
id="test_should_not_migrate_if_the_partitioned_state_is_not_in_correct_format",
195195
),
196+
pytest.param(
197+
{},
198+
id="test_should_not_migrate_if_not_state_is_passed",
199+
),
196200
],
197201
)
198202
def test_should_not_migrate(input_state):

0 commit comments

Comments
 (0)