TypeORM migrations

I've installed the Railway CLI and am trying to run database migrations. This is from my package.json on the backend service. "migration:run:prod": "npm run typeorm migration:run", So I'm trying to cd into the /server directory, and run
railway run yarn run migration:run:prod
railway run yarn run migration:run:prod
but I'm getting the error message:
Error during migration run:
Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string
Error during migration run:
Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string
All my env vars are set, so I'm not sure what else to check. Does anyone have a suggestion? 🙂
Solution:
try updating the typeorm version to a more recent version?
Jump to solution
61 Replies
Percy
Percy10mo ago
Project ID: 152d55f3-0598-4f41-a2a7-ca60b41a626c
Brody
Brody10mo ago
does your version of typeorm not support the scram auth in use by postgres?
Steve Cook
Steve CookOP10mo ago
Project ID: 152d55f3-0598-4f41-a2a7-ca60b41a626c That's a great question - I'm on version 1.22.21 - I'll check
Brody
Brody10mo ago
1.22.21? npm says typeorm is at 0.3.20 unless im looking at the wrong package? https://www.npmjs.com/package/typeorm
Steve Cook
Steve CookOP10mo ago
Ah right - sorry, that was the yarn version. I'm on [email protected]
Brody
Brody10mo ago
mar 4th 2022 i forget if scram auth was the default back then
Solution
Brody
Brody10mo ago
try updating the typeorm version to a more recent version?
Steve Cook
Steve CookOP10mo ago
Gotcha - I'll give that a try. Thanks [again]! OK, it looks like I got past that error, now there's another error about missing the dataSource that TypeORM is raising. But I should be able to fix that pretty quick.
Brody
Brody10mo ago
awsome!
Steve Cook
Steve CookOP10mo ago
Well, I thought that solved it but it didn't. I did upgrade to [email protected] and am still getting the error. I was getting a different error before because with the version upgrade I had to update ormconfig.ts and add an argument to the script line. But once I corrected that, the SCRAM error returned. It is working locally just fine. I've researched the issue but haven't been able to come up with a specific reason for this. These are the scripts as defined in my package.json:
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
"typeorm:prod": "node ./node_modules/typeorm/cli.js",
"migration:run": "DB_PORT=4420 dotenv -e ../.env npx typeorm-ts-node-commonjs migration:run -- -d ./src/ormconfig.ts",
"migration:run:prod": "npm run typeorm:prod migration:run -- -d ./dist/ormconfig.js",
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
"typeorm:prod": "node ./node_modules/typeorm/cli.js",
"migration:run": "DB_PORT=4420 dotenv -e ../.env npx typeorm-ts-node-commonjs migration:run -- -d ./src/ormconfig.ts",
"migration:run:prod": "npm run typeorm:prod migration:run -- -d ./dist/ormconfig.js",
I run yarn run migration:run locally and railway run yarn run migration:run:prod for my Railway service. Any suggestions would be greatly appreciated.
Brody
Brody10mo ago
what version of node are you using locally and what version is railway using?
Steve Cook
Steve CookOP10mo ago
We're both on v20.11.0 I think the issue is that env vars are not loaded when I run migrations. When I run railway run yarn run env I do not see any of the database vars that are set in the Variables dashboard of my backend service. I'm not sure why that's happening. I figured it out - since I have a monorepo but two separate client and server services, the server service was linked to the client service; therefore it wasn't getting the server env vars. Once I linked it, it started working.
Brody
Brody10mo ago
well I'm glad you figured it out because there was no way I was going to come to that conclusion! 😆
Steve Cook
Steve CookOP10mo ago
Haha right @Brody ! It is worth noting, that railway run yarn run env does not produce the same results as railway variables . railway variables is definitely the way to go.
Brody
Brody10mo ago
whats the difference?
Steve Cook
Steve CookOP10mo ago
When I run railway run yarn run env gives me my local env vars. No matter which directory of which linked service I'm in.
Brody
Brody10mo ago
railway variables just prints variables, so what variables are missing from railway run?
Steve Cook
Steve CookOP10mo ago
May be an issue with the version of yarn I'm running. railway run yarn run env does not print any of the variables set in the service.
Brody
Brody10mo ago
what actual command is being ran by the env script?
Steve Cook
Steve CookOP10mo ago
Well, scratch what I just said - now it's working. I swear it was not working before. env should print out the variables in the service. I'm guessing railway variables is an alias for that.
MUG Knight
MUG Knight10mo ago
How did you get past the dataSource error @Steve Cook ? I've tried everything I can think of....
Steve Cook
Steve CookOP10mo ago
But the nice thing about railway variables is that it does not print out all the unnecessary variables - it only prints those in the service, which is very nice. And it's formatted very nicely, as well. @MUG Knight have you double-checked that your directory is linked to the appropriate service? The way I finally figured out my linking issue was running railway variables and seeing the wrong vars.
MUG Knight
MUG Knight10mo ago
I did link it this morning. Will double-check the vars again.
Brody
Brody10mo ago
right but how does that help you get those variables loaded into your environment so that your code can use them?
MUG Knight
MUG Knight10mo ago
It wasn't linked yesterday when I tried.
Steve Cook
Steve CookOP10mo ago
I'm loading those variables into my data source in ormconfig - is that what you mean? I'm referencing them wherever I need them on the server side with process.env.[var] railway variables doesn't do anything other than confirm I have them set correctly.
Brody
Brody10mo ago
right so what command are you now running to do the migrations locally?
Steve Cook
Steve CookOP10mo ago
I was so stuck on thinking the issue stemmed from my upgrading TypeORM that missed the obvious. @Brody to run migration locally: railway run yarn run migration:run:prod which is how I have the script configured in packages.json. I did have to edit that a bit after upgrading, as it now requires the the dataSource flag, and I had to export a dataSource from ormconfig rather than just have the config and use createConnection (which is now deprecated).
Brody
Brody10mo ago
so the entire problem is that you where just linked to the wrong service?
Steve Cook
Steve CookOP10mo ago
Not entirely - the initial problem was my migration script was incorrect to work with [email protected]. I was having trouble determining that the cause was linking because I couldn't get migrations to run local with the production ./dist .js files. Once I was able to get it to work loading env vars with dotenv locally, it didn't take long to track down the linking issue.
Brody
Brody10mo ago
gotcha, thanks!
Steve Cook
Steve CookOP10mo ago
Of course! Thank you!! Now I just need to figure out why I'm getting a 404 when I try to load the app using the URL generated for the client service. But that's a different issue. I'm not setting the listening host because express listens on 0.0.0.0 by default. After I deploy it logs listening on http://localhost[$PORT] so I'm not sure what's mis-configured there. I'll keep digging.
Brody
Brody10mo ago
seeing a 404 for the root path is very normal for many APIs
Steve Cook
Steve CookOP10mo ago
Yeah that's the client service though that I'm loading. What error are you getting? The dataSource error I was getting was because my migration command wasn't quite right.
MUG Knight
MUG Knight10mo ago
it keeps telling me my dataSource variable is not provided. It is defined in the package.json and I even try to pass it in manually via the CLI
Steve Cook
Steve CookOP10mo ago
Give this a shot in your packages.json scripts: "migration:run:prod": "npx typeorm migration:run --dataSource ./dist/ormconfig.js",. I found production to be a bit more tricky than just my local migrations. Also you need to make sure you're exporting the dataSource in your ormconfig
MUG Knight
MUG Knight10mo ago
When you linked, did you use the dist directory, or just the root directory of the product?
Steve Cook
Steve CookOP10mo ago
Root directory of the service, e.g., /server and /client
MUG Knight
MUG Knight10mo ago
My migration:run looks just like that, but uses database.config.ts, which does all of the same stuff.
Steve Cook
Steve CookOP10mo ago
I have it set to use ./dist for production, I'm guessing you do, as well. So you should be linking to your .js version in /dist (your database.config.js, that is), or wherever you're exporting the data source.
MUG Knight
MUG Knight10mo ago
cool. I'll keep hacking away at it and see what happens
Steve Cook
Steve CookOP10mo ago
Are you exporting the dataSource in database.config.ts?
MUG Knight
MUG Knight10mo ago
yep
Steve Cook
Steve CookOP10mo ago
ok cool
MUG Knight
MUG Knight10mo ago
welp, it just started working... 🤷‍♂️ not sure what I did different...
Steve Cook
Steve CookOP9mo ago
Hey, @Brody - I removed serve from my client service and copied Caddyfile and nixpacks.toml into my repo. I'm getting ready to try a deployment. I have the Dockerfile - do I not need to do anything to run it in production or will those new files do everything I need (I'm running yarn dev for my local environment)?
Brody
Brody9mo ago
if you are using nixpacks you would want to remove the dockerfile, they are mutually exclusive
Steve Cook
Steve CookOP9mo ago
Ah gotcha. Docker has been part of my dev environment forever - might be hard to break that one. But I'll see what I can do. Can't get it to work in prod anyway (even though the log says listening on localhost:8002) so I guess it can't hurt to try something different.
Brody
Brody9mo ago
are you serving the frontend and backend separately? like two apps in a monorepo?
Steve Cook
Steve CookOP9mo ago
yes - it is a monorepo but I have them split into separate services - may need to revisit that.
Brody
Brody9mo ago
and you do use a dockerfile for the frontend?
Steve Cook
Steve CookOP9mo ago
backend is working fine, just can't get the two to work together and yes, both services have their own Dockerfile well, can't get the client to serve - just get a 404
Brody
Brody9mo ago
why not have a Dockerfile.dev and a Dockerfile.prod, forego the whole nixpacks stuff
Steve Cook
Steve CookOP9mo ago
I was able to post to the backend with curl, though I have some conditionals in the Dockerfiles to run different commands based on the environment
Brody
Brody9mo ago
fair enough, so then write the dockerfile to use caddy, still foregoing the nixpacks stuff
Steve Cook
Steve CookOP9mo ago
ah gotcha - that sounds good. great idea. I'll read up on Caddy - I was cheating and using your files haha 😆
Brody
Brody9mo ago
you would still use my caddyfile though
Steve Cook
Steve CookOP9mo ago
yep, I'll just update the Dockerfile and nix the nixpack toml
Brody
Brody9mo ago
the root path in it would likely need to be changed, i dont know your project structure
Steve Cook
Steve CookOP9mo ago
Thanks for all the help 🤘 I was talking with my buddy over coffee this morning - he's the one who recommended Railway - and he knew who you are. You're omnipresent 😂
Brody
Brody9mo ago
thank you, im honored
Want results from more Discord servers?
Add your server