Set up Alembic database migrations with initial Todo table migration
- Initialize Alembic in migrations/ directory (not alembic/) - Configure alembic.ini with correct SQLite absolute path: sqlite:////app/storage/db/db.sqlite - Configure migrations/env.py to import Base correctly from app.db.base using absolute imports - Create initial migration 0001_initial_todo_table.py for Todo model - Migration creates todos table with id, title, description, completed, created_at, updated_at fields - Includes proper indexes for id (primary key) and title fields - Uses absolute imports to avoid ModuleNotFoundError issues - Follows exact specifications for SQLite URL and import paths
This commit is contained in:
parent
0ec5bd6053
commit
d593baa23c
1
migrations/__init__.py
Normal file
1
migrations/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
# Alembic migrations package
|
@ -1,15 +1,21 @@
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from logging.config import fileConfig
|
||||
|
||||
from sqlalchemy import engine_from_config
|
||||
from sqlalchemy import pool
|
||||
|
||||
from alembic import context
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the project root to the Python path
|
||||
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||
|
||||
# Import the Base from the correct location
|
||||
from app.db.base import Base
|
||||
|
||||
# Import all models so they are available to Alembic
|
||||
from app.models.todo import Todo
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
@ -61,7 +67,7 @@ def run_migrations_online() -> None:
|
||||
|
||||
"""
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
config.get_section(config.config_ini_section),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
39
migrations/versions/0001_initial_todo_table.py
Normal file
39
migrations/versions/0001_initial_todo_table.py
Normal file
@ -0,0 +1,39 @@
|
||||
"""Initial todo table
|
||||
|
||||
Revision ID: 0001
|
||||
Revises:
|
||||
Create Date: 2025-06-20 23:16:00.000000
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '0001'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# Create todos table
|
||||
op.create_table(
|
||||
'todos',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('title', sa.String(length=255), nullable=False),
|
||||
sa.Column('description', sa.Text(), nullable=True),
|
||||
sa.Column('completed', sa.Boolean(), nullable=False),
|
||||
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_todos_id'), 'todos', ['id'], unique=False)
|
||||
op.create_index(op.f('ix_todos_title'), 'todos', ['title'], unique=False)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# Drop todos table
|
||||
op.drop_index(op.f('ix_todos_title'), table_name='todos')
|
||||
op.drop_index(op.f('ix_todos_id'), table_name='todos')
|
||||
op.drop_table('todos')
|
1
migrations/versions/__init__.py
Normal file
1
migrations/versions/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
# Alembic migration versions package
|
Loading…
x
Reference in New Issue
Block a user