Scaffold SQLAlchemy models, routers, and Alembic migrations
- app/models/: Printer, Filament, PrintJob, Todo, PricingConfig with full SQLAlchemy 2.x mapped columns and relationships - app/routers/: list + get-by-id endpoints for all five resources - app/db.py: SQLAlchemy engine + SessionLocal + get_db dependency - alembic/: env.py reads MYSQL_* env vars; 0001_initial_schema mirrors db.sql - alembic.ini: script_location configured, URL injected at runtime Since db.sql already created the tables, stamp the DB before running future migrations: alembic stamp head Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
54
alembic/env.py
Normal file
54
alembic/env.py
Normal file
@@ -0,0 +1,54 @@
|
||||
import os
|
||||
from logging.config import fileConfig
|
||||
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
|
||||
from alembic import context
|
||||
|
||||
config = context.config
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
from app.models.base import Base
|
||||
import app.models # noqa: F401 — registers all models with Base.metadata
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def get_url() -> str:
|
||||
return (
|
||||
f"mysql+pymysql://{os.environ['MYSQL_USER']}:{os.environ['MYSQL_PASSWORD']}"
|
||||
f"@{os.environ['MYSQL_HOST']}:{os.environ.get('MYSQL_PORT', 3306)}"
|
||||
f"/{os.environ['MYSQL_DATABASE']}"
|
||||
)
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
context.configure(
|
||||
url=get_url(),
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
config.set_main_option("sqlalchemy.url", get_url())
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
with connectable.connect() as connection:
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
25
alembic/script.py.mako
Normal file
25
alembic/script.py.mako
Normal file
@@ -0,0 +1,25 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, None] = ${repr(down_revision)}
|
||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
112
alembic/versions/0001_initial_schema.py
Normal file
112
alembic/versions/0001_initial_schema.py
Normal file
@@ -0,0 +1,112 @@
|
||||
"""initial schema
|
||||
|
||||
Revision ID: 0001
|
||||
Revises:
|
||||
Create Date: 2026-06-18
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy.dialects import mysql
|
||||
|
||||
revision: str = "0001"
|
||||
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:
|
||||
op.create_table(
|
||||
"printers",
|
||||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column("name", sa.String(length=100), nullable=False),
|
||||
sa.Column("model", sa.String(length=100), nullable=True),
|
||||
sa.Column("location", sa.String(length=100), nullable=True),
|
||||
sa.Column(
|
||||
"status",
|
||||
mysql.ENUM("idle", "printing", "error", "offline"),
|
||||
nullable=False,
|
||||
server_default="offline",
|
||||
),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.text("CURRENT_TIMESTAMP")),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False, server_default=sa.text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_table(
|
||||
"filaments",
|
||||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column("brand", sa.String(length=100), nullable=True),
|
||||
sa.Column(
|
||||
"material",
|
||||
mysql.ENUM("PLA", "PETG", "ABS", "ASA", "TPU", "Nylon", "PC", "Other"),
|
||||
nullable=False,
|
||||
server_default="PLA",
|
||||
),
|
||||
sa.Column("color", sa.String(length=100), nullable=True),
|
||||
sa.Column("color_hex", sa.String(length=7), nullable=True),
|
||||
sa.Column("weight_total_g", sa.Numeric(precision=8, scale=2), nullable=False),
|
||||
sa.Column("weight_remaining_g", sa.Numeric(precision=8, scale=2), nullable=False),
|
||||
sa.Column("price_per_kg_eur", sa.Numeric(precision=8, scale=2), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.text("CURRENT_TIMESTAMP")),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False, server_default=sa.text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_table(
|
||||
"pricing_config",
|
||||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column("name", sa.String(length=100), nullable=False, server_default="default"),
|
||||
sa.Column("electricity_kwh_eur", sa.Numeric(precision=6, scale=4), nullable=False, server_default="0.3000"),
|
||||
sa.Column("printer_watt", sa.Numeric(precision=8, scale=2), nullable=False, server_default="200.00"),
|
||||
sa.Column("hourly_rate_eur", sa.Numeric(precision=8, scale=2), nullable=False, server_default="0.00"),
|
||||
sa.Column("markup_percent", sa.Numeric(precision=5, scale=2), nullable=False, server_default="20.00"),
|
||||
sa.Column("valid_from", sa.Date(), nullable=False, server_default=sa.text("(CURRENT_DATE)")),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.text("CURRENT_TIMESTAMP")),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_table(
|
||||
"todos",
|
||||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column("title", sa.String(length=255), nullable=False),
|
||||
sa.Column("done", sa.Boolean(), nullable=False, server_default="0"),
|
||||
sa.Column("printer_id", sa.Integer(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.text("CURRENT_TIMESTAMP")),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False, server_default=sa.text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")),
|
||||
sa.ForeignKeyConstraint(["printer_id"], ["printers.id"], name="fk_todo_printer"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_table(
|
||||
"print_jobs",
|
||||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column("printer_id", sa.Integer(), nullable=False),
|
||||
sa.Column("filament_id", sa.Integer(), nullable=True),
|
||||
sa.Column("name", sa.String(length=200), nullable=False),
|
||||
sa.Column("file_name", sa.String(length=255), nullable=True),
|
||||
sa.Column(
|
||||
"status",
|
||||
mysql.ENUM("queued", "printing", "done", "failed", "cancelled"),
|
||||
nullable=False,
|
||||
server_default="queued",
|
||||
),
|
||||
sa.Column("queue_position", sa.Integer(), nullable=True),
|
||||
sa.Column("started_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("finished_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("duration_minutes", sa.Integer(), nullable=True),
|
||||
sa.Column("filament_used_g", sa.Numeric(precision=8, scale=2), nullable=True),
|
||||
sa.Column("cost_eur", sa.Numeric(precision=8, scale=2), nullable=True),
|
||||
sa.Column("notes", sa.Text(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.text("CURRENT_TIMESTAMP")),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False, server_default=sa.text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")),
|
||||
sa.ForeignKeyConstraint(["filament_id"], ["filaments.id"], name="fk_job_filament"),
|
||||
sa.ForeignKeyConstraint(["printer_id"], ["printers.id"], name="fk_job_printer"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table("print_jobs")
|
||||
op.drop_table("todos")
|
||||
op.drop_table("pricing_config")
|
||||
op.drop_table("filaments")
|
||||
op.drop_table("printers")
|
||||
Reference in New Issue
Block a user