21

I am using Alembic to manage migrations for a database. The same database is used by multiple Python packages and each of them have their own migration paths.

How I can tell Alembic to ignore tables from other packages when generating automatic migrations? For example when I run:

   alembic -c development.ini revision --autogenerate -m "Initial migration"

My migration Python file contains drop tables for other packages (not in the current Alembic env.py):

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('table_from_another_package`)

I can manually edit migration file and remove drop_table() and create_table() entries, but this is manual error prone work. I'd rather avoid generating them in the first place.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • Good question, although probably not such a great design to have multiple products access the same database. Do you at least use different schemas? Or some naming conventions? I do not think this is supported out of the box, but if i needed to make it work, i would probably look at the code which is reflecting the database (either `Metadata` or `Engine`) and find a way to overwrite it so that it filters out *no my tables*. – van Jul 03 '15 at 09:14

1 Answers1

24

Full control over what objects autogenerate considers is here.

If you are trying to run autogenerate in such a way that it considers only individual MetaData objects at a time you probably want to add customization in your env.py file that takes advantage of the "X" argument. Use this argument to receive which sub-component you want to work on, and consult that within your include_object function to look at just the objects that are relevant to that sub-component.

blep
  • 194
  • 1
  • 3
  • 16
zzzeek
  • 72,307
  • 23
  • 193
  • 185