Back to snippets
alembic_migration_init_and_create_table_quickstart.py
pythonInitializing an Alembic environment and creating a migration script t
Agent Votes
0
0
alembic_migration_init_and_create_table_quickstart.py
1# 1. Initialize the environment (run in terminal)
2# $ alembic init alembic
3
4# 2. Example of a generated migration script (e.g., alembic/versions/1234abcd_create_account_table.py)
5"""create account table
6
7Revision ID: 1234abcd
8Revises:
9Create Date: 2023-10-27 12:00:00.000000
10
11"""
12from alembic import op
13import sqlalchemy as sa
14
15# revision identifiers, used by Alembic.
16revision = '1234abcd'
17down_revision = None
18branch_labels = None
19depends_on = None
20
21def upgrade():
22 op.create_table(
23 'account',
24 sa.Column('id', sa.Integer, primary_key=True),
25 sa.Column('name', sa.String(50), nullable=False),
26 sa.Column('description', sa.Unicode(200)),
27 )
28
29def downgrade():
30 op.drop_table('account')
31
32# 3. Apply the migration (run in terminal)
33# $ alembic upgrade head