Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flask-Migrate integration #738

Closed
dolfandringa opened this issue Apr 14, 2018 · 5 comments
Closed

Flask-Migrate integration #738

dolfandringa opened this issue Apr 14, 2018 · 5 comments
Labels

Comments

@dolfandringa
Copy link
Contributor

I see a script db_migrate and db_upgrade in the bin folder, but no docs on how I am supposed to use it with flask appbuilder.
I see issue #373 about someone trying to integrate it themselves, and I can probably get that idea to work (I think the error there is he just doesn't import the model) but is there a way this is supposed to be integrated in flask appbuilder? Wouldn't it make sense to integrate it in fabmanager?

@dolfandringa
Copy link
Contributor Author

I'd be happy to write some docs on it if I get some pointers.

@dolfandringa
Copy link
Contributor Author

OK, I have it working now. I heavily borrowed ideas from superset. It would be great if fabmanager supports flask-migrate by default (contributions welcome?). I now wrote my own flask-script cli commands to implement it.

I made a script in app/bin/<appname> with the following:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import warnings
from flask.exthook import ExtDeprecationWarning
warnings.simplefilter('ignore', ExtDeprecationWarning)

from app.cli import manager

if __name__ == '__main__':
    manager.run()

And then in app/cli.py I implemented the command like this:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from flask_migrate import MigrateCommand
from flask_script import Manager

from app import app, db
from app.models import *

config = app.config


manager = Manager(app)
manager.add_command('db', MigrateCommand)

From there, you can start the flask-migrate stuff with app/bin/<appname> db init
After that you still need to change the target_metadata in app/migrate/env.py to point to the flask_appbuilder metadata.
At the top:

from flask_appbuilder import Base

Further down, change the existing target_metadata variable to:

target_metadata = Base.metadata

This will ensure it finds your flask_appbuilder sqlalchemy metadata. If you don't, it will actually create scripts to remove all flask_appbuilder tables, because it doesn't know about them.
Then (like always with flask-migrate) run app/bin/<appname> db migrate on an empty database to create your first migration script, which will create all tables like they are currently defined in your model, and then app/bin/<appname> db upgrade to actually run the migration.

@dpgaspar
Copy link
Owner

Hi @dolfandringa ,

New 0.11.0 solves this issue, made a small quickhowto with Flask-Migrate has an example: https://github.com/dpgaspar/Flask-AppBuilder/tree/master/examples/quickmigrate

@jabbawockeez
Copy link

Hi @dpgaspar I successfully ran the quickmigrate project but I have to detect the change of column type, so I add the compare_type=True as mentioned here, and then I ran it again but it stuck right before the upgrade, what is the problem here? Thanks.

@jabbawockeez
Copy link

Alright, since I use mysql as the store, I found out the problem. It's because that the type of the column "active" in the table ab_user is tinyint but is bool in model instead, so flask-migrate(alembic) detects the difference every time I try to upgrade, but as it's mentioned here, I think changing the column type to bool in mysql makes no sense, but I still don't know why it just stuck.
And one of the possible solutions is to ignore the table in detection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants