Run Alembic migrations on Google App Engine

[亡魂溺海] 提交于 2019-12-06 02:55:26

问题


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?


回答1:


  • Whitelist your local machine's IP: https://console.cloud.google.com/sql/instances/INSTANCENAME/access-control/authorization?project=PROJECTNAME
  • Create an user: https://console.cloud.google.com/sql/instances/INSTANCENAME/access-control/users?project=PROJECTNAME
  • Assign an external IP address to the instance: https://console.cloud.google.com/sql/instances/INSTANCENAME/access-control/ip?project=PROJECTNAME
  • Use the following SQLAlchemy connection URI: SQLALCHEMY_DATABASE_URI = 'mysql://user:pw@ip:3306/DBNAME'
  • Remember to release the IP later as you are charged for every hour it's not used



回答2:


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




回答3:


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)



来源:https://stackoverflow.com/questions/35391120/run-alembic-migrations-on-google-app-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!