Run Alembic migrations on Google App Engine

2019-04-08 14:32发布

I have a Flask app that uses SQLAlchemy (Flask-SQLAlchemy) and Alembic (Flask-Migrate). The app runs on Google App Engine. I want to use Google Cloud SQL.

On my machine, I run python manage.py db upgrade to run my migrations against my local database. Since GAE does not allow arbitrary shell commands to be run, how do I run the migrations on it?

3条回答
甜甜的少女心
2楼-- · 2019-04-08 15:12

It's all just code you can run, so you can create an admin endpoint with which to effect an upgrade:

@app.route('/admin/dbupgrade')
def dbupgrade():
    from flask_migrate import upgrade, Migrate
    migrate = Migrate(app, db)
    upgrade(directory=migrate.directory)
    return 'migrated'

(Dropwizard, for instance, caters nicely for such admin things via tasks)

查看更多
趁早两清
3楼-- · 2019-04-08 15:15

You can whitelist the ip of your local machine for the Google Cloud SQL instance, then you run the script on your local machine.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-04-08 15:22
查看更多
登录 后发表回答