Skip to content
This repository was archived by the owner on Aug 25, 2022. It is now read-only.

Commit 7aa7c32

Browse files
Fix zendesk (#1)
* fix the target-postgres to work with zendesk - fix an error when an empty type declaration is found i.e. "custom": {} - fix a problem in string sanitization for \u0000 * sanitize csv values to remove \u0000 * Update db_sync.py Remove a print statement
1 parent dfc5b22 commit 7aa7c32

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

target_postgres/db_sync.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ def column_clause(name, schema_property):
4343
return '{} {}'.format(safe_column_name(name), column_type(schema_property))
4444

4545

46+
def sanitize(value):
47+
if not isinstance(value, str):
48+
return value
49+
50+
# this sequence will cause the CSV load to fail
51+
return value.replace("\\u0000", '')
52+
53+
4654
def flatten_key(k, parent_key, sep):
4755
full_key = parent_key + [k]
4856
inflected_key = [inflect_column_name(n) for n in full_key]
@@ -60,18 +68,24 @@ def flatten_schema(d, parent_key=[], sep='__'):
6068
items = []
6169
for k, v in d['properties'].items():
6270
new_key = flatten_key(k, parent_key, sep)
71+
72+
if not v:
73+
logger.warn("Empty definition for {}.".format(new_key))
74+
continue
75+
6376
if 'type' in v.keys():
6477
if 'object' in v['type']:
6578
items.extend(flatten_schema(v, parent_key + [k], sep=sep).items())
6679
else:
6780
items.append((new_key, v))
6881
else:
69-
if list(v.values())[0][0]['type'] == 'string':
70-
list(v.values())[0][0]['type'] = ['null', 'string']
71-
items.append((new_key, list(v.values())[0][0]))
72-
elif list(v.values())[0][0]['type'] == 'array':
73-
list(v.values())[0][0]['type'] = ['null', 'array']
74-
items.append((new_key, list(v.values())[0][0]))
82+
property = list(v.values())[0][0]
83+
if property['type'] == 'string':
84+
property['type'] = ['null', 'string']
85+
items.append((new_key, property))
86+
elif property['type'] == 'array':
87+
property['type'] = ['null', 'array']
88+
items.append((new_key, property))
7589

7690
key_func = lambda item: item[0]
7791
sorted_items = sorted(items, key=key_func)
@@ -150,7 +164,7 @@ def record_to_csv_line(self, record):
150164
flatten = flatten_record(record)
151165
return ','.join(
152166
[
153-
json.dumps(flatten[name]) if name in flatten and flatten[name] else ''
167+
json.dumps(sanitize(flatten[name])) if name in flatten and flatten[name] else ''
154168
for name in self.flatten_schema
155169
]
156170
)

0 commit comments

Comments
 (0)