Automated Action a9210ca8ed Create manga inventory API with FastAPI and SQLite
- Implemented CRUD operations for manga, authors, publishers, and genres
- Added search and filtering functionality
- Set up SQLAlchemy ORM with SQLite database
- Configured Alembic for database migrations
- Implemented logging with Loguru
- Added comprehensive API documentation
- Set up error handling and validation
- Added ruff for linting and formatting
2025-05-30 12:29:35 +00:00

132 lines
5.2 KiB
Python

"""Initial tables
Revision ID: 01_initial_tables
Revises:
Create Date: 2023-11-10
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "01_initial_tables"
down_revision: str | None = None
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
# Create author table
op.create_table(
"author",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.String(length=100), nullable=False),
sa.Column("biography", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(op.f("ix_author_id"), "author", ["id"], unique=False)
op.create_index(op.f("ix_author_name"), "author", ["name"], unique=False)
# Create publisher table
op.create_table(
"publisher",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.String(length=100), nullable=False),
sa.Column("website", sa.String(length=255), nullable=True),
sa.Column("country", sa.String(length=50), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(op.f("ix_publisher_id"), "publisher", ["id"], unique=False)
op.create_index(op.f("ix_publisher_name"), "publisher", ["name"], unique=False)
# Create genre table
op.create_table(
"genre",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.String(length=50), nullable=False),
sa.Column("description", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(op.f("ix_genre_id"), "genre", ["id"], unique=False)
op.create_index(op.f("ix_genre_name"), "genre", ["name"], unique=True)
# Create manga table
op.create_table(
"manga",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("title", sa.String(length=255), nullable=False),
sa.Column("original_title", sa.String(length=255), nullable=True),
sa.Column("isbn", sa.String(length=20), nullable=True),
sa.Column("description", sa.Text(), nullable=True),
sa.Column("volume_number", sa.Integer(), nullable=True),
sa.Column("total_volumes", sa.Integer(), nullable=True),
sa.Column("author_id", sa.Integer(), nullable=True),
sa.Column("publisher_id", sa.Integer(), nullable=True),
sa.Column("publication_date", sa.DateTime(), nullable=True),
sa.Column("page_count", sa.Integer(), nullable=True),
sa.Column("price", sa.Float(), nullable=True),
sa.Column("quantity", sa.Integer(), nullable=False, server_default="0"),
sa.Column("in_stock", sa.Boolean(), nullable=False, server_default="1"),
sa.Column("rating", sa.Float(), nullable=True),
sa.Column("language", sa.String(length=50), nullable=True),
sa.Column("cover_image_url", sa.String(length=255), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(
["author_id"],
["author.id"],
),
sa.ForeignKeyConstraint(
["publisher_id"],
["publisher.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(op.f("ix_manga_id"), "manga", ["id"], unique=False)
op.create_index(op.f("ix_manga_isbn"), "manga", ["isbn"], unique=True)
op.create_index(op.f("ix_manga_title"), "manga", ["title"], unique=False)
# Create manga_genre table (junction table)
op.create_table(
"manga_genre",
sa.Column("manga_id", sa.Integer(), nullable=False),
sa.Column("genre_id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(
["genre_id"],
["genre.id"],
),
sa.ForeignKeyConstraint(
["manga_id"],
["manga.id"],
),
sa.PrimaryKeyConstraint("manga_id", "genre_id"),
)
def downgrade() -> None:
# Drop tables in reverse order
op.drop_table("manga_genre")
op.drop_index(op.f("ix_manga_title"), table_name="manga")
op.drop_index(op.f("ix_manga_isbn"), table_name="manga")
op.drop_index(op.f("ix_manga_id"), table_name="manga")
op.drop_table("manga")
op.drop_index(op.f("ix_genre_name"), table_name="genre")
op.drop_index(op.f("ix_genre_id"), table_name="genre")
op.drop_table("genre")
op.drop_index(op.f("ix_publisher_name"), table_name="publisher")
op.drop_index(op.f("ix_publisher_id"), table_name="publisher")
op.drop_table("publisher")
op.drop_index(op.f("ix_author_name"), table_name="author")
op.drop_index(op.f("ix_author_id"), table_name="author")
op.drop_table("author")