Rails 5 db migration:如何修复ActiveRecord :: ConcurrentMigrationError

时间:2017-10-27 13:44:44

标签: ruby postgresql activerecord ruby-on-rails-5

之前的rake db:rollback停滞不前。现在,在尝试新迁移时,我们收到以下错误:

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)

我们正在使用Postresql

4 个答案:

答案 0 :(得分:14)

在Rails 5中添加了通知锁定,以防止在迁移期间发生意外的并发错误。修复是清除留在原地的数据库锁。

通过针对您的数据库运行此SQL来检查锁:

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

要清除锁定,请针对您的数据库运行此SQL:

select pg_advisory_unlock({the pid of the lock you want to release})

答案 1 :(得分:4)

对我来说,这种解决方法是这样的:

选择咨询锁:

SELECT pid, locktype, mode FROM pg_locks WHERE locktype = 'advisory';
SELECT pg_terminate_backend(<PID>);

答案 2 :(得分:2)

所以在我看来,查询是不同的

With HTMLDoc.getElementsByName("UFG.USER_TYPES")(0)
    .Children(0).Selected = True
    .Children(1).Selected = True
    .Children(2).Selected = True
End With

这基本上会告诉您pids

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.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

,然后您可以使用以下命令简单地解锁pid: 0 years 0 mons 0 days 0 hours 0 mins -0.01005 secs 360 true PostgreSQL JDBC Driver 2019-04-03 16:57:16.873609 2019-04-03 16:58:00.531675 2019-04-03 16:58:00.541727 active 2019-04-03 16:58:00.541725 SELECT DISTINCT age(now(), query_start) AS age, pg_stat_acti 17272 true "" <insufficient privilege> 22640 true "" <insufficient privilege> 29466 true "" <insufficient privilege>

例如:

select pg_advisory_unlock(#{target_pid})

干杯!

答案 3 :(得分:0)

如果您正在使用数据库中的数据进行操作,而不是使用表格和列设置,那么一次rake任务可能是更好的地方吗?