9

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?

davidism
  • 121,510
  • 29
  • 395
  • 339
gberger
  • 2,813
  • 3
  • 28
  • 50

3 Answers3

6
gberger
  • 2,813
  • 3
  • 28
  • 50
  • When you say, "release the IP" in the last bullet, are you referring to the local machine's IP from the first bullet or the assignment of the external IP in the third bullet? – mrp Nov 04 '21 at 13:21
4

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)

opyate
  • 5,388
  • 1
  • 37
  • 64
1

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

Herman
  • 1,882
  • 3
  • 14
  • 17
  • This is actually what I ended up doing. However, assigning a IPV4 address to the SQL instance costs $0.01/h = $7.2/mo. If there was a way to automate obtaining the address, running the migrations, and then releasing it, I'd be a happy camper – gberger Feb 14 '16 at 18:34
  • you can use gcloud sql instances patch --assign-ip, gcloud sql instances patch --no-assign-ip to assign/unassign an IP address – Vadim Feb 14 '16 at 23:08