Monkey patch activerecord-3.0.9/lib/active_record/railties/databases.rake enables parallel_tests to work on my PG Jenkins CI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# #{Rails.root}/lib/tasks/databases.rake | |
=begin | |
Monkey Patch | |
activerecord-3.0.9/lib/active_record/railties/databases.rake | |
clears obstinate stale PG session to get parallel_tests working | |
also, PG user must be superuser to use these low level PG functions | |
=end | |
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/ | |
ActiveRecord::Base.establish_connection(config) | |
ActiveRecord::Base.connection.select_all("select * from pg_stat_activity order by pid;").each do |x| | |
if config['database'] == x['datname'] && x['current_query'] =~ /<IDLE>/ | |
ActiveRecord::Base.connection.execute("select pg_terminate_backend(#{x['pid']})") | |
end | |
end | |
ActiveRecord::Base.connection.close | |
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public')) | |
ActiveRecord::Base.connection.drop_database config['database'] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment