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

Fix multiple issues #15

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
py_modules=["target_postgres"],
install_requires=[
"singer-python==5.1.1",
"psycopg2==2.7.5",
"psycopg2==2.8.4",
"inflection==0.3.1"
],
entry_points="""
Expand Down
19 changes: 16 additions & 3 deletions target_postgres/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@
from tempfile import TemporaryFile

import pkg_resources
from jsonschema.validators import Draft4Validator
from jsonschema import validators, Draft4Validator, FormatChecker
from decimal import Decimal
import singer
from target_postgres.db_sync import DbSync

logger = singer.get_logger()


def float_to_decimal(value):
'''Walk the given data structure and turn all instances of float into
double.'''
if isinstance(value, float):
return Decimal(str(value))
if isinstance(value, list):
return [float_to_decimal(child) for child in value]
if isinstance(value, dict):
return {k: float_to_decimal(v) for k, v in value.items()}
return value

def emit_state(state):
if state is not None:
line = json.dumps(state)
Expand Down Expand Up @@ -65,7 +77,7 @@ def persist_lines(config, lines):
stream = o['stream']

# Validate record
validators[stream].validate(o['record'])
validators[stream].validate(float_to_decimal(o['record']))

sync = stream_to_sync[stream]

Expand Down Expand Up @@ -93,7 +105,8 @@ def persist_lines(config, lines):
raise Exception("Line is missing required key 'stream': {}".format(line))
stream = o['stream']
schemas[stream] = o
validators[stream] = Draft4Validator(o['schema'])
schema = float_to_decimal(o['schema'])
validators[stream] = Draft4Validator(schema, format_checker=FormatChecker())
if 'key_properties' not in o:
raise Exception("key_properties field is required")
key_properties[stream] = o['key_properties']
Expand Down
2 changes: 1 addition & 1 deletion target_postgres/db_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def record_to_csv_line(self, record):
flatten = flatten_record(record)
return ','.join(
[
json.dumps(flatten[name]) if name in flatten and flatten[name] else ''
json.dumps(flatten[name], ensure_ascii=False) if name in flatten and (flatten[name] == 0 or flatten[name]) else ''
for name in self.flatten_schema
]
)
Expand Down