feat: extract shared infrastructure from shared_lib
Phase 1-3 of decoupling plan: - Shared DB, models, infrastructure, browser, config, utils - Event infrastructure (domain_events outbox, bus, processor) - Structured logging - Generic container concept (container_type/container_id) - Alembic migrations for all schema changes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
11
models/__init__.py
Normal file
11
models/__init__.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from .user import User
|
||||
from .kv import KV
|
||||
from .magic_link import MagicLink
|
||||
from .menu_item import MenuItem
|
||||
|
||||
from .ghost_membership_entities import (
|
||||
GhostLabel, UserLabel,
|
||||
GhostNewsletter, UserNewsletter,
|
||||
GhostTier, GhostSubscription,
|
||||
)
|
||||
from .domain_event import DomainEvent
|
||||
30
models/domain_event.py
Normal file
30
models/domain_event.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from sqlalchemy import String, Integer, DateTime, Text, func
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
from shared.db.base import Base
|
||||
|
||||
|
||||
class DomainEvent(Base):
|
||||
__tablename__ = "domain_events"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
event_type: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
|
||||
aggregate_type: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
aggregate_id: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
payload: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
|
||||
state: Mapped[str] = mapped_column(
|
||||
String(20), nullable=False, default="pending", server_default="pending", index=True
|
||||
)
|
||||
attempts: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default="0")
|
||||
max_attempts: Mapped[int] = mapped_column(Integer, nullable=False, default=5, server_default="5")
|
||||
last_error: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), nullable=False, server_default=func.now()
|
||||
)
|
||||
processed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<DomainEvent {self.id} {self.event_type} [{self.state}]>"
|
||||
122
models/ghost_membership_entities.py
Normal file
122
models/ghost_membership_entities.py
Normal file
@@ -0,0 +1,122 @@
|
||||
# suma_browser/models/ghost_membership_entities.py
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import (
|
||||
Integer, String, Text, Boolean, DateTime, ForeignKey, UniqueConstraint
|
||||
)
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy.ext.associationproxy import association_proxy
|
||||
|
||||
from shared.db.base import Base
|
||||
|
||||
|
||||
# -----------------------
|
||||
# Labels (simple M2M)
|
||||
# -----------------------
|
||||
|
||||
class GhostLabel(Base):
|
||||
__tablename__ = "ghost_labels"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
ghost_id: Mapped[str] = mapped_column(String(64), unique=True, index=True, nullable=False)
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
slug: Mapped[Optional[str]] = mapped_column(String(255))
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, default=datetime.utcnow)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, default=datetime.utcnow)
|
||||
|
||||
# Back-populated by User.labels
|
||||
users = relationship("User", secondary="user_labels", back_populates="labels", lazy="selectin")
|
||||
|
||||
|
||||
class UserLabel(Base):
|
||||
__tablename__ = "user_labels"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True)
|
||||
label_id: Mapped[int] = mapped_column(ForeignKey("ghost_labels.id", ondelete="CASCADE"), index=True)
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint("user_id", "label_id", name="uq_user_label"),
|
||||
)
|
||||
|
||||
|
||||
# -----------------------
|
||||
# Newsletters (association object + proxy)
|
||||
# -----------------------
|
||||
|
||||
class GhostNewsletter(Base):
|
||||
__tablename__ = "ghost_newsletters"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
ghost_id: Mapped[str] = mapped_column(String(64), unique=True, index=True, nullable=False)
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
slug: Mapped[Optional[str]] = mapped_column(String(255))
|
||||
description: Mapped[Optional[str]] = mapped_column(Text)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, default=datetime.utcnow)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, default=datetime.utcnow)
|
||||
|
||||
# Association-object side (one-to-many)
|
||||
user_newsletters = relationship(
|
||||
"UserNewsletter",
|
||||
back_populates="newsletter",
|
||||
cascade="all, delete-orphan",
|
||||
lazy="selectin",
|
||||
)
|
||||
|
||||
# Convenience: list-like proxy of Users via association rows (read-only container)
|
||||
users = association_proxy("user_newsletters", "user")
|
||||
|
||||
|
||||
class UserNewsletter(Base):
|
||||
__tablename__ = "user_newsletters"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True)
|
||||
newsletter_id: Mapped[int] = mapped_column(ForeignKey("ghost_newsletters.id", ondelete="CASCADE"), index=True)
|
||||
subscribed: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint("user_id", "newsletter_id", name="uq_user_newsletter"),
|
||||
)
|
||||
|
||||
# Bidirectional links for the association object
|
||||
user = relationship("User", back_populates="user_newsletters", lazy="selectin")
|
||||
newsletter = relationship("GhostNewsletter", back_populates="user_newsletters", lazy="selectin")
|
||||
|
||||
|
||||
# -----------------------
|
||||
# Tiers & Subscriptions
|
||||
# -----------------------
|
||||
|
||||
class GhostTier(Base):
|
||||
__tablename__ = "ghost_tiers"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
ghost_id: Mapped[str] = mapped_column(String(64), unique=True, index=True, nullable=False)
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
slug: Mapped[Optional[str]] = mapped_column(String(255))
|
||||
type: Mapped[Optional[str]] = mapped_column(String(50)) # e.g. free, paid
|
||||
visibility: Mapped[Optional[str]] = mapped_column(String(50))
|
||||
|
||||
|
||||
class GhostSubscription(Base):
|
||||
__tablename__ = "ghost_subscriptions"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
ghost_id: Mapped[str] = mapped_column(String(64), unique=True, index=True, nullable=False)
|
||||
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True)
|
||||
status: Mapped[Optional[str]] = mapped_column(String(50))
|
||||
tier_id: Mapped[Optional[int]] = mapped_column(ForeignKey("ghost_tiers.id", ondelete="SET NULL"), index=True)
|
||||
cadence: Mapped[Optional[str]] = mapped_column(String(50)) # month, year
|
||||
price_amount: Mapped[Optional[int]] = mapped_column(Integer)
|
||||
price_currency: Mapped[Optional[str]] = mapped_column(String(10))
|
||||
stripe_customer_id: Mapped[Optional[str]] = mapped_column(String(255), index=True)
|
||||
stripe_subscription_id: Mapped[Optional[str]] = mapped_column(String(255), index=True)
|
||||
raw: Mapped[Optional[dict]] = mapped_column(JSONB, nullable=True)
|
||||
|
||||
# Relationships
|
||||
user = relationship("User", back_populates="subscriptions", lazy="selectin")
|
||||
tier = relationship("GhostTier", lazy="selectin")
|
||||
12
models/kv.py
Normal file
12
models/kv.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from __future__ import annotations
|
||||
from datetime import datetime
|
||||
from sqlalchemy import String, Text, DateTime
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
from shared.db.base import Base
|
||||
|
||||
class KV(Base):
|
||||
__tablename__ = "kv"
|
||||
"""Simple key-value table for settings/cache/demo."""
|
||||
key: Mapped[str] = mapped_column(String(120), primary_key=True)
|
||||
value: Mapped[str | None] = mapped_column(Text(), nullable=True)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
25
models/magic_link.py
Normal file
25
models/magic_link.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from __future__ import annotations
|
||||
from datetime import datetime
|
||||
from sqlalchemy import String, Integer, DateTime, ForeignKey, func, Index
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from shared.db.base import Base
|
||||
|
||||
class MagicLink(Base):
|
||||
__tablename__ = "magic_links"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
token: Mapped[str] = mapped_column(String(128), unique=True, index=True, nullable=False)
|
||||
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True)
|
||||
purpose: Mapped[str] = mapped_column(String(32), nullable=False, default="signin")
|
||||
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
used_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
||||
ip: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
user_agent: Mapped[str | None] = mapped_column(String(256), nullable=True)
|
||||
|
||||
user = relationship("User", backref="magic_links")
|
||||
|
||||
__table_args__ = (
|
||||
Index("ix_magic_link_token", "token", unique=True),
|
||||
Index("ix_magic_link_user", "user_id"),
|
||||
)
|
||||
42
models/menu_item.py
Normal file
42
models/menu_item.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy import Integer, String, DateTime, ForeignKey, func
|
||||
from shared.db.base import Base
|
||||
|
||||
|
||||
class MenuItem(Base):
|
||||
__tablename__ = "menu_items"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
|
||||
# Foreign key to posts table
|
||||
post_id: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
ForeignKey("posts.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True
|
||||
)
|
||||
|
||||
# Order for sorting menu items
|
||||
sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0, index=True)
|
||||
|
||||
# Timestamps
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
server_default=func.now(),
|
||||
nullable=False
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
server_default=func.now(),
|
||||
onupdate=func.now(),
|
||||
nullable=False
|
||||
)
|
||||
deleted_at: Mapped[Optional[datetime]] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
nullable=True
|
||||
)
|
||||
|
||||
# Relationship to Post
|
||||
post: Mapped["Post"] = relationship("Post", back_populates="menu_items")
|
||||
46
models/user.py
Normal file
46
models/user.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from __future__ import annotations
|
||||
from datetime import datetime
|
||||
from sqlalchemy import String, Integer, DateTime, func, Index, Text, Boolean
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy.ext.associationproxy import association_proxy
|
||||
from shared.db.base import Base
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
email: Mapped[str] = mapped_column(String(255), unique=True, index=True, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
||||
last_login_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
|
||||
# Ghost membership linkage
|
||||
ghost_id: Mapped[str | None] = mapped_column(String(64), unique=True, index=True, nullable=True)
|
||||
name: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
ghost_status: Mapped[str | None] = mapped_column(String(50), nullable=True) # free, paid, comped
|
||||
ghost_subscribed: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=func.true())
|
||||
ghost_note: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
avatar_image: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
stripe_customer_id: Mapped[str | None] = mapped_column(String(255), index=True, nullable=True)
|
||||
ghost_raw: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
|
||||
|
||||
# Relationships to Ghost-related entities
|
||||
|
||||
user_newsletters = relationship("UserNewsletter", back_populates="user", cascade="all, delete-orphan", lazy="selectin")
|
||||
newsletters = association_proxy("user_newsletters", "newsletter")
|
||||
labels = relationship("GhostLabel", secondary="user_labels", back_populates="users", lazy="selectin")
|
||||
subscriptions = relationship("GhostSubscription", back_populates="user", cascade="all, delete-orphan", lazy="selectin")
|
||||
|
||||
liked_products = relationship("ProductLike", back_populates="user", cascade="all, delete-orphan")
|
||||
liked_posts = relationship("PostLike", back_populates="user", cascade="all, delete-orphan")
|
||||
cart_items = relationship(
|
||||
"CartItem",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
)
|
||||
|
||||
__table_args__ = (
|
||||
Index("ix_user_email", "email", unique=True),
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<User {self.id} {self.email}>"
|
||||
Reference in New Issue
Block a user