go-pg / migrations

SQL database migrations for Golang go-pg and PostgreSQL
https://godoc.org/github.com/go-pg/migrations
BSD 2-Clause "Simplified" License
309 stars 59 forks source link

Allow collection.Version() calls during a migration #89

Closed maximerety closed 4 years ago

maximerety commented 4 years ago

As of today, if you try to call collection.Version(db) when a migration is running, it will wait for the LOCK TABLE gopg_migrations lock to be released before returning.

If you have long-running migrations (e.g. CREATE INDEX CONCURRENTLY ... that is running in background) and other pieces of code that rely on getting the current database version, this becomes a problem.

I stumbled upon this issue when trying to start services that checked the current database version at startup while a long-running migration was executed.

To allow this use case, we can update the lock command to LOCK TABLE ? IN EXCLUSIVE MODE which allows concurrent SELECT commands to be run on the migrations table, while still forbidding other operations.

From the documentation:

The EXCLUSIVE mode allows only concurrent ACCESS SHARE locks, i.e., only reads from the table can proceed in parallel with a transaction holding this lock mode.

See:

I've targeted the v7 branch for this patch, since v8 is not released yet. I can update this pull request if this is not the best target.

Thanks,

vmihailenco commented 4 years ago

Looks good - thanks.

vmihailenco commented 4 years ago

Released as v7.1.11