125 lines
6.2 KiB
Python
125 lines
6.2 KiB
Python
"""Initial schema
|
|
|
|
Revision ID: 20260114_01
|
|
Revises: None
|
|
Create Date: 2026-01-14 00:00:00
|
|
"""
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
from sqlalchemy.dialects import postgresql
|
|
|
|
# Revision identifiers, used by Alembic.
|
|
revision = "20260114_01"
|
|
down_revision = None
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.create_table(
|
|
"products",
|
|
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
|
sa.Column("source", sa.String(length=50), nullable=False),
|
|
sa.Column("reference", sa.String(length=100), nullable=False),
|
|
sa.Column("url", sa.Text(), nullable=False),
|
|
sa.Column("title", sa.Text(), nullable=True),
|
|
sa.Column("category", sa.Text(), nullable=True),
|
|
sa.Column("currency", sa.String(length=3), nullable=True),
|
|
sa.Column("first_seen_at", sa.TIMESTAMP(), nullable=False),
|
|
sa.Column("last_updated_at", sa.TIMESTAMP(), nullable=False),
|
|
sa.UniqueConstraint("source", "reference", name="uq_product_source_reference"),
|
|
)
|
|
op.create_index("ix_product_source", "products", ["source"], unique=False)
|
|
op.create_index("ix_product_reference", "products", ["reference"], unique=False)
|
|
op.create_index("ix_product_last_updated", "products", ["last_updated_at"], unique=False)
|
|
|
|
op.create_table(
|
|
"price_history",
|
|
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
|
sa.Column("product_id", sa.Integer(), nullable=False),
|
|
sa.Column("price", sa.Numeric(10, 2), nullable=True),
|
|
sa.Column("shipping_cost", sa.Numeric(10, 2), nullable=True),
|
|
sa.Column("stock_status", sa.String(length=20), nullable=True),
|
|
sa.Column("fetch_method", sa.String(length=20), nullable=False),
|
|
sa.Column("fetch_status", sa.String(length=20), nullable=False),
|
|
sa.Column("fetched_at", sa.TIMESTAMP(), nullable=False),
|
|
sa.ForeignKeyConstraint(["product_id"], ["products.id"], ondelete="CASCADE"),
|
|
sa.UniqueConstraint("product_id", "fetched_at", name="uq_price_history_product_time"),
|
|
sa.CheckConstraint("stock_status IN ('in_stock', 'out_of_stock', 'unknown')"),
|
|
sa.CheckConstraint("fetch_method IN ('http', 'playwright')"),
|
|
sa.CheckConstraint("fetch_status IN ('success', 'partial', 'failed')"),
|
|
)
|
|
op.create_index("ix_price_history_product_id", "price_history", ["product_id"], unique=False)
|
|
op.create_index("ix_price_history_fetched_at", "price_history", ["fetched_at"], unique=False)
|
|
|
|
op.create_table(
|
|
"product_images",
|
|
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
|
sa.Column("product_id", sa.Integer(), nullable=False),
|
|
sa.Column("image_url", sa.Text(), nullable=False),
|
|
sa.Column("position", sa.Integer(), nullable=False),
|
|
sa.ForeignKeyConstraint(["product_id"], ["products.id"], ondelete="CASCADE"),
|
|
sa.UniqueConstraint("product_id", "image_url", name="uq_product_image_url"),
|
|
)
|
|
op.create_index("ix_product_image_product_id", "product_images", ["product_id"], unique=False)
|
|
|
|
op.create_table(
|
|
"product_specs",
|
|
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
|
sa.Column("product_id", sa.Integer(), nullable=False),
|
|
sa.Column("spec_key", sa.String(length=200), nullable=False),
|
|
sa.Column("spec_value", sa.Text(), nullable=False),
|
|
sa.ForeignKeyConstraint(["product_id"], ["products.id"], ondelete="CASCADE"),
|
|
sa.UniqueConstraint("product_id", "spec_key", name="uq_product_spec_key"),
|
|
)
|
|
op.create_index("ix_product_spec_product_id", "product_specs", ["product_id"], unique=False)
|
|
op.create_index("ix_product_spec_key", "product_specs", ["spec_key"], unique=False)
|
|
|
|
op.create_table(
|
|
"scraping_logs",
|
|
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
|
sa.Column("product_id", sa.Integer(), nullable=True),
|
|
sa.Column("url", sa.Text(), nullable=False),
|
|
sa.Column("source", sa.String(length=50), nullable=False),
|
|
sa.Column("reference", sa.String(length=100), nullable=True),
|
|
sa.Column("fetch_method", sa.String(length=20), nullable=False),
|
|
sa.Column("fetch_status", sa.String(length=20), nullable=False),
|
|
sa.Column("fetched_at", sa.TIMESTAMP(), nullable=False),
|
|
sa.Column("duration_ms", sa.Integer(), nullable=True),
|
|
sa.Column("html_size_bytes", sa.Integer(), nullable=True),
|
|
sa.Column("errors", postgresql.JSONB(), nullable=True),
|
|
sa.Column("notes", postgresql.JSONB(), nullable=True),
|
|
sa.ForeignKeyConstraint(["product_id"], ["products.id"], ondelete="SET NULL"),
|
|
sa.CheckConstraint("fetch_method IN ('http', 'playwright')"),
|
|
sa.CheckConstraint("fetch_status IN ('success', 'partial', 'failed')"),
|
|
)
|
|
op.create_index("ix_scraping_log_product_id", "scraping_logs", ["product_id"], unique=False)
|
|
op.create_index("ix_scraping_log_source", "scraping_logs", ["source"], unique=False)
|
|
op.create_index("ix_scraping_log_fetched_at", "scraping_logs", ["fetched_at"], unique=False)
|
|
op.create_index("ix_scraping_log_fetch_status", "scraping_logs", ["fetch_status"], unique=False)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_index("ix_scraping_log_fetch_status", table_name="scraping_logs")
|
|
op.drop_index("ix_scraping_log_fetched_at", table_name="scraping_logs")
|
|
op.drop_index("ix_scraping_log_source", table_name="scraping_logs")
|
|
op.drop_index("ix_scraping_log_product_id", table_name="scraping_logs")
|
|
op.drop_table("scraping_logs")
|
|
|
|
op.drop_index("ix_product_spec_key", table_name="product_specs")
|
|
op.drop_index("ix_product_spec_product_id", table_name="product_specs")
|
|
op.drop_table("product_specs")
|
|
|
|
op.drop_index("ix_product_image_product_id", table_name="product_images")
|
|
op.drop_table("product_images")
|
|
|
|
op.drop_index("ix_price_history_fetched_at", table_name="price_history")
|
|
op.drop_index("ix_price_history_product_id", table_name="price_history")
|
|
op.drop_table("price_history")
|
|
|
|
op.drop_index("ix_product_last_updated", table_name="products")
|
|
op.drop_index("ix_product_reference", table_name="products")
|
|
op.drop_index("ix_product_source", table_name="products")
|
|
op.drop_table("products")
|