Skip to content

Commit

Permalink
Simplify and knock out transferwise#68
Browse files Browse the repository at this point in the history
  • Loading branch information
msg555 committed Sep 19, 2023
1 parent 13f8321 commit 7cf43c0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
8 changes: 3 additions & 5 deletions target_postgres/db_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ def stream_name_to_dict(stream_name, separator='-'):


def csv_quote(s):
if s is None:
return ""
if isinstance(s, int):
return str(s)
return '"' + str(s).replace("\\", "\\\\").replace('"', '\\"') + '"'
Expand Down Expand Up @@ -353,11 +355,7 @@ def record_primary_key_string(self, record):
def record_to_csv_line(self, record):
flatten = flatten_record(record, self.flatten_schema, max_level=self.data_flattening_max_level)
return ','.join(
[
csv_quote(flatten[name])
if name in flatten and (flatten[name] == 0 or flatten[name]) else ''
for name in self.flatten_schema
]
csv_quote(flatten.get(name)) for name in self.flatten_schema
)

def load_csv(self, file, count, size_bytes):
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_db_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,26 @@ def test_record_to_csv_line(self):
},
'1,"some \\"quotes\\" and \\\\backslashes\\\\",555',
),
(
{
"c_pk": 1,
"c_varchar": "",
"c_int": 2,
},
'1,"",2',
),
(
{
"c_pk": 1,
"c_varchar": None,
"c_int": 2,
},
'1,,2',
),
(
{},
',,',
),
]

for idx, (record, expected_output) in enumerate(test_cases):
Expand Down

0 comments on commit 7cf43c0

Please sign in to comment.