todoappbackend-0jo333/alembic/versions/002_enhance_todo_features.py
Automated Action 4b391fe54b Enhance todo app with advanced features
- Add User model with email-based authentication
- Add Tag model with many-to-many relationship to todos
- Add TodoTag junction table for todo-tag relationships
- Enhance Todo model with priority levels (low, medium, high)
- Add due_date field with datetime support
- Add recurrence_pattern field for recurring todos
- Add parent-child relationship for subtasks support
- Create comprehensive alembic migration for all changes
- Add proper indexes for performance optimization
- Use Text type for todo descriptions
- Implement proper SQLAlchemy relationships and foreign keys
2025-06-19 12:49:26 +00:00

110 lines
4.5 KiB
Python

"""Enhance todo features with categories, priorities, users, and subtasks
Revision ID: 002
Revises: 001
Create Date: 2024-01-02 00:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '002'
down_revision = '001'
branch_labels = None
depends_on = None
def upgrade() -> None:
# Create users table
op.create_table('users',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('email', sa.String(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
# Create tags table
op.create_table('tags',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('color', sa.String(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_tags_id'), 'tags', ['id'], unique=False)
op.create_index(op.f('ix_tags_name'), 'tags', ['name'], unique=True)
# Create todo_tags junction table
op.create_table('todo_tags',
sa.Column('todo_id', sa.Integer(), nullable=False),
sa.Column('tag_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['tag_id'], ['tags.id'], ),
sa.ForeignKeyConstraint(['todo_id'], ['todos.id'], ),
sa.PrimaryKeyConstraint('todo_id', 'tag_id')
)
# Add new columns to todos table
op.add_column('todos', sa.Column('priority', sa.Enum('LOW', 'MEDIUM', 'HIGH', name='prioritylevel'), server_default='MEDIUM', nullable=False))
op.add_column('todos', sa.Column('due_date', sa.DateTime(timezone=True), nullable=True))
op.add_column('todos', sa.Column('recurrence_pattern', sa.Enum('NONE', 'DAILY', 'WEEKLY', 'MONTHLY', 'YEARLY', name='recurrencepattern'), server_default='NONE', nullable=False))
op.add_column('todos', sa.Column('user_id', sa.Integer(), nullable=False))
op.add_column('todos', sa.Column('parent_id', sa.Integer(), nullable=True))
# Change description column type to Text
op.alter_column('todos', 'description',
existing_type=sa.String(),
type_=sa.Text(),
existing_nullable=True)
# Create foreign key constraints
op.create_foreign_key('fk_todos_user_id', 'todos', 'users', ['user_id'], ['id'])
op.create_foreign_key('fk_todos_parent_id', 'todos', 'todos', ['parent_id'], ['id'])
# Create indexes for new columns
op.create_index(op.f('ix_todos_priority'), 'todos', ['priority'], unique=False)
op.create_index(op.f('ix_todos_due_date'), 'todos', ['due_date'], unique=False)
op.create_index(op.f('ix_todos_user_id'), 'todos', ['user_id'], unique=False)
op.create_index(op.f('ix_todos_parent_id'), 'todos', ['parent_id'], unique=False)
def downgrade() -> None:
# Drop indexes
op.drop_index(op.f('ix_todos_parent_id'), table_name='todos')
op.drop_index(op.f('ix_todos_user_id'), table_name='todos')
op.drop_index(op.f('ix_todos_due_date'), table_name='todos')
op.drop_index(op.f('ix_todos_priority'), table_name='todos')
# Drop foreign key constraints
op.drop_constraint('fk_todos_parent_id', 'todos', type_='foreignkey')
op.drop_constraint('fk_todos_user_id', 'todos', type_='foreignkey')
# Revert description column type back to String
op.alter_column('todos', 'description',
existing_type=sa.Text(),
type_=sa.String(),
existing_nullable=True)
# Drop new columns from todos table
op.drop_column('todos', 'parent_id')
op.drop_column('todos', 'user_id')
op.drop_column('todos', 'recurrence_pattern')
op.drop_column('todos', 'due_date')
op.drop_column('todos', 'priority')
# Drop todo_tags table
op.drop_table('todo_tags')
# Drop tags table
op.drop_index(op.f('ix_tags_name'), table_name='tags')
op.drop_index(op.f('ix_tags_id'), table_name='tags')
op.drop_table('tags')
# Drop users table
op.drop_index(op.f('ix_users_email'), table_name='users')
op.drop_index(op.f('ix_users_id'), table_name='users')
op.drop_table('users')