Use advisory lock during table creation with knex #15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

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.