Databases python Intermediate

Alembic Migration Boilerplate

Alembic migration file with upgrade and downgrade steps and proper op helpers.

python
"""Add modules table"""
revision = 'abc123'
down_revision = None

from alembic import op
import sqlalchemy as sa

def upgrade() -> None:
    op.create_table('modules',
        sa.Column('id',    sa.Integer(), primary_key=True),
        sa.Column('slug',  sa.String(255), unique=True, nullable=False),
        sa.Column('title', sa.String(255), nullable=False),
        sa.Column('code',  sa.Text(),      nullable=False),
    )

def downgrade() -> None:
    op.drop_table('modules')

Prerequisites

  • alembic
  • sqlalchemy

Pro Tips

  • → Always implement downgrade() for safe rollbacks.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
python
Level
Intermediate
Read time
3 min
Published
Mar 2025
License
Commercial License

Newsletter

New modules every week, directly to your inbox.