Skip to content

Commit 7b9ee5f

Browse files
committed
Enable reading from $all streams
1 parent db3ca2b commit 7b9ee5f

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

CHANGELOG.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Release History
2+
===============
3+
4+
0.1.2
5+
-----
6+
7+
* Enable reading from all streams
8+
9+
0.1.1
10+
-----
11+
12+
* Enable editable installation with Poetry
13+
* Return data as json instead of string
14+
15+
0.1.0
16+
-----
17+
18+
* First Release

message_db/client.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ def _write(
7575

7676
result = cursor.fetchone()
7777
except Exception as exc:
78-
raise ValueError(exc.args[0].splitlines()[0]) from exc
78+
raise ValueError(
79+
f"{getattr(exc, 'pgcode')}-{getattr(exc, 'pgerror').splitlines()[0]}"
80+
) from exc
7981

8082
return result["write_message"]
8183

@@ -131,7 +133,24 @@ def read(
131133
cursor = conn.cursor(cursor_factory=RealDictCursor)
132134

133135
if not sql:
134-
if "-" in stream_name:
136+
if stream_name == "$all":
137+
sql = """
138+
SELECT
139+
id::varchar,
140+
stream_name::varchar,
141+
type::varchar,
142+
position::bigint,
143+
global_position::bigint,
144+
data::varchar,
145+
metadata::varchar,
146+
time::timestamp
147+
FROM
148+
messages
149+
WHERE
150+
global_position > %(position)s
151+
LIMIT %(batch_size)s
152+
"""
153+
elif "-" in stream_name:
135154
sql = "SELECT * FROM get_stream_messages(%(stream_name)s, %(position)s, %(batch_size)s);"
136155
else:
137156
sql = "SELECT * FROM get_category_messages(%(stream_name)s, %(position)s, %(batch_size)s);"

tests/test_client.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_that_write_fails_on_expected_version_mismatch(self, client):
7878

7979
assert (
8080
exc.value.args[0]
81-
== "Wrong expected version: 1 (Stream: testStream-123, Stream Version: 2)"
81+
== "P0001-ERROR: Wrong expected version: 1 (Stream: testStream-123, Stream Version: 2)"
8282
)
8383

8484

@@ -139,6 +139,28 @@ def test_read_category_messages(self, client):
139139
assert messages[4]["data"] == {"foo": "bar4"}
140140

141141

142+
class TestReadAll:
143+
def test_reading_all_streams(self, client):
144+
client.write("stream1-123", "Event1", {"foo": "bar"})
145+
client.write("stream2-123", "Event2", {"foo": "bar"})
146+
147+
messages = client.read("$all")
148+
assert len(messages) == 2
149+
150+
def test_reading_all_streams_after_position(self, client):
151+
for i in range(5):
152+
client.write("stream1-123", f"Event1{i+1}", {"foo": "bar"})
153+
for i in range(5):
154+
client.write("stream2-123", f"Event2{i+1}", {"foo": "bar"})
155+
156+
messages = client.read("$all")
157+
assert len(messages) == 10
158+
messages = client.read("$all", position=5)
159+
assert len(messages) == 5
160+
messages = client.read("$all", position=5, no_of_messages=2)
161+
assert len(messages) == 2
162+
163+
142164
class TestReadStream:
143165
def test_reading_a_category_throws_error(self, client):
144166
with pytest.raises(ValueError) as exc:

0 commit comments

Comments
 (0)