How to run migrations/seeds with Railway
Hi, so I'm new to Railway, coming from Heroku. With Heroku I would be able to use the terminal and run my migrations within the site or from the CLI on my code editor.
Im using postgreSQL, Knex and Express. I've linked both railway and my github but I can't seem to find a way to run my migrations/seeds within the CLI. I try to do railway run --knex , but after this I'm not sure what else to do, am i missing something?
76 Replies
Please provide your project ID or reply with
N/A
. Thread will automatically be closed if no reply is received within 10 minutes. You can copy your project's id by pressing Ctrl/Cmd + K -> Copy Project ID.you are correct, you would run the migration/seed command prefixed with
railway run
what seems to be the issue?
if the app doesnt detect the changes that the migration/seed command has done, you can click the 3 dots on your latest deploy and then click re-deployNo project ID was provided. Closing thread.
What does it mean when it asks for project ID? I'm sorry for all the questions but I'm new here.
And I did try to run the command but nothing happened, the command I used was railway run --knex migrate:latest <my project name here> but I got an error. Does railway have a CLI i can use on their site instead?
theres no cli access to the deployments
what was the error you got from the railway cli
It's not an error actually sorry, more of a file or directory not found issue.
With Heroku, I would run a similar command and would add the project name after knex migrate: latest /seed within my CLI or on their site's CLI and it would add it to production.
I did try to go over some tutorials on Youtube but couldn't find anything, most of them (if any) are not using Knex. I attached 2 other screenshots. One of those screenshots shows my Postgres database (ends with railway); the other is my deployed API.
I think I'm on the right path but missing something minor.
railway isnt a clone of heroku, so if you try to do everything the same way that you used to with heroku you are going to run into a lot of headaches
make sure you have linked your local project to the correct railway project
railway link
make sure you have selected the correct service railway service
then run your migration command railway run knex migrate:latest
(i dont know why you had double dashes??)
and if you havent read the cli help printout, railway run
will only run commands locally, so make sure you have knex installed
you might have to run railway run npx knex migrate:latest
if you dont have knex installed globallyAh Brody, it might be working now, your info helped but I believe my railway is reading my development instead of my production setup gahh
railway run knex migrate:latest
Message: Using environment: development
you can switch environments with
railway environment
When I run railway service and link I try to select the production option, but when i run knex, it defaults to development
highly suggest reading the output of
railway help
Gonna do that right now, i appreciate you responding back, you're a rockstar! Let me read the docs and I'll get back to you here 🙂
nede to do a sanity check, the service you have linked, can you show me a screenshot of its service variables?
a screenshot from in the railway ui
yes, let me do that now
Does this help?
show me a full browser window screenshot please
full screenshot with this
Does this help? It says I'm in the production environment but somehow when i run knex migrate it redirects it into my development environment which i don't want.
that would be a knex problem that you would need to look into
and I assume you don't use a railway database?
Well, I created a railway DB and added the database_url into my nodeExpress-graphQL API. I'm sure it's something minor, i was able to finally run knex with railway which I couldnt before you helped me 🙂
I don't see a database in any of your screenshots?
oh, i might have not taken a screenshot, one sec
well the database should be in the same project as the services that uses it
and if you have 2 services that both use the database, the the two services should be in the same service too
This is the postgres DB i created, I copied the database_url and add it as an ENV variable on the nodeExpress_graphql API, did i do it right?
^
I'll have to look into it a little more later, im tired for the day, it's been a challenge today, will keep posted 🙂
Hi Brody, i was off the whole weekend but am back, i had to delete and start from scratch again and still stuck with deploying my postgres DB.
I'm able to run it locally and even created a postgres DB, but i still can't seem to find a way to run migrations and add them into the postgres DB I created on Railway, gonna attach more screenshots, if you have time you think you can look into it? I understand if it's too much to ask.
screenshots are good
So this is all I could come up with, more info below:
Postgres DB Project ID: a6a00d13-360e-4ad0-8492-af83dc69d9c2
Api Project ID: 45283fa7-475e-4619-81ab-de78c05575a1
^^
https://jwtauth-heroku-postgresql-production.up.railway.app/
I know you said 'railway run' runs locally, but everytime I do that it says 'production' environment, wouldn't that go into my dev environment and not production?
Locally it's fine, i run my migrations/seeds and i can see that on my local DB, only thing I'm missing now is just running the migrations within the CLI and adding them to my production env, this is where I'm currently stuck.
For my package.json file, don't mind the Heroku part, it's actually what I used to do before to run my migrations and it worked fine, but since you mentioned Railway and Heroku are totally different, then I guess this is what I'm trying to accomplish through Railway now
again i see a database without any services
I ran 'railway link' but I see 2 options, I guess this is where I'm confused. When you say services, do I need to link both the API and the db?
there is only a database here, where are the services that need to access this database???
I guess this is where I'm confused or I'm just not getting it. The Railway Service would be my deployed project? I added the DATABASE_URL to my 'env' file and thought that would be enough but I guess I'm still very far off from getting it right.
i dont see any railway services here
i dont know why you keep showing me screenshots of your terminal
because when I do railway service that's all that comes up. So I already linked them, ran railway service and thought this would add the service but I'm just not getting it 😦
why are there no services here??
if you want to have a service access that database, you need to have the service in that project
at this point, this has nothing to do with the railway cli
wouldn't the DATABASE_URL be the service? I copied that and added it to my env file.
thats a variable
look
theres a database, and in my case, 3 services
This a lot more advanced for my skillset hehe but am learning. Seems like it takes a little more setup to get it up and running. So if I wanted to add a service, what would be my next step?
I did go on their docs, but it says to go to a service and create a variable, which I thought I did, but that was not it.
aww man i feel like such a noob but then again, im trying to break into tech and would be good to know how to troubleshoot problems like this
you have a database and no services
show me a screenshot of your dashboard
Whoa, yours actually has services within your postgres db, that's what I want.
you have seperated your database and services into seprate projects
ahhh
I thought I could create the postgrest DB then use the DATABASE_URL within my project / api
you could, but thats very much the incorrect way, and would only cause headaches
go ahead and merge your two seprate projects into one
Okay I tried i wasn't able to link them, i might as well delete them and start from scratch again lol.
Then create a postgresDB, and add my service from within there? I tried looking up stuff online, but when I try to link my project and then service it just wont show.
yes the merging would involve deleting the project that only contains the services, then adding services to the project that currently contains just the database
Okay, so I deleted the service and added them within my JWT_DB, now it says 2 services but the postgrest DB is one of them so I still might not be correct, gahh.
This is complicated lol but I'm sure sooner or later i'll get it right.
so in your dashboard, how many projects do you have
If you are referring to JWT_DB, it says 2 services
no
how many projects do you have in your dashboard
2
screenshot please
I think, because one is the db, and the other is the service unless im looking at the wrong page. When you say dashboard, are you referring to this:
that is not a dashboard
this is a dashboard
So it's the first page of the site, this one?
that is not the dashboard, as I've previously stated
my bad i attached the wrong one
gonna take a lunch, brb
when back
delete your service variable
DATABASE_URL
and properly setup the variable refences in your service
https://docs.railway.app/develop/variables#reference-variablesBack, I see what happened there. I added it manually but technically all I needed to do was let the autocomplete dropdown add the variable instead.
Now that I've added it, i linked both the db and the api project, i ran the service but i still can't run my migrations.
So now that I've added the reference variable the correct way, what would be the next step on my end?
read the docs for whatever orm you are using
Okay, I'm going over it, i see Prisma but not Knex although I might follow along with this:
https://blog.railway.app/p/expressjs-with-postgresql
I think I get the whole 'service' part now, needs to be within my postgrestDB. Now the part I need to figure out is how to run those migrations/seeds into my prod DATABASE_URL service
Railway Blog
Deploy an ExpressJS app with PostgreSQL database
In this post, we will walk you through the process of building and hosting your ExpressJS backend with Railway. From deploying the barebones example to setting up a database (+ adding dummy data to it) to working with it locally, we've got you covered!
read the docs for the orm you are using, not the railway docs
gahh, okay, let me go to the knex docs
So i went over it, it's basically the same thing. 'Knex run:seed", "knex migrate:latest".
So I know you said railway run (and the docs) is for running the CLI locally, but how can i run these same commands to my postgresDB on Railway? I think it's set up right now, just missing the last step adding migrations/seeds to my railway db. Sorry for all the back and forth, this has been a struggle for me lol i understand if there's not much you can assist with although you did guide me in the right direction and was able to add a service now 🙂
So I know you said railway run (and the docs) is for running the CLI locally, but how can i run these same commands to my postgresDB on Railway? I think it's set up right now, just missing the last step adding migrations/seeds to my railway db. Sorry for all the back and forth, this has been a struggle for me lol i understand if there's not much you can assist with although you did guide me in the right direction and was able to add a service now 🙂
railway run
pulls the service variables (one of which would be the database variable) then runs the given command with the service variables available, so you would have to setup knex to tell it to connect to the database from the database variable, obviously you have yet to setup knex to connect to the database in the database url variable, and you are just expecting it to magically work somehow
just tell knex to connect to the database in the DATABASE_URL variable while running seed and migrate commands, to figure out how to do this you would need to read the documentation for knexThank you so much
Finally, I tested it, and it's working omg, took so long, but going over the documentation like you mentioned helped. I was too lazy to go over it but after taking my time, i finally got it to work. I'm now running my knex in production only
reading the docs always works
Yes, it did, you helped me a ton, i appreciate it a lot Brody
no problem 🙂