This commit is contained in:
@@ -0,0 +1 @@
|
||||
Generic single-database configuration.
|
||||
@@ -0,0 +1,80 @@
|
||||
from logging.config import fileConfig
|
||||
|
||||
from sqlalchemy import pool
|
||||
from sqlalchemy import engine_from_config
|
||||
|
||||
from alembic import context
|
||||
|
||||
# Import Base and models so Alembic can see all tables for autogenerate.
|
||||
from app.db import Base
|
||||
import app.models # noqa: F401 — registers Box, Item, SubItem on Base.metadata
|
||||
|
||||
config = context.config
|
||||
|
||||
# Dynamically set sqlalchemy.url from app config (not hardcoded in alembic.ini).
|
||||
# When called programmatically via app.migrate.run_migrations(), the URL is
|
||||
# already set on the Config object — respect it. Fall back to get_settings()
|
||||
# only when invoked from the ``alembic`` CLI.
|
||||
from app.config import get_settings
|
||||
|
||||
if not config.get_main_option("sqlalchemy.url"):
|
||||
settings = get_settings()
|
||||
config.set_main_option("sqlalchemy.url", settings.database_url)
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
"""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
render_as_batch=True,
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
"""
|
||||
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,
|
||||
render_as_batch=True,
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
@@ -0,0 +1,28 @@
|
||||
"""${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 identifiers, used by Alembic.
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, Sequence[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:
|
||||
"""Upgrade schema."""
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
${downgrades if downgrades else "pass"}
|
||||
@@ -0,0 +1,96 @@
|
||||
"""V1 baseline
|
||||
|
||||
Revision ID: 57af90893f55
|
||||
Revises:
|
||||
Create Date: 2026-06-01 13:49:15.867487
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '57af90893f55'
|
||||
down_revision: Union[str, Sequence[str], None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('boxes',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(length=100), nullable=False),
|
||||
sa.Column('note', sa.Text(), nullable=True),
|
||||
sa.Column('room', sa.String(length=100), nullable=True),
|
||||
sa.Column('status', sa.String(length=50), nullable=True),
|
||||
sa.Column('image_blob', sa.LargeBinary(), nullable=True),
|
||||
sa.Column('image_mime_type', sa.String(length=50), nullable=True),
|
||||
sa.Column('image_width', sa.Integer(), nullable=True),
|
||||
sa.Column('image_height', sa.Integer(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
with op.batch_alter_table('boxes', schema=None) as batch_op:
|
||||
batch_op.create_index(batch_op.f('ix_boxes_id'), ['id'], unique=False)
|
||||
|
||||
op.create_table('items',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('box_id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(length=100), nullable=False),
|
||||
sa.Column('note', sa.Text(), nullable=True),
|
||||
sa.Column('quantity', sa.Integer(), nullable=True),
|
||||
sa.Column('is_container', sa.Boolean(), nullable=False),
|
||||
sa.Column('image_blob', sa.LargeBinary(), nullable=True),
|
||||
sa.Column('image_mime_type', sa.String(length=50), nullable=True),
|
||||
sa.Column('image_width', sa.Integer(), nullable=True),
|
||||
sa.Column('image_height', sa.Integer(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(['box_id'], ['boxes.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
with op.batch_alter_table('items', schema=None) as batch_op:
|
||||
batch_op.create_index(batch_op.f('ix_items_id'), ['id'], unique=False)
|
||||
|
||||
op.create_table('subitems',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('parent_item_id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(length=100), nullable=False),
|
||||
sa.Column('note', sa.Text(), nullable=True),
|
||||
sa.Column('quantity', sa.Integer(), nullable=True),
|
||||
sa.Column('image_blob', sa.LargeBinary(), nullable=True),
|
||||
sa.Column('image_mime_type', sa.String(length=50), nullable=True),
|
||||
sa.Column('image_width', sa.Integer(), nullable=True),
|
||||
sa.Column('image_height', sa.Integer(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(['parent_item_id'], ['items.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
with op.batch_alter_table('subitems', schema=None) as batch_op:
|
||||
batch_op.create_index(batch_op.f('ix_subitems_id'), ['id'], unique=False)
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('subitems', schema=None) as batch_op:
|
||||
batch_op.drop_index(batch_op.f('ix_subitems_id'))
|
||||
|
||||
op.drop_table('subitems')
|
||||
with op.batch_alter_table('items', schema=None) as batch_op:
|
||||
batch_op.drop_index(batch_op.f('ix_items_id'))
|
||||
|
||||
op.drop_table('items')
|
||||
with op.batch_alter_table('boxes', schema=None) as batch_op:
|
||||
batch_op.drop_index(batch_op.f('ix_boxes_id'))
|
||||
|
||||
op.drop_table('boxes')
|
||||
# ### end Alembic commands ###
|
||||
Reference in New Issue
Block a user