Skip to content
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pip install singer-target-postgres
```bash
~/.virtualenvs/tap-something/bin/tap-something \
| ~/.virtualenvs/target-postgres/bin/target-postgres \
--config ~/singer.io/target_postgres_config.json
--config ~/singer.io/target_postgres_config.json >> state.json
```

### Config.json
Expand Down Expand Up @@ -93,7 +93,6 @@ _The above is copied from the [current list of versions](https://www.postgresql.

## Known Limitations

- Ignores `STATE` Singer messages.
- Requires a [JSON Schema](https://json-schema.org/) for every stream.
- Only string, string with date-time format, integer, number, boolean,
object, and array types with or without null are supported. Arrays can
Expand Down
10 changes: 7 additions & 3 deletions target_postgres/target_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def stream_to_target(stream, target, config={}):
:return: None
"""

state_writer = sys.stdout
streams = {}
try:
if not config.get('disable_collection', False):
Expand All @@ -55,7 +56,8 @@ def stream_to_target(stream, target, config={}):
invalid_records_threshold,
max_batch_rows,
max_batch_size,
line)
line,
state_writer)
if line_count > 0 and line_count % batch_detection_threshold == 0:
_flush_streams(streams, target)
line_count += 1
Expand Down Expand Up @@ -98,7 +100,7 @@ def _report_invalid_records(streams):


def _line_handler(streams, target, invalid_records_detect, invalid_records_threshold, max_batch_rows, max_batch_size,
line):
line, state_writer):
try:
line_data = json.loads(line)
except json.decoder.JSONDecodeError:
Expand Down Expand Up @@ -163,7 +165,9 @@ def _line_handler(streams, target, invalid_records_detect, invalid_records_thres
target.write_batch(stream_buffer)
target.activate_version(stream_buffer, line_data['version'])
elif line_data['type'] == 'STATE':
LOGGER.warning('`STATE` Singer message type not supported')
line = json.dumps(line_data['value'])
state_writer.write("{}\n".format(line))
state_writer.flush()
else:
raise TargetError('Unknown message type {} in message {}'.format(
line_data['type'],
Expand Down
17 changes: 17 additions & 0 deletions tests/test_target_tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from copy import deepcopy
import json

from unittest.mock import patch
import pytest
Expand Down Expand Up @@ -70,3 +71,19 @@ def test_loading__invalid__records__threshold():
target_tools.stream_to_target(InvalidCatStream(20), target, config=config)

assert len(target.calls['write_batch']) == 0


def test_state__capture(capsys):
stream = [
json.dumps({'type': 'STATE', 'value': { 'test': 'state-1' }}),
json.dumps({'type': 'STATE', 'value': { 'test': 'state-2' }})]

target_tools.stream_to_target(stream, Target())

out, _ = capsys.readouterr()

filtered_output = list(filter(None, out.split('\n')))

assert len(filtered_output) == 2
assert json.loads(filtered_output[0])['test'] == 'state-1'
assert json.loads(filtered_output[1])['test'] == 'state-2'