Skip to content

Commit 0c199e4

Browse files
authored
Merge pull request #29 from mobuild-io/single_row_tracking
Use single row tracking
2 parents 3c36e01 + 8ef126b commit 0c199e4

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

eventsourcing_sqlalchemy/models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from sqlalchemy.orm import declarative_base
99
except ImportError:
1010
from sqlalchemy.ext.declarative import declarative_base
11+
1112
from sqlalchemy.orm import Mapped
1213
from sqlalchemy_utils.types.uuid import UUIDType
1314

@@ -87,9 +88,9 @@ class NotificationTrackingRecord(Base):
8788
__abstract__ = True
8889

8990
# Application name.
90-
application_name = Column(String(length=32), primary_key=True)
91+
application_name: Mapped[str] = Column(String(length=32), primary_key=True)
9192

9293
# Notification ID.
93-
notification_id = Column(
94-
BigInteger().with_variant(Integer(), "sqlite"), primary_key=True
94+
notification_id: Mapped[int] = Column(
95+
BigInteger().with_variant(Integer(), "sqlite")
9596
)

eventsourcing_sqlalchemy/recorders.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,15 @@ def _insert_tracking(self, session: Session, tracking: Tracking) -> None:
430430
tracking.application_name, tracking.notification_id
431431
):
432432
raise IntegrityError
433+
434+
existing = (
435+
session.query(self.tracking_record_cls)
436+
.filter_by(application_name=str(tracking.application_name))
437+
.first()
438+
)
439+
if existing:
440+
existing.notification_id = tracking.notification_id
441+
else:
433442
record = self.tracking_record_cls(
434443
application_name=tracking.application_name,
435444
notification_id=tracking.notification_id,

0 commit comments

Comments
 (0)