From 47de54170c2e140d9c788bead699a68660e59dc0 Mon Sep 17 00:00:00 2001 From: Niklas Schmidtmer Date: Fri, 16 Aug 2024 17:37:36 +0200 Subject: [PATCH] DynamoDB: Wrap all values in UPDATE statements in quotes --- CHANGES.md | 1 + src/commons_codec/transform/dynamodb.py | 6 ++++-- tests/transform/test_dynamodb.py | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 11283d8..d08ef28 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,7 @@ # Changelog ## Unreleased +- DynamoDB: Fixed a syntax issue with `text` data type in `UPDATE` statements ## 2024/08/16 v0.0.6 - Changed `UPDATE` statements from DMS not to write the entire `data` diff --git a/src/commons_codec/transform/dynamodb.py b/src/commons_codec/transform/dynamodb.py index 5fbde2a..bc439e2 100644 --- a/src/commons_codec/transform/dynamodb.py +++ b/src/commons_codec/transform/dynamodb.py @@ -139,14 +139,16 @@ def values_to_update(self, keys: t.Dict[str, t.Dict[str, str]]) -> str: {'humidity': {'N': '84.84'}, 'temperature': {'N': '55.66'}} OUT: - data['humidity] = 84.84, temperature = 55.66 + data['humidity] = '84.84', temperature = '55.66' """ values_clause = self.deserialize_item(keys) constraints: t.List[str] = [] for key_name, key_value in values_clause.items(): - constraint = f"{self.DATA_COLUMN}['{key_name}'] = {key_value}" + key_value = str(key_value).replace("'", "''") + constraint = f"{self.DATA_COLUMN}['{key_name}'] = '{key_value}'" constraints.append(constraint) + return ", ".join(constraints) def keys_to_where(self, keys: t.Dict[str, t.Dict[str, str]]) -> str: diff --git a/tests/transform/test_dynamodb.py b/tests/transform/test_dynamodb.py index 4cad2a8..e9154f7 100644 --- a/tests/transform/test_dynamodb.py +++ b/tests/transform/test_dynamodb.py @@ -68,12 +68,14 @@ "humidity": {"N": "84.84"}, "temperature": {"N": "55.66"}, "device": {"S": "bar"}, + "location": {"S": "Sydney"}, "timestamp": {"S": "2024-07-12T01:17:42"}, }, "OldImage": { "humidity": {"N": "84.84"}, "temperature": {"N": "42.42"}, "device": {"S": "foo"}, + "location": {"S": "Sydney"}, "timestamp": {"S": "2024-07-12T01:17:42"}, }, "SizeBytes": 161, @@ -145,7 +147,7 @@ def test_decode_cdc_insert_nested(): def test_decode_cdc_modify(): assert ( DynamoCDCTranslatorCrateDB(table_name="foo").to_sql(MSG_MODIFY) == 'UPDATE "foo" ' - "SET data['humidity'] = 84.84, data['temperature'] = 55.66 " + "SET data['humidity'] = '84.84', data['temperature'] = '55.66', data['location'] = 'Sydney' " "WHERE data['device'] = 'foo' AND data['timestamp'] = '2024-07-12T01:17:42';" )