Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 5 db migration: how to fix ActiveRecord::ConcurrentMigrationError

A previous rake db:rollback stalled. Now when attempting a new migration we get the following error:

rake aborted! ActiveRecord::ConcurrentMigrationError:   Cannot run migrations because another migration process is currently running.  /home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-    5.1.4/lib/active_record/migration.rb:1315:in `with_advisory_lock' /home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.4/lib/active_record/migration.rb:1148:in `migrate' /home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.4/lib/active_record/migration.rb:1007:in `up' /home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.4/lib/active_record/migration.rb:985:in `migrate' /home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:171:in `migrate' /home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.4/lib/active_record/railties/databases.rake:58:in `block (2 levels) in <top (required)>' /home/me/.rvm/gems/ruby-2.4.1/gems/rake-12.1.0/exe/rake:27:in `<top (required)>' /home/me/.rvm/gems/ruby-2.4.1/bin/ruby_executable_hooks:15:in `eval' /home/me/.rvm/gems/ruby-2.4.1/bin/ruby_executable_hooks:15:in `<main>' Tasks: TOP => db:migrate (See full trace by running task with --trace) 

We are using Postresql

like image 296
KeithP Avatar asked Oct 27 '17 13:10

KeithP


People also ask

How do I redo a migration in Rails?

You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.

How do I down a specific migration in Rails?

To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users.

How can I check my Rails migration status?

rake db:migrate:status (Rails 3 to 5) or rails db:migrate:status (Rails 5) will accomplish this. See this commit. up means the migration has been run. down means the migration has not been run.

Can you edit a migration file Rails?

If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run rake db:migrate.


2 Answers

Advisory locking was added in Rails 5 to prevent unplanned concurrency errors during migration. The fix is to clear the DB lock that was left in place.

Review the locks by running this SQL against your DB:

SELECT DISTINCT age(now(), query_start) AS age, pg_stat_activity.pid,pg_locks.granted,pg_stat_activity.application_name,pg_stat_activity.backend_start, pg_stat_activity.xact_start, pg_stat_activity.state_change, pg_stat_activity.waiting, pg_stat_activity.state, pg_stat_activity.query_start, left(pg_stat_activity.query, 60) FROM pg_stat_activity, pg_locks WHERE pg_locks.pid = pg_stat_activity.pid 

To clear a lock, run this SQL against your DB:

select pg_advisory_unlock({the pid of the lock you want to release}) 
like image 189
KeithP Avatar answered Sep 19 '22 07:09

KeithP


For me it was resolved in this way:

Select advisory locks:

SELECT pid, locktype, mode FROM pg_locks WHERE locktype = 'advisory'; 
SELECT pg_terminate_backend(<PID>); 
like image 33
Aleksei Strizhak Avatar answered Sep 20 '22 07:09

Aleksei Strizhak