3rd-project-7hltbg/alembic/versions/20250414_144046_ebe9468d_update_fruit.py

29 lines
973 B
Python

"""add shape field to fruits table
Revision ID: 1a2b3c4d5e6f
Revises: 0002
Create Date: 2024-01-20 10:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '1a2b3c4d5e6f'
down_revision = '0002'
branch_labels = None
depends_on = None
def upgrade():
# SQLite doesn't support adding NOT NULL columns without defaults
# We need to use batch operations for this
with op.batch_alter_table('fruits') as batch_op:
batch_op.add_column(sa.Column('shape', sa.String(), nullable=True))
# Update existing rows with a default value
op.execute("UPDATE fruits SET shape = 'unknown' WHERE shape IS NULL")
# Now make the column NOT NULL
batch_op.alter_column('shape',
existing_type=sa.String(),
nullable=False)
def downgrade():
with op.batch_alter_table('fruits') as batch_op:
batch_op.drop_column('shape')