postgres-migrations
postgres-migrations copied to clipboard
Error position
I've hand-written a pretty large baseline migration, and then started debugging it by creating a fresh database and running postgres-migrations
repeatedly. One problem I've run into is that crucial error details are lost inside this library, here:
https://github.com/ThomWright/postgres-migrations/blob/25e737982e1c6070fecccfc4855889b71e237a3b/src/run-migration.js#L49-L61
Sometimes PostgreSQL spews out messages like syntax error at or near ")"
, in a file full of functions, triggers, constraints, arrays, etc. The only way to find the cause for an error like that is to know the position
, which is contained inside the err
returned from the driver.
The easiest solution would probably be to extend the error message with Position: ${err.position}
, but there may be some value is passing through the other fields too. An even better solution would be to print the problematic line and its surroundings, with the exact position pointed out with a ^
, but that's a lot of extra effort for a small QoL improvement, so that's just an idea for the future.
Hi, thanks for the issue. I reckon the solution of putting the err.position
in our error message is something I'd accept a PR for.
Probably the main reason I don't do much with the errors is that I don't know what errors might be thrown. I assume not every error will have a position
property. However, if it does it seems worth emitting that.
What I tend to do in your position is to look at the PostgreSQL logs, mainly because I don't trust JS libraries (including mine!) to give the full picture.
Similar problem on my first attempts to use this library. I get pumped and write a big migration, then gotta debug it. But the error message reads: error at or near "." ... and there are 76 "." in the migration!
cause: Error: An error occurred running 'prepare'. Rolled back this migration. No further migrations were run. Reason: syntax error at or near "."
Can we at least add line indicator?
I don't think Postgres gives that information (correct me if I'm wrong). We can only report what we get back from Postgres.
Again, I'm happy to accept a PR for this.