45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
"""Rename username to sub
|
|
|
|
Revision ID: 460871bfa69e
|
|
Revises: 36c87c86c7b1
|
|
Create Date: 2025-05-07 10:35:27.476402
|
|
|
|
"""
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision = '460871bfa69e'
|
|
down_revision = '36c87c86c7b1'
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
def upgrade():
|
|
bind = op.get_bind()
|
|
inspector = sa.inspect(bind)
|
|
columns = [col['name'] for col in inspector.get_columns('users')]
|
|
|
|
# Check if migration already applied
|
|
if 'sub' in columns and 'username' not in columns:
|
|
return # Migration already applied
|
|
|
|
if bind.dialect.name == 'postgresql':
|
|
# PostgreSQL: Direct operations work fine
|
|
op.alter_column('users', 'username', new_column_name='sub')
|
|
op.drop_constraint('users_username_key', 'users', type_='unique')
|
|
op.create_unique_constraint(None, 'users', ['sub'])
|
|
else:
|
|
# SQLite: Use batch mode for column renaming and constraint handling
|
|
with op.batch_alter_table('users', schema=None) as batch_op:
|
|
batch_op.alter_column('username',
|
|
existing_type=sa.String(length=265),
|
|
new_column_name='sub',
|
|
nullable=False)
|
|
# Batch mode automatically handles constraint recreation
|
|
|
|
|
|
def downgrade():
|
|
# Not supported
|
|
raise Exception("Downgrade not supported")
|