R
Railwayβ€’2y ago
sergey

Running DB migrations during CI/CD pipeline

Hey there πŸ‘‹ I've been looking into the proper way of running DB migrations (with Prisma in my case) as a part of deployment process. Trying to understand what's the proper phase for this on Railway. E.g. Heroku has the "Release" phase defined specifically for this task https://devcenter.heroku.com/articles/release-phase. I've seen that in one of your templates migrations are ran during the build phase https://github.com/railwayapp-templates/nextjs-prisma/blob/main/package.json#L7, which works, but doesn't look like the best solution, since build and release are kinda separate phases. And running migrations via railway cli from local computer doesn't look very good either. Did I miss any other piece of documentation? What would be the general advice for such cases? Thanks!
GitHub
nextjs-prisma/package.json at main Β· railwayapp-templates/nextjs-pr...
Contribute to railwayapp-templates/nextjs-prisma development by creating an account on GitHub.
14 Replies
Percy
Percyβ€’2y ago
Project ID: N/A
sergey
sergeyOPβ€’2y ago
N/A
Brody
Brodyβ€’2y ago
you can absolutely do a release phase on railway if you share your current railway.json file and your desired migration command I'd be happy to modify your railway.json file to include a release phase/command you can even have a migration stage
sergey
sergeyOPβ€’2y ago
Don't have a custom config file now, nixpack defaults seems to work quite nite in my case. But where would you put the release phase in the config? πŸ€” Couldn't find anything similar here https://docs.railway.app/deploy/config-as-code
Brody
Brodyβ€’2y ago
every deployment has a railway.json file but unfortunately I'm not at my computer anymore, so I can't make this modification for you
sergey
sergeyOPβ€’2y ago
every deployment has a railway.json file
I'm pretty sure I don't have a railway.json file πŸ˜„
Brody
Brodyβ€’2y ago
you might not in your project, but every deployment does get a railway.json generated for it I forgot the button to view it though
sergey
sergeyOPβ€’2y ago
Ah, interesting, I think I've found it in the deployment details
Brody
Brodyβ€’2y ago
do you have vscode?
sergey
sergeyOPβ€’2y ago
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS",
"buildCommand": "npm run build"
},
"deploy": {
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 10
}
}
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS",
"buildCommand": "npm run build"
},
"deploy": {
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 10
}
}
What do you mean?
Brody
Brodyβ€’2y ago
haha okay that's a no I'll get back to you later today
sergey
sergeyOPβ€’2y ago
sure thing, thanks. I would appreciate a link to the docs or the conceptual description of how to get the same or similar "release" phase as you'd have on Heroku, not really looking for a specific solution for current project
Brody
Brodyβ€’2y ago
it's hard to put into words, I'm not good at words, later today I will modify that railway.json you sent to have a release phase
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS",
"buildCommand": "npm run build",
"nixpacksPlan": {
"phases": {
"release": {
"dependsOn": ["build"],
"cmds": ["npm run migrate"]
}
}
}
},
"deploy": {
"startCommand": "npm run start",
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 10
}
}
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS",
"buildCommand": "npm run build",
"nixpacksPlan": {
"phases": {
"release": {
"dependsOn": ["build"],
"cmds": ["npm run migrate"]
}
}
}
},
"deploy": {
"startCommand": "npm run start",
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 10
}
}
sergey
sergeyOPβ€’2y ago
Thanks man, appreciate it a lot! So when exactly in the workflow will the release phase run? Is it happening before deploy? What happens if it fails? I went with adding the nixpacks.toml to the project with the following content
[phases.release]
cmds = ["npm run db:migrate"]
dependsOn = ["build"]
[phases.release]
cmds = ["npm run db:migrate"]
dependsOn = ["build"]
Seems to be working, although not 100% what I was looking for, as it still runs as a part of build process. But I believe there's no separate "release phase" concept on Railway. @Brody thanks for your help!
Want results from more Discord servers?
Add your server