Migration Lock Timeout
Migration Lock Timeout is a Ruby gem that adds a lock timeout to all Active Record migrations in your Ruby on Rails project. A lock timeout sets a timeout on how long PostgreSQL will wait to acquire a lock on tables being altered before failing and rolling back. This prevents migrations from creating additional lock contention that can take down your site when it's under heavy load. Migration Lock Timeout currently only supports PostgreSQL
Add this line to your application's Gemfile:
Configure the default lock timeout in a Rails initializer
#config/initializers/migration_lock_timeout.rb MigrationLockTimeout.configure do |config| config.default_timeout = 5 #timeout in seconds end
And that's all! Now every
up migration will execute
SET LOCAL lock_timeout = '5s';
inside the migration transaction before your migration code runs. No lock
timeout will be used for the
You can disable the lock timeout by using:
class AddFoo < ActiveRecord::Migration disable_lock_timeout! def change create_table :foo do |t| t.timestamps end end end
Custom lock timeout
You can change the duration of the lock timeout by using:
class AddBar < ActiveRecord::Migration set_lock_timeout 10 def change create_table :bar do |t| t.timestamps end end end
Additionally, if you have not set a default lock timeout, you can use this to set a timeout for a particular migration.
If you use
disable_ddl_transaction!, no lock timeout will occur
class AddMonkey < ActiveRecord::Migration disable_ddl_transaction! def change create_table :monkey do |t| t.timestamps end end end
Running the specs
To run the specs you must have PostgreSQL
installed. Create a database called
migration_lock_timeout_test and set the
POSTGRES_DB_PASSWORD then run
Bug reports and pull requests are welcome on GitHub at https://github.com/procore/migration-lock-timeout. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Migration Lock Timeout is maintained by Procore Technologies.
Procore - building the software that builds the world.
Learn more about the #1 most widely used construction management software at procore.com