Search code examples
google-cloud-platformflask-sqlalchemygoogle-cloud-sqlalembicflask-migrate

unable to perform flask-migrate migrations on cloud SQL


I am trying to implement flask-migrate with cloud SQL database. for this, I created a PostgreSQL instance on cloud sql. following is my app.py file

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate


app = Flask(__name__)
DB_MIGRATION_URI = "postgresql://postgres:1234@public-ip/demo"
app.config['SQLALCHEMY_DATABASE_URI'] = DB_MIGRATION_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
migrate = Migrate(app, db)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(20))
    last_name = db.Column(db.String(20))
    # following = db1.relationship('Channel', secondary=user_channel, backref='followers')

    def __repr__(self):
        return f'<User: {self.name}>'

class Channel(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20))

    def __repr__(self):
        return f'<Channel: {self.name}>'

I created this on the google cloud shell and I am connecting and trying to run migrations, everything from the google cloud shell.

when I first ran "flask db migrate", everything worked as expected. A version file was generated with autogenerated migrations script.

then, I ran "flask db upgrade" and it worked as expected too. I could see the changes in the database.

after that, I made some changes to schema in app.py and ran "flask db migrate -m "v2" it did not work. No matter what I do its not creating new versions It gives me the following error: enter image description here

enter image description here

I tried creating a new database and pointed the URL to it, I stopped and restarted the cloud SQL instance but it still gives me the same error. can someone help me figure out what is happening? Thank you!


Solution

  • You probably used the gcloud sql connect command, this whitelists your ip for 5 minutes. This is the command that gets executed if you click "open cloud shell" on the cloud SQL page.

    You can either whitelist your IP again for 5 more minutes or use the cloud sql proxy for longer-running connections: https://cloud.google.com/sql/docs/mysql/connect-instance-auth-proxy