为什么我不能成功使用rake db:drop或rake db:purge? [英] Why can't I successfully use rake db:drop or rake db:purge?

查看:115
本文介绍了为什么我不能成功使用rake db:drop或rake db:purge?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有效命令:




  • rake db:migrate

  • rake db:rollback

  • rake db:seed



不执行的命令:




  • rake db:drop

  • rake db:drop:all

  • rake db:migrate :reset

  • 耙db:purge



如果我使用db:rollback了足够的时间(或使用STEP),我的数据库成功删除,但是当我使用db:drop / db:drop:all时没有成功。 db:migrate:reset和db:purge保持所有数据不变。跟踪运行,查看development.log,系统日志,并通过DB监视器监视DB查询(这些命令似乎都没有运行SQL查询。)也没有错误。



运行:




  • Rails 4.2

  • postgres 9.4

  • CentOS 7


解决方案

如果存在连接,则不能删除PG数据库。 / p>

除非您使用此魔术:

 #config / initializers / postgresql_database_tasks .rb 
模块ActiveRecord
模块任务
类PostgreSQLDatabaseTasks
def drop
Establishment_master_connection
connection.select_all从pg_stat_activity中选择pg_terminate_backend(pg_stat_activity.pid),其中datname ='#{configuration ['database']}'AND state ='idle';
connection.drop_database配置['数据库']
结束
结束
结束
结束

在初始化器中输入并运行以下命令:

  rake environment db:放下

环境参数使rake检查初始化程序。


Commands that work:

  • rake db:migrate
  • rake db:rollback
  • rake db:seed

Commands that don't:

  • rake db:drop
  • rake db:drop:all
  • rake db:migrate:reset
  • rake db:purge

If I use db:rollback enough times (or with STEP), my databases drop successfully, but not when I use db:drop / db:drop:all. db:migrate:reset and db:purge leave all the data untouched. Running with trace, watching development.log, system logs and watching DB queries via the DB monitor (no SQL queries appear to run for those commands.) No errors, either.

Running:

  • Rails 4.2
  • postgres 9.4
  • CentOS 7

解决方案

You can't drop a PG database if a connection exists.

Unless you use this magic:

# config/initializers/postgresql_database_tasks.rb
module ActiveRecord
  module Tasks
    class PostgreSQLDatabaseTasks
      def drop
        establish_master_connection
        connection.select_all "select pg_terminate_backend(pg_stat_activity.pid) from pg_stat_activity where datname='#{configuration['database']}' AND state='idle';"
        connection.drop_database configuration['database']
      end
    end
  end
end

put that in your intializers and run this command:

rake environment db:drop

The environment argument makes rake check the initializers.

这篇关于为什么我不能成功使用rake db:drop或rake db:purge?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆