pythonpython-3.xpostgresqlflaskflask-migrate

Railway.app: Running command (Flask-Migrate) in Deployed Service Environment


I am in the process of migrating a Python flask web app from Heroku to Railway. The app uses a set of Flask CLI commands to initialize and update a Postgres database schema.

In the development environment, for example, I would run "flask db migrate" in order to create a db model to be copied to other locations.

In order to copy the model into staging or production environments, I would then need to run the command "flask db upgrade" on the staging/production app. Heroku's CLI "run" command allows you to do this without SSH by running the following command:

heroku run flask db upgrade --app NAME-OF-STAGING/PRODUCTION-APP

As far as I know, Railway's CLI run command does not allow you to select an online environment on which to run the command in the same way. Nor do they allow SSH access.

Is there any way to run a CLI command or Python file on a Railway live app, so that I can run Flask-migrate's "db upgrade" and get my Postgres DB working?


Solution

  • You can access your Railway database through a URL. You can find it in DB variables and it will look something like this: postgresql://username:password@something.railway.app:port/railway. So just set your SQLALCHEMY_DATABASE_URI to be that URL and then run the migration script locally.

    Hope that helps!