How to use Flask-Migrate with Google App Engine?
Question: Since I moved to Google App Engine I cannot run the Flask-Migrate command
`python manage.py db migrate` because I have exceptions regarding some GAE
related imports (`No module named google.appengine.ext` for example).

Is there a way to run this, or an alternative, to upgrade my database on GAE?


Answer: Yes, there is a way to run it, though it's not as straightforward as you'd
might like:

  1. You need to [configure your Google Cloud SQL](https://cloud.google.com/sql/docs/mysql-client#configure-instance-mysql), add yourself as an authorized user (by entering your ip address) and request to have an IPv4 address. Deal with SSL as appropriate.
  2. Using a script:

Replacing `user`, `password`, `instance_id`, `db_name`, and `path` below

    
    
    # migrate_prod.py
    DB_MIGRATION_URI = "mysql+mysqldb://user:password@instance_id/db_name?ssl_key=path/client-key.pem&ssl_cert=path/client-cert.pem&&ssl_ca=path/server-ca.pem"
    
    from flask import Flask
    from flask.ext.sqlalchemy import SQLAlchemy
    from flask_script import Manager
    from flask_migrate import Migrate, MigrateCommand
    from models import *   # not needed if migration file is already generated
    
    app = Flask(__name__)
    app.config["SQLALCHEMY_DATABASE_URI"] = DB_MIGRATION_URI
    app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
    db = SQLAlchemy(app)
    migrate = Migrate(app, db)
    
    manager = Manager(app)
    manager.add_command('db', MigrateCommand)
    
    if __name__ == '__main__':
        manager.run()
    

  3. Run the script as you would to migrate locally: `python migrate_prod.py db upgrade`, assuming your migration file is already there.

  4. Release the IPv4, so that you're not charged for it. 

I give much credit to other answers: [how to connect via
SSL](http://%20%20http://stackoverflow.com/questions/25785252/how-to-connect-
to-mysql-server-with-ssl-from-a-flask-app) and [run alembic migrations on
GAE](http://stackoverflow.com/questions/35391120/run-alembic-migrations-on-
google-app-engine/35395267) (of which this is probably a duplicate).

