aboutsummaryrefslogtreecommitdiff
path: root/app/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/models.py')
-rw-r--r--app/models.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/app/models.py b/app/models.py
new file mode 100644
index 0000000..34bc160
--- /dev/null
+++ b/app/models.py
@@ -0,0 +1,72 @@
+from datetime import datetime
+from sqlalchemy import String, Boolean, Integer, DateTime, ForeignKey, Text, UniqueConstraint
+from sqlalchemy.orm import Mapped, mapped_column, relationship
+from app.db import Base
+
+
+class Device(Base):
+ __tablename__ = "devices"
+ __table_args__ = (UniqueConstraint("apns_token", "bundle_id", name="uq_device_token_bundle"),)
+
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
+ user_id: Mapped[str] = mapped_column(String(255), index=True)
+ apns_token: Mapped[str] = mapped_column(Text)
+ apns_env: Mapped[str] = mapped_column(String(32))
+ bundle_id: Mapped[str] = mapped_column(String(255))
+ platform: Mapped[str] = mapped_column(String(32), default="ios")
+ app_version: Mapped[str | None] = mapped_column(String(64), nullable=True)
+ device_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
+ is_enabled: Mapped[bool] = mapped_column(Boolean, default=True)
+ last_seen_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
+ created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
+ updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
+
+ subscriptions = relationship("Subscription", back_populates="device", cascade="all, delete-orphan")
+
+
+class Subscription(Base):
+ __tablename__ = "subscriptions"
+
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
+ device_id: Mapped[int] = mapped_column(ForeignKey("devices.id", ondelete="CASCADE"), index=True)
+ source_type: Mapped[str] = mapped_column(String(64), index=True)
+ source_id: Mapped[str] = mapped_column(String(255), index=True)
+ event_type: Mapped[str] = mapped_column(String(64), index=True)
+ is_enabled: Mapped[bool] = mapped_column(Boolean, default=True)
+ created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
+ updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
+
+ device = relationship("Device", back_populates="subscriptions")
+
+
+class Event(Base):
+ __tablename__ = "events"
+ __table_args__ = (UniqueConstraint("dedupe_key", name="uq_event_dedupe_key"),)
+
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
+ source_type: Mapped[str] = mapped_column(String(64), index=True)
+ source_id: Mapped[str] = mapped_column(String(255), index=True)
+ event_type: Mapped[str] = mapped_column(String(64), index=True)
+ external_id: Mapped[str] = mapped_column(String(255), index=True)
+ dedupe_key: Mapped[str] = mapped_column(String(255))
+ title: Mapped[str] = mapped_column(String(255))
+ body: Mapped[str] = mapped_column(Text)
+ deep_link: Mapped[str | None] = mapped_column(String(512), nullable=True)
+ payload_json: Mapped[str | None] = mapped_column(Text, nullable=True)
+ created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
+
+
+class Delivery(Base):
+ __tablename__ = "deliveries"
+
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
+ event_id: Mapped[int] = mapped_column(ForeignKey("events.id", ondelete="CASCADE"), index=True)
+ device_id: Mapped[int] = mapped_column(ForeignKey("devices.id", ondelete="CASCADE"), index=True)
+ status: Mapped[str] = mapped_column(String(32), default="pending", index=True)
+ attempt_count: Mapped[int] = mapped_column(Integer, default=0)
+ last_error: Mapped[str | None] = mapped_column(Text, nullable=True)
+ apns_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
+ scheduled_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
+ sent_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
+ created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
+ updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)