R
Railway•2y ago
seiya2323

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
Percy
Percy•2y ago
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.
Brody
Brody•2y ago
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-deploy
Percy
Percy•2y ago
No project ID was provided. Closing thread.
seiya2323
seiya2323OP•2y ago
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?
Brody
Brody•2y ago
theres no cli access to the deployments what was the error you got from the railway cli
seiya2323
seiya2323OP•2y ago
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.
Brody
Brody•2y ago
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 globally
seiya2323
seiya2323OP•2y ago
Ah 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
Brody
Brody•2y ago
you can switch environments with railway environment
seiya2323
seiya2323OP•2y ago
When I run railway service and link I try to select the production option, but when i run knex, it defaults to development
Brody
Brody•2y ago
highly suggest reading the output of railway help
seiya2323
seiya2323OP•2y ago
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 🙂
Brody
Brody•2y ago
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
seiya2323
seiya2323OP•2y ago
yes, let me do that now
seiya2323
seiya2323OP•2y ago
Does this help?
Brody
Brody•2y ago
show me a full browser window screenshot please full screenshot with this
Brody
Brody•2y ago
seiya2323
seiya2323OP•2y ago
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.
Brody
Brody•2y ago
that would be a knex problem that you would need to look into and I assume you don't use a railway database?
seiya2323
seiya2323OP•2y ago
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 🙂
Brody
Brody•2y ago
I don't see a database in any of your screenshots?
seiya2323
seiya2323OP•2y ago
oh, i might have not taken a screenshot, one sec
Brody
Brody•2y ago
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
seiya2323
seiya2323OP•2y ago
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?
Brody
Brody•2y ago
^
seiya2323
seiya2323OP•2y ago
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.
Brody
Brody•2y ago
screenshots are good
seiya2323
seiya2323OP•2y ago
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
Brody
Brody•2y ago
again i see a database without any services
seiya2323
seiya2323OP•2y ago
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?
seiya2323
seiya2323OP•2y ago
Brody
Brody•2y ago
there is only a database here, where are the services that need to access this database???
seiya2323
seiya2323OP•2y ago
mildpanic
seiya2323
seiya2323OP•2y ago
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.
Brody
Brody•2y ago
i dont see any railway services here i dont know why you keep showing me screenshots of your terminal
seiya2323
seiya2323OP•2y ago
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 😦
Brody
Brody•2y ago
why are there no services here??
Brody
Brody•2y ago
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
seiya2323
seiya2323OP•2y ago
wouldn't the DATABASE_URL be the service? I copied that and added it to my env file.
Brody
Brody•2y ago
thats a variable
Brody
Brody•2y ago
look
Brody
Brody•2y ago
theres a database, and in my case, 3 services
seiya2323
seiya2323OP•2y ago
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
Brody
Brody•2y ago
you have a database and no services show me a screenshot of your dashboard
seiya2323
seiya2323OP•2y ago
Whoa, yours actually has services within your postgres db, that's what I want.
Brody
Brody•2y ago
you have seperated your database and services into seprate projects
seiya2323
seiya2323OP•2y ago
ahhh I thought I could create the postgrest DB then use the DATABASE_URL within my project / api
Brody
Brody•2y ago
you could, but thats very much the incorrect way, and would only cause headaches go ahead and merge your two seprate projects into one
seiya2323
seiya2323OP•2y ago
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.
Brody
Brody•2y ago
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
seiya2323
seiya2323OP•2y ago
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.
Brody
Brody•2y ago
so in your dashboard, how many projects do you have
seiya2323
seiya2323OP•2y ago
If you are referring to JWT_DB, it says 2 services
Brody
Brody•2y ago
no how many projects do you have in your dashboard
seiya2323
seiya2323OP•2y ago
2
Brody
Brody•2y ago
screenshot please
seiya2323
seiya2323OP•2y ago
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:
Brody
Brody•2y ago
that is not a dashboard
Brody
Brody•2y ago
this is a dashboard
seiya2323
seiya2323OP•2y ago
So it's the first page of the site, this one?
Brody
Brody•2y ago
that is not the dashboard, as I've previously stated
seiya2323
seiya2323OP•2y ago
my bad i attached the wrong one
seiya2323
seiya2323OP•2y ago
gonna take a lunch, brb mildpanic
Brody
Brody•2y ago
when back delete your service variable DATABASE_URL and properly setup the variable refences in your service https://docs.railway.app/develop/variables#reference-variables
seiya2323
seiya2323OP•2y ago
Back, 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?
Brody
Brody•2y ago
read the docs for whatever orm you are using
seiya2323
seiya2323OP•2y ago
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!
Brody
Brody•2y ago
read the docs for the orm you are using, not the railway docs
seiya2323
seiya2323OP•2y ago
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 🙂
Brody
Brody•2y ago
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 knex
seiya2323
seiya2323OP•2y ago
Thank you so much
seiya2323
seiya2323OP•2y ago
seiya2323
seiya2323OP•2y ago
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
Brody
Brody•2y ago
reading the docs always works
seiya2323
seiya2323OP•2y ago
Yes, it did, you helped me a ton, i appreciate it a lot Brody
Brody
Brody•2y ago
no problem 🙂
Want results from more Discord servers?
Add your server