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

后端 未结 4 1593
星月不相逢
星月不相逢 2021-01-07 18:36

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

rake aborted!
ActiveRecord::ConcurrentMigrationError: 

         


        
4条回答
  •  耶瑟儿~
    2021-01-07 19:13

    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})
    

提交回复
热议问题