0

I'm new to Alembic, so my apologies if I'm missing something, but I'm struggling to understand some methodology of my predecessor.

My predecessor (at my company) has a series of revision files inside of the versions/ directory. If a change to the database schema was necessary, he would make changes to the existing revision file and check that in to source control. For example, to resize a column, the existing create_table() function would be altered, from this:

op.create_table(
    'mytable',
    id(),
    sa.Column('name', sa.VARCHAR(50)),

...to this:

op.create_table(
    'mytable',
    id(),
    sa.Column('name', sa.VARCHAR(100)),

Is there any way this could be a useful or beneficial way to use Alembic that I am not aware of because I am new to it? Isn't this defeating the purpose of using Alembic altogether?

Regular User
  • 682
  • 7
  • 16

1 Answers1

0

whenever we create new revision file below will be the generic pattern i.e it will have upgrade and downgrade functions.

"""create mytable table

Revision ID: 3975ea83b745
Revises:
Create Date: 2021-05-21 23:48:27.038406

"""

# revision identifiers, used by Alembic.
revision = '3975ea83b745'
down_revision = None
branch_labels = None

from alembic import op
import sqlalchemy as sa

def upgrade():
    # create table mytable
    pass

def downgrade():
    # drop table mytable if exists
    pass

Changing exiting script is not good practice as you would need to downgrade till respective revision Id (here you will loose data) and upgrade again with new changes and it is obvious that you don't want to downgrade complete version in production. e.g In this case "mytable" will be downgraded first and then upgrade it with new changes.

As alembic is invocation of change management script for relational database.For new change create new revision and add change there. So in this case it would be of altering existing table's column's datatype.

Poonam Adhav
  • 382
  • 3
  • 10