1

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?

davidism
  • 121,510
  • 29
  • 395
  • 339
Cyril N.
  • 38,875
  • 36
  • 142
  • 243
  • What is the stack trace of the error? – Miguel Grinberg Apr 18 '16 at 17:01
  • what do you mean by "upgrade my database on GAE"? – marcadian Apr 18 '16 at 21:02
  • I'm using a Cloud SQL 2nd generation. When I modify one model in my code, Flask-Migrate generate the migration code to upgrade the current state of the database to the new one (adding one column, removing another one, etc) That's what I mean by Upgrade – Cyril N. Apr 19 '16 at 07:41
  • @Miguel There is no stack trace : I don't know how to run the command via app engine, that's all. I cannot simply do something like `./dev_appserver.py manage.py db migrate` – Cyril N. Apr 19 '16 at 07:42

1 Answers1

4

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, 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()
  1. Run the script as you would to migrate locally: python migrate_prod.py db upgrade, assuming your migration file is already there.

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

I give much credit to other answers: how to connect via SSL and run alembic migrations on GAE (of which this is probably a duplicate).

apaderno
  • 28,547
  • 16
  • 75
  • 90
Merbs
  • 377
  • 2
  • 5
  • Are we still charged for keeping the IP address ? I thought it was over with the v2 ? – Cyril N. May 09 '16 at 06:59
  • It may be, I'm still using v1. [See differences between v1 and v2](https://cloud.google.com/sql/docs/v1-v2-differences) – Merbs May 13 '16 at 02:39
  • Thank you. Assumes this still works but had to edit it a bit to get it to work for our postgres DB, and not having any hard-coded values and also use our existing DB configuration: https://gist.github.com/jwanglof/0131fc3df8a74250c7a20ec91b9ca179 – jwanglof Feb 23 '20 at 17:07