Use advisory lock during table creation with knex#15
Use advisory lock during table creation with knex#15lexuzieel wants to merge 1 commit intoJulien-R44:mainfrom
Conversation
|
Hey, thanks a lot for investigating what about using |
|
Yeah I tried using it briefly through I tried using raw SQL like so: |
|
Hey man, I attempted a fix in the commit jsut above. your solution could indeed work, but issue is that its Postgres-specific, and the Knex driver is used with other databases too So lets see if the next release fixes your issue. If not, a reproduction would be super helpful! |
|
Awesome, looks like your fix works! I've updated the package to 0.5.2 and added some debug logging to verify - catch branch executes, but the code does not throw anymore, so it works as intended. Anyway, I have created a reproduction branch in case you are curious: https://github.com/lexuzieel/lavoro/tree/verrou-knex-race Since the test is flaky, you better run it like so (it will automatically spin up postgres in docker): This is an error that I was getting:
P.S. Yes, the name of the package is in fact inspired by verrou in a way :) and the package has adonisjs integration. Actually, I have in my plans to open a discussion with you guys in adonisjs forum, but after I draft up some docs & motivation. |

Hello, I have encountered a somewhat weird edge case which this PR aims to fix.
I am currently developing a package that uses Verrou under the hood. I am using
knexadapter for postgres and in my project I am using testcontainers to spin up ephemeral postgres instance.Somehow during testing there occurs a race condition where Verrou tries to create table twice & as a result it tries to create primary key multiple times as well. Here is the error that I get:
Wrapping table creation logic into a transaction and issuing an advisory lock fixes this for my case.
Take note, that I have not tested it with
kyselybut I think it is the same with that one.The only caveat with this PR is the hash generation for cache (I am not that familiar with postgres) but I suppose it should be safe since it is inside transaction.