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

Remove returns in final clause of S3ToDynamoDBOperator #43456

Merged
merged 7 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,13 @@ def _load_into_existing_table(self) -> str:
total_items += page.get("Count", 0)
dynamodb_hook.write_batch_data(items=page["Items"])
self.log.info("Number of items loaded: %s", total_items)
finally:
self.log.info("Delete tmp DynamoDB table %s", self.tmp_table_name)
client.delete_table(TableName=self.tmp_table_name)
return dynamodb_hook.get_conn().Table(self.dynamodb_table_name).table_arn
except Exception as e:
# Does not raise errors to keep previous behavior.
romsharon98 marked this conversation as resolved.
Show resolved Hide resolved
self.log.warning("Error while loading data from temp table, deleting temp table. Error: %s", e)
yangyulely marked this conversation as resolved.
Show resolved Hide resolved

self.log.info("Delete tmp DynamoDB table %s", self.tmp_table_name)
client.delete_table(TableName=self.tmp_table_name)
yangyulely marked this conversation as resolved.
Show resolved Hide resolved
return dynamodb_hook.get_conn().Table(self.dynamodb_table_name).table_arn

def execute(self, context: Context) -> str:
"""
Expand Down
29 changes: 29 additions & 0 deletions providers/tests/amazon/aws/transfers/test_s3_to_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,32 @@ def test_s3_to_dynamodb_existing_table(self, mock_get_conn, new_table_load_mock,
mock_put_item.assert_has_calls(batch_writer_calls)
mock_client.delete_table.assert_called_once_with(TableName=exist_table_op.tmp_table_name)
assert res == "arn:aws:dynamodb"

@mock.patch(
"airflow.providers.amazon.aws.transfers.s3_to_dynamodb.S3ToDynamoDBOperator._load_into_new_table"
)
@mock.patch.object(DynamoDBHook, "get_conn")
def test_load_into_existing_table_exception(self, mock_get_conn, new_table_load_mock, exist_table_op):
mock_conn = mock.MagicMock()
mock_client = mock.Mock()

mock_client.get_paginator.paginate.side_effect = AirflowException()
mock_conn.meta.client = mock_client
mock_conn.Table.return_value.table_arn = "arn:aws:dynamodb"
mock_get_conn.return_value = mock_conn

res = exist_table_op._load_into_existing_table()

new_table_load_mock.assert_called_once_with(
table_name=exist_table_op.tmp_table_name, delete_on_error=False
)
mock_client.get_paginator.assert_called_once_with("scan")
mock_client.get_paginator.return_value.paginate.assert_called_once_with(
TableName=exist_table_op.tmp_table_name,
Select="ALL_ATTRIBUTES",
ReturnConsumedCapacity="NONE",
ConsistentRead=True,
)
mock_conn.Table.assert_called_with("test-table")
mock_client.delete_table.assert_called_once_with(TableName=exist_table_op.tmp_table_name)
assert res == "arn:aws:dynamodb"