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>
79 lines
2.0 KiB
Python
79 lines
2.0 KiB
Python
"""Add cart_items table for shopping cart"""
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision = "0002_add_cart_items"
|
|
down_revision = "0001_initial_schema"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.create_table(
|
|
"cart_items",
|
|
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
|
|
|
# Either a logged-in user *or* an anonymous session_id
|
|
sa.Column(
|
|
"user_id",
|
|
sa.Integer(),
|
|
sa.ForeignKey("users.id", ondelete="CASCADE"),
|
|
nullable=True,
|
|
),
|
|
sa.Column("session_id", sa.String(length=128), nullable=True),
|
|
|
|
# IMPORTANT: reference products.id (PK), not slug
|
|
sa.Column(
|
|
"product_id",
|
|
sa.Integer(),
|
|
sa.ForeignKey("products.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
),
|
|
|
|
sa.Column(
|
|
"quantity",
|
|
sa.Integer(),
|
|
nullable=False,
|
|
server_default="1",
|
|
),
|
|
sa.Column(
|
|
"created_at",
|
|
sa.DateTime(timezone=True),
|
|
nullable=False,
|
|
server_default=sa.text("now()"),
|
|
),
|
|
sa.Column(
|
|
"updated_at",
|
|
sa.DateTime(timezone=True),
|
|
nullable=False,
|
|
server_default=sa.text("now()"),
|
|
),
|
|
sa.Column(
|
|
"deleted_at",
|
|
sa.DateTime(timezone=True),
|
|
nullable=True,
|
|
),
|
|
)
|
|
|
|
# Indexes to speed up cart lookups
|
|
op.create_index(
|
|
"ix_cart_items_user_product",
|
|
"cart_items",
|
|
["user_id", "product_id"],
|
|
unique=False,
|
|
)
|
|
op.create_index(
|
|
"ix_cart_items_session_product",
|
|
"cart_items",
|
|
["session_id", "product_id"],
|
|
unique=False,
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_index("ix_cart_items_session_product", table_name="cart_items")
|
|
op.drop_index("ix_cart_items_user_product", table_name="cart_items")
|
|
op.drop_table("cart_items")
|