【问题标题】:Cucumber and RSpec testing with zeus: Postgres is being accessed by other users使用 zeus 进行 Cucumber 和 RSpec 测试:其他用户正在访问 Postgres
【发布时间】:2013-07-12 13:04:03
【问题描述】:

在我的 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 所描述的,它需要整个不确定的马戏团来尝试终止数据库连接才能使其再次工作。但这并不总是有效,而且也是一个很大的麻烦。对此必须有更好的解决方案。有人知道吗?

【问题讨论】:

  • 是的,我已经检查过了。我的项目中没有autorun
  • 它工作的事实有时表明 postgres 会话关闭正在完成,但正在异步发生,并且运行下一个测试的 zeus 相关加速足以阻止在正常情况下完成。然而,如果是这样的话,很难相信没有更多关于它的文章。我不知道它是否相关,但你可能想看看platformonrails.wordpress.com/2013/04/06/…
  • 这是惊人的任意性。我刚刚尝试了博客文章中的custom_plan.rb,一切运行良好……我可以运行 Cucumber、RSpec 和所有东西。现在我刚刚重新启动了我的 MacBook 并尝试运行 zeus rake spec 并再次收到错误消息。尽管这是重启后的第一个 zeus 命令。现在我完全困惑了。有人吗?

标签: ruby-on-rails postgresql rspec cucumber zeus


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    namespace :db do
    
      desc 'Clear the database'
      task :clear_db => :environment do |t,args|
        ActiveRecord::Base.establish_connection
        ActiveRecord::Base.connection.tables.each do |table|
          next if table == 'schema_migrations'
          ActiveRecord::Base.connection.execute("TRUNCATE #{table}")
        end
      end
    
      desc 'Delete all tables (but not the database)'
      task :drop_schema => :environment do |t,args|
        ActiveRecord::Base.establish_connection
        ActiveRecord::Base.connection.execute("DROP SCHEMA public CASCADE")
        ActiveRecord::Base.connection.execute("CREATE SCHEMA public")
        ActiveRecord::Base.connection.execute("GRANT ALL ON SCHEMA public TO postgres")
        ActiveRecord::Base.connection.execute("GRANT ALL ON SCHEMA public TO public")
        ActiveRecord::Base.connection.execute("COMMENT ON SCHEMA public IS 'standard public schema'")
      end
    
      desc 'Recreate the database and seed'
      task :redo_db => :environment do |t,args|
        # Executes the dependencies, but only once
        Rake::Task["db:drop_schema"].invoke
        Rake::Task["db:migrate"].invoke
        Rake::Task["db:migrate:status"].invoke
        Rake::Task["db:structure:dump"].invoke
        Rake::Task["db:seed"].invoke
      end
    
    end
    

    【讨论】:

      猜你喜欢
      • 2013-05-15
      • 2011-11-12
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多