0

I am trying to use alembic migrations to act on different versions of the same database. An example would be that I have two databases, one live and one for testing. Each of them might be in different states of migration. For one, the test database might not exist at all.

Say live has a table table1 with columns A and B. Now I would like to add column C. I change my model to include C and I generate a migration script that has the following code

op.add_column('table1', sa.Column('C', sa.String(), nullable=True))

This works fine with the existing live database.

If I now call alembic upgrade head referring to a non-existing test database, I get an (Operational Error) duplicate column name... error. I assume this is due to my model containing the C column and that alembic/sqlalchemy creates the full table automatically if it does not exist.

Should I simply trap the error or is there a better way of doing this?

Tammo Heeren
  • 1,966
  • 3
  • 15
  • 20

1 Answers1

1

I would suggest that immediately after your test db is newly created you should stamp it with head

command.stamp(configs_for_test_db, 'head')  

This will go ahead and insert the head revision number into the appropriate alembic table without actually running migrations so that the revision number will reflect the state of the db (namely that your newly created db is up to date wrt migrations). After the db is stamped, alembic upgrade should behave properly.

karina
  • 805
  • 5
  • 10