animemerchandisecatalogapi-.../migrations/versions/20231216_000000_initial_schema.py

88 lines
3.1 KiB
Python

"""initial schema
Revision ID: 20231216_000000
Revises:
Create Date: 2023-12-16 00:00:00.000000
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = "20231216_000000"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Create category table
op.create_table(
"category",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.Column("name", sa.String(length=100), nullable=False),
sa.Column("description", sa.Text(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(op.f("ix_category_id"), "category", ["id"], unique=False)
op.create_index(op.f("ix_category_name"), "category", ["name"], unique=True)
# Create product table
op.create_table(
"product",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.Column("name", sa.String(length=255), nullable=False),
sa.Column("description", sa.Text(), nullable=True),
sa.Column("price", sa.Float(), nullable=False),
sa.Column("stock", sa.Integer(), nullable=False),
sa.Column("image_url", sa.String(length=255), nullable=True),
sa.Column("anime_title", sa.String(length=255), nullable=True),
sa.Column("character_name", sa.String(length=255), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(
op.f("ix_product_anime_title"), "product", ["anime_title"], unique=False
)
op.create_index(
op.f("ix_product_character_name"), "product", ["character_name"], unique=False
)
op.create_index(op.f("ix_product_id"), "product", ["id"], unique=False)
op.create_index(op.f("ix_product_name"), "product", ["name"], unique=False)
# Create product_category association table
op.create_table(
"product_category",
sa.Column("product_id", sa.Integer(), nullable=False),
sa.Column("category_id", sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(
["category_id"],
["category.id"],
),
sa.ForeignKeyConstraint(
["product_id"],
["product.id"],
),
sa.PrimaryKeyConstraint("product_id", "category_id"),
)
def downgrade() -> None:
# Drop tables in reverse order
op.drop_table("product_category")
op.drop_index(op.f("ix_product_name"), table_name="product")
op.drop_index(op.f("ix_product_id"), table_name="product")
op.drop_index(op.f("ix_product_character_name"), table_name="product")
op.drop_index(op.f("ix_product_anime_title"), table_name="product")
op.drop_table("product")
op.drop_index(op.f("ix_category_name"), table_name="category")
op.drop_index(op.f("ix_category_id"), table_name="category")
op.drop_table("category")