Run Alembic migrations on Google App Engine
Asked Answered
G

3

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?

Glosseme answered 14/2, 2016 at 11:17 Comment(0)
G
6
Glosseme answered 19/4, 2016 at 23:34 Comment(1)
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?Equity
C
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)

Contradistinguish answered 12/1, 2018 at 14:38 Comment(0)
H
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.

Harsho answered 14/2, 2016 at 18:10 Comment(2)
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 camperGlosseme
you can use gcloud sql instances patch --assign-ip, gcloud sql instances patch --no-assign-ip to assign/unassign an IP addressNoise

© 2022 - 2024 — McMap. All rights reserved.