diff --git a/migrations/versions/2a3b4c5d6e7f_add_username_field.py b/migrations/versions/2a3b4c5d6e7f_add_username_field.py index 0bf25ab..bf70a08 100644 --- a/migrations/versions/2a3b4c5d6e7f_add_username_field.py +++ b/migrations/versions/2a3b4c5d6e7f_add_username_field.py @@ -18,10 +18,8 @@ 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('ix_user_username', ['username'], unique=True) - + op.add_column('user', sa.Column('username', sa.String(), nullable=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 @@ -30,13 +28,14 @@ def upgrade() -> None: WHERE username IS NULL """) - # Now make username non-nullable for future records + # Now make username non-nullable for future records and add index with op.batch_alter_table('user') as batch_op: batch_op.alter_column('username', nullable=False) + batch_op.create_index('ix_user_username', ['username'], unique=True) def downgrade() -> None: # Remove username column from user table with op.batch_alter_table('user') as batch_op: batch_op.drop_index('ix_user_username') - batch_op.drop_column('username') \ No newline at end of file + op.drop_column('user', 'username') \ No newline at end of file