
- Created User model and schemas - Implemented secure password hashing with bcrypt - Added JWT token-based authentication - Created user registration and login endpoints - Added authentication to todo routes - Updated todos to be associated with users - Created migration script for the user table - Updated documentation with auth information
63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
"""add users table and update todos table
|
|
|
|
Revision ID: 0002
|
|
Revises: 0001
|
|
Create Date: 2023-11-10
|
|
|
|
"""
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision = "0002"
|
|
down_revision = "0001"
|
|
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("username", sa.String(), nullable=False),
|
|
sa.Column("hashed_password", sa.String(), nullable=False),
|
|
sa.Column("is_active", sa.Boolean(), nullable=False, default=True),
|
|
sa.Column("is_superuser", sa.Boolean(), nullable=False, default=False),
|
|
sa.Column(
|
|
"created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()
|
|
),
|
|
sa.Column(
|
|
"updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()
|
|
),
|
|
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)
|
|
op.create_index(op.f("ix_users_username"), "users", ["username"], unique=True)
|
|
|
|
# Add owner_id column to todos table
|
|
with op.batch_alter_table("todos") as batch_op:
|
|
# First, create a default user to associate with existing todos
|
|
# For SQLite, we need to handle this differently
|
|
batch_op.add_column(sa.Column("owner_id", sa.Integer(), nullable=True))
|
|
|
|
# Create a foreign key constraint separately
|
|
with op.batch_alter_table("todos") as batch_op:
|
|
batch_op.create_foreign_key("fk_todos_owner_id_users", "users", ["owner_id"], ["id"])
|
|
|
|
|
|
def downgrade() -> None:
|
|
# Remove foreign key and owner_id column from todos table
|
|
with op.batch_alter_table("todos") as batch_op:
|
|
batch_op.drop_constraint("fk_todos_owner_id_users", type_="foreignkey")
|
|
batch_op.drop_column("owner_id")
|
|
|
|
# Drop users table
|
|
op.drop_index(op.f("ix_users_username"), table_name="users")
|
|
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")
|