Fix todo priority migration script to add columns before updating values
This commit is contained in:
parent
cf6d0b35ef
commit
c2e277157a
@ -18,16 +18,18 @@ depends_on = None
|
|||||||
|
|
||||||
def upgrade():
|
def upgrade():
|
||||||
# Create priority_level enum type
|
# Create priority_level enum type
|
||||||
|
# Step 1: Add the priority column
|
||||||
with op.batch_alter_table('todos', schema=None) as batch_op:
|
with op.batch_alter_table('todos', schema=None) as batch_op:
|
||||||
# Add priority column
|
|
||||||
batch_op.add_column(sa.Column('priority', sa.Enum('low', 'medium', 'high', name='prioritylevel'), nullable=True))
|
batch_op.add_column(sa.Column('priority', sa.Enum('low', 'medium', 'high', name='prioritylevel'), nullable=True))
|
||||||
# Set default values for existing records
|
|
||||||
batch_op.execute("UPDATE todos SET priority = 'medium' WHERE priority IS NULL")
|
|
||||||
# Make priority not nullable with default
|
|
||||||
batch_op.alter_column('priority', nullable=False, server_default='medium')
|
|
||||||
|
|
||||||
# Add due_date column
|
# Add due_date column
|
||||||
batch_op.add_column(sa.Column('due_date', sa.DateTime(), nullable=True))
|
batch_op.add_column(sa.Column('due_date', sa.DateTime(), nullable=True))
|
||||||
|
|
||||||
|
# Step 2: Update existing records with default priority value
|
||||||
|
op.execute("UPDATE todos SET priority = 'medium' WHERE priority IS NULL")
|
||||||
|
|
||||||
|
# Step 3: Add constraints to make priority non-nullable with default
|
||||||
|
with op.batch_alter_table('todos', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('priority', nullable=False, server_default='medium')
|
||||||
|
|
||||||
|
|
||||||
def downgrade():
|
def downgrade():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user