-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
migrate/postgres: skip advisory lock for cockroachdb #1762
Conversation
Oh yeah, I forgot to include in the description -- there is precedence in other projects for taking this approach: prisma/prisma-engines#2635 Which doesn't necessarily mean it's the right approach here of course. |
@mehcode wrote most of the migration machinery so I'd like him to chime in when it's convenient. But from what I understand, the point of taking a lock is so that multiple instances of the application starting at the same time (for example, when deploying a new image to a Kubernetes cluster with >1 replicas) don't try to execute the same migrations at the same time, which could cause errors or data loss. Advisory locks are one way to do this, but we could also just acquire an exclusive lock on the lock table _sqlx_migrations in exclusive mode
I think a little bit of refactoring would be necessary as we'd have to create the |
@abonander is correct. I'd be opposed to dropping locks for cockroachdb and would rather switch to an exclusive table lock for cockroachdb. That should maintain the same invariant. @abonander Do you think there is any purpose in keeping the advisory lock here and downgrading to table lock or just doing only table lock? |
Welp, unfortunately it looks like the However, I think instead of trying to special-case for CockroachDB, we should maybe just have a |
Okay, this makes sense. I hadn't been thinking of the >1 replica situation and agree with you that it's probably not a good idea to enable that for CockroachDB until they implement some kind of appropriate locking mechanism. Would a transaction around the migration operations on Anyway, I hadn't thought to try it yesterday but today I see that
I like the default being to keep the locking behavior and to opt in to disabling it -- if |
@waynr were you going to add that here or in a new PR? |
Closing due to inactivity. If you want to reopen, please follow up on my previous comment. |
Hey @abonander my intuition is that it's best not to make it possible for migrations to be run in application without the advisory lock or something similar so I decided not to open a PR with my changes. |
This PR skips the use of
pg_advisory_lock
inimpl Migrate for PgConnection
if the string returned bySELECT version()
includes the substring "CockroachDB".This is necessary because the folks at CockroachDB haven't implemented it yet.
I actually don't know if this PR is the right approach (seems like it would be better for CockroachDB to do its best to implement support). Though for that matter, reading the postgres docs on advisory locks leaves me feeling a little confused because of the following:
Does this mean that locks held by the sqlx migration may not hold meaning or be honored by application(s) using the DB?
Anyway, it seems like this PR or something like it would be necessary for sqlx to run migrations against cockroach db instances older than whatever version finally supports
pg_advisory_lock
.Also, for what it's worth this is the error I get without this PR: