ruby-on-rails - 使用 zeus : Postgres is being accessed by other users 进行 cucumber 和 RSpec 测试

标签 ruby-on-rails postgresql rspec cucumber zeus

在我的 Rails 3.2.13 应用程序中,我使用的是 Zeus。在测试环境中,我使用 PostgreSQL。当我运行 Cucumber 然后运行 ​​RSpec(或相反)时,10 次中有 9 次我收到消息:

PG::Error: ERROR:  database "bp_test" is being accessed by other users
DETAIL:  There are 1 other session(s) using the database.
: DROP DATABASE IF EXISTS "bp_test"

Tasks: TOP => db:test:load => db:test:purge
(See full trace by running task with --trace)

here 所述,需要尝试终止数据库连接才能使其再次工作,这需要整个非确定性的马戏团。 .但这并不总是有效,而且也是一个很大的麻烦。必须有更好的解决方案。有人知道吗?

最佳答案

灵感来自 this answer ,我们创建了以下 database.rake 文件。在原始答案仅适用于 PostgreSQL 9.1 的地方,这个答案被修改为也适用于 PostgreSQL 9.2。该机制不是最漂亮的:当 9.1 命令失败时,它只是执行 9.2 命令。但最重要的是:它有效!

#{Rails.root}/lib/tasks/databases.rake
# monkey patch ActiveRecord to avoid There are n other session(s) using the database.
def drop_database(config)
  case config['adapter']
  when /mysql/
    ActiveRecord::Base.establish_connection(config)
    ActiveRecord::Base.connection.drop_database config['database']
  when /sqlite/
    require 'pathname'
    path = Pathname.new(config['database'])
    file = path.absolute? ? path.to_s : File.join(Rails.root, path)

    FileUtils.rm(file)
  when /postgresql/
    begin
      ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
      ActiveRecord::Base.connection.select_all("select * from pg_stat_activity order by procpid;").each do |x|
        if config['database'] == x['datname'] && x['current_query'] =~ /<IDLE>/
          ActiveRecord::Base.connection.execute("select pg_terminate_backend(#{x['procpid']})")
        end
      end
      ActiveRecord::Base.connection.drop_database config['database']
    rescue # in PG 9.2 column procpid was renamed pid and the query status is checked not using 'current_query' but using 'state'
      ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
      ActiveRecord::Base.connection.select_all("select * from pg_stat_activity order by pid;").each do |x|
        if config['database'] == x['datname'] && x['state'] =~ /idle/
          ActiveRecord::Base.connection.execute("select pg_terminate_backend(#{x['pid']})")
        end
      end
      ActiveRecord::Base.connection.drop_database config['database']
    end
  end
end

关于ruby-on-rails - 使用 zeus : Postgres is being accessed by other users 进行 cucumber 和 RSpec 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17615574/

相关文章:

mysql - 推荐 "to optimize the response time of an SQL query"

ruby-on-rails - 将 database_cleaner 与 Rails/Spork/RSpec 一起使用时出现 SQLite3::SQLException

ruby-on-rails - 在验收规范中测试或不测试什么?

ruby-on-rails - Rspec:验证失败:名称已被占用

java - PSQL异常 : ERROR: duplicate key value violates unique constraint

postgresql - 使用 Postgres,我想要查询中字符串中特定格式的时间戳

ruby-on-rails - Rails 验证,知道哪个字段无效?

ruby-on-rails - Rails 模型验证 1 个字

ruby-on-rails - 设置自定义规则

python - 无法安装 psycopg2 Ubuntu