Skip to content

Conversation

@akshah123
Copy link

Add CLI support for migration locking to prevent concurrent migrations from running simultaneously. This addresses community requests from issues #335 and #972.

New features:

  • Add -lock flag with modes: none, session, table
  • Add GOOSE_LOCK environment variable support
  • Session locking uses PostgreSQL advisory locks (pg_advisory_lock)
  • Table locking uses distributed table-based locks with heartbeat

When locking is enabled, the CLI uses the Provider API which supports the SessionLocker and Locker interfaces. This ensures proper integration with the existing lock package.

Fail-fast behavior: If a user requests locking for a driver that doesn't support it, the CLI exits with a clear error message. Currently only PostgreSQL (postgres, pgx, redshift) supports both lock modes.

Usage examples:
goose -lock=session postgres "..." up
GOOSE_LOCK=table goose postgres "..." up

Add CLI support for migration locking to prevent concurrent migrations
from running simultaneously. This addresses community requests from
issues pressly#335 and pressly#972.

New features:
- Add -lock flag with modes: none, session, table
- Add GOOSE_LOCK environment variable support
- Session locking uses PostgreSQL advisory locks (pg_advisory_lock)
- Table locking uses distributed table-based locks with heartbeat

When locking is enabled, the CLI uses the Provider API which
supports the SessionLocker and Locker interfaces. This ensures
proper integration with the existing lock package.

Fail-fast behavior: If a user requests locking for a driver that
doesn't support it, the CLI exits with a clear error message.
Currently only PostgreSQL (postgres, pgx, redshift) supports both
lock modes.

Usage examples:
  goose -lock=session postgres "..." up
  GOOSE_LOCK=table goose postgres "..." up
@aieri
Copy link

aieri commented Dec 17, 2025

is there a reason to not make locking the default for supported drivers?

@akshah123
Copy link
Author

akshah123 commented Dec 17, 2025

is there a reason to not make locking the default for supported drivers?

I believe even without CLI, locking is not enabled by default. Users have to explicitly add it with withLocking.... I didn't want to introduce a potentially backward breaking change here. For reference: https://pressly.github.io/goose/documentation/provider/#withsessionlocker

If maintainers wish to make this default, I can update my PR accordingly.

@mfridman
Copy link
Collaborator

Locking will be the default in /v4 of this library.

This feature didn't always exist, and for historical reasons, serialization was managed manually (no need for locking). But since then, it's become apparent that locking in the app is the way to go and has worked quite well for many users.

@akshah123
Copy link
Author

@mfridman to remove any ambiguity, are you suggesting I should make it a default in this PR or that not to do so until v4?

@akshah123
Copy link
Author

Also, would love your thoughts on whether this is something community would like to have and if yes any suggestions on what I should address so that this can be merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants