Flask-migrate: Packaging migrations with setuptools

Created on 26 Jan 2021  路  8Comments  路  Source: miguelgrinberg/Flask-Migrate

Is possible to package the migrations folder with setuptools ?

I was fallowing the flask tutorial and got my application setup as:

from setuptools import find_packages, setup

setup(
    name='myapp',
    version='0.0.1',
    packages=find_packages(),
    include_package_data=True,
    zip_safe=False,
    long_description=__doc__,
    tests_require = ['pytest', 'pytest-cov'],
    install_requires=[
        'Flask',
        'Flask-Migrate',
        'Flask-SQLAlchemy',
        'Flask-Login',
        'Flask-WTF'
    ],
)

and the dir tree:

myapp/
  __init__.py
  ...
migrations/
  alembic.ini
  ...

setup.py

and MANIFEST.in:

include requirements.txt
graft migrations
graft myapp/static
graft myapp/templates
global-exclude *.pyc

The problem is that after installing the package in a test environment i can't find the migrations directory anywhere .

question

Most helpful comment

Ended up making my own package with a simple blueprint that assumes the migrations directory is called exactly migrations and is distribute along with the python package, also allowing only upgrade and history commands as safety measures.

All 8 comments

Well moving migrations onto the application package and changing MANIFEST.in to:

include requirements.txt
graft myapp/migrations
graft myapp/static
graft myapp/templates
global-exclude *.pyc

seems to add the migrations to the package, but how can tell to flask-migrate where to search for them since the migrations are now in lib/python...

@ybenitezf first of all, this is not a workflow that I have envisioned for Flask-Migrate. But in any case, all flask db commands support a -d option that can be used to specify a non-default migrations directory.

I just wanted a traditional way to deploying my application, so that in production i can simply do something like:

pip install git+https://github.com/ybenitezf/myapp
flask db upgrade

Instead of cloning my repository. I suppose can live with:

pip install git+https://github.com/ybenitezf/myapp
flask db upgrade -d SOME_DIR/site-packages/myapp/migrations

@ybenitezf You can create your own entry point, where you can invoke the upgrade command programmatically. Maybe something like this:

pip install git+https://github.com/ybenitezf/myapp
myapp upgrade

Something like my own CLI command to do my upgrade ? Can i reuse the Flask-Migrate code for this ? ummm ?

I will try ... thanks

Yeah, the upgrade command can be executed from Python, and you can pass the migrations directory as an argument. See the Reference API section of the docs.

thanks

Ended up making my own package with a simple blueprint that assumes the migrations directory is called exactly migrations and is distribute along with the python package, also allowing only upgrade and history commands as safety measures.

Was this page helpful?
0 / 5 - 0 ratings