Fix user role migration script to add column before updating values

This commit is contained in:
Automated Action 2025-06-17 02:56:06 +00:00
parent d60767d0ba
commit cf6d0b35ef

View File

@ -20,7 +20,12 @@ def upgrade():
# Add role column to users table
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.add_column(sa.Column('role', sa.Enum('admin', 'user', name='userrole'), nullable=True))
batch_op.execute("UPDATE users SET role = 'user' WHERE role IS NULL")
# Update existing users to have the 'user' role
op.execute("UPDATE users SET role = 'user' WHERE role IS NULL")
# Make the column non-nullable with a default value
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.alter_column('role', nullable=False, server_default='user')