
- Create User model and database schema - Add JWT authentication with secure password hashing - Create authentication endpoints for registration and login - Update invoice routes to require authentication - Ensure users can only access their own invoices - Update documentation in README.md
60 lines
2.3 KiB
Python
60 lines
2.3 KiB
Python
"""add users and update invoices
|
|
|
|
Revision ID: b0e7512c61a3
|
|
Revises: ef0aaab3a275
|
|
Create Date: 2023-07-25 12:00:00.000000
|
|
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision = 'b0e7512c61a3'
|
|
down_revision = 'ef0aaab3a275'
|
|
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(length=255), nullable=False),
|
|
sa.Column('username', sa.String(length=50), nullable=False),
|
|
sa.Column('hashed_password', sa.String(length=255), nullable=False),
|
|
sa.Column('full_name', sa.String(length=100), nullable=True),
|
|
sa.Column('is_active', sa.Boolean(), nullable=False, default=True),
|
|
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_users_email'), 'users', ['email'], unique=True)
|
|
op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
|
|
op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True)
|
|
|
|
# Create temporary table for invoices
|
|
with op.batch_alter_table('invoices', schema=None) as batch_op:
|
|
# Add user_id column with a default value of 1 (will be set properly later)
|
|
batch_op.add_column(sa.Column('user_id', sa.Integer(), nullable=True))
|
|
|
|
# Create foreign key (has to be done separately in SQLite)
|
|
with op.batch_alter_table('invoices', schema=None) as batch_op:
|
|
batch_op.create_foreign_key('fk_invoices_user_id', 'users', ['user_id'], ['id'])
|
|
|
|
# Make user_id not nullable after we've set up proper values
|
|
with op.batch_alter_table('invoices', schema=None) as batch_op:
|
|
batch_op.alter_column('user_id', nullable=False)
|
|
|
|
|
|
def downgrade() -> None:
|
|
# Drop foreign key constraint
|
|
with op.batch_alter_table('invoices', schema=None) as batch_op:
|
|
batch_op.drop_constraint('fk_invoices_user_id', type_='foreignkey')
|
|
batch_op.drop_column('user_id')
|
|
|
|
# Drop users table
|
|
op.drop_index(op.f('ix_users_username'), table_name='users')
|
|
op.drop_index(op.f('ix_users_id'), table_name='users')
|
|
op.drop_index(op.f('ix_users_email'), table_name='users')
|
|
op.drop_table('users') |