How to automate module migration in checkbox migration

In my project, jars in some model definitions use sqlalchemy_utils, which leads to migration errors, for example:

NameError: global name 'sqlalchemy_utils' is not defined 

due to the fact that this package is not imported into migration files.

I would like flask-migrate / alembic to automatically generate strings importing this package into migration files, how can I achieve this?

I looked at alembic.ini and migrations / env.py - but it did not seem to me that this is the correct way / if at all possible.

+7
python flask alembic flask-migrate
source share
1 answer

The easiest way is to modify the template to enable this import.

script.py.mako :

 ... from alembic import op import sqlalchemy as sa import sqlalchemy_utils ${imports if imports else ''} ... 

If you have several modules that provide custom types, you can use the strategy described in the documents . Create a module in your project that imports the various modules, and then install it, as the Alembic prefix should use for custom types.

/myapp/migration_types.py :

 from sqlalchemy_utils import * from myapp.custom_model_type import MyType 

script.py.mako :

 ... from myapp import migration_types ... 

env.py :

 ... def run_migrations_online(): ... context.configure( ... user_module_prefix='migration_types.', ... ) ... 
+9
source share

All Articles