ecommerceapplication-i7hp4n/migrations/versions/2a3b4c5d6e7f_add_username_field.py
Automated Action da59077885 Switch from email to username for authentication
- Add username field to User model
- Update authentication endpoints to use username instead of email
- Create migration for adding username column to user table
- Update user services to handle username validation and uniqueness
- Maintain email for compatibility, but make username the primary identifier
2025-05-26 13:17:17 +00:00

42 lines
1.2 KiB
Python

"""Add username field
Revision ID: 2a3b4c5d6e7f
Revises: 1a2b3c4d5e6f
Create Date: 2023-11-17 00:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '2a3b4c5d6e7f'
down_revision = '1a2b3c4d5e6f'
branch_labels = None
depends_on = None
def upgrade() -> None:
# Add username column to user table
with op.batch_alter_table('user') as batch_op:
batch_op.add_column(sa.Column('username', sa.String(), nullable=True))
batch_op.create_index(op.f('ix_user_username'), 'username', unique=True)
# For existing users, initialize username from email
# This is just SQL template code - in a real migration with existing data,
# you would need to handle this appropriately
op.execute("""
UPDATE "user" SET username = email
WHERE username IS NULL
""")
# Now make username non-nullable for future records
with op.batch_alter_table('user') as batch_op:
batch_op.alter_column('username', nullable=False)
def downgrade() -> None:
# Remove username column from user table
with op.batch_alter_table('user') as batch_op:
batch_op.drop_index(op.f('ix_user_username'))
batch_op.drop_column('username')