Skip to content

Commit

Permalink
Adjusted reading of results when returning notification IDs after ins…
Browse files Browse the repository at this point in the history
…erting events - seems the NOTIFY doesn't appear in exactly the same place with Postgres v13?
  • Loading branch information
johnbywater committed Feb 4, 2025
1 parent 820b1b6 commit ee93f43
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
13 changes: 4 additions & 9 deletions eventsourcing/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,19 +488,14 @@ def _fetch_ids_after_insert_events(
notification_ids: List[int] = []
len_events = len(stored_events)
if len_events:
if (
(c.statusmessage == "SET")
and c.nextset()
and (c.statusmessage == "LOCK TABLE")
and c.nextset()
and (c.statusmessage == "NOTIFY")
):
while c.nextset() and len(notification_ids) != len_events:
while c.nextset() and len(notification_ids) != len_events:
if c.statusmessage and c.statusmessage.startswith("INSERT"):
row = c.fetchone()
assert row is not None
notification_ids.append(row["notification_id"])
if len(notification_ids) != len(stored_events):
msg = "Couldn't get all notification IDs"
msg = "Couldn't get all notification IDs "
msg += f"(got {len(notification_ids)}, expected {len(stored_events)}"
raise ProgrammingError(msg)
return notification_ids

Expand Down
5 changes: 2 additions & 3 deletions tests/persistence_tests/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,6 @@ def make_events() -> List[StoredEvent]:
)
]

#
# Check it actually works.
recorder = PostgresApplicationRecorder(
datastore=self.datastore, events_table_name=EVENTS_TABLE_NAME
Expand All @@ -769,13 +768,13 @@ def make_events() -> List[StoredEvent]:
self.assertEqual(len(notification_ids), 1)
self.assertEqual(max_notification_id + 1, notification_ids[0])

# Events but no lock table statements.
# Insert statement has no RETURNING clause.
with self.assertRaises(ProgrammingError):
recorder = PostgresApplicationRecorder(
datastore=self.datastore, events_table_name=EVENTS_TABLE_NAME
)
recorder.insert_events_statement = recorder.insert_events_statement.partition("RETURNING")[0]
recorder.create_table()
recorder.lock_table_statements = []
recorder.insert_events(make_events())


Expand Down

0 comments on commit ee93f43

Please sign in to comment.