Skip to content

Instantly share code, notes, and snippets.

@ak47
Created July 28, 2011 18:56
Show Gist options
  • Save ak47/1112258 to your computer and use it in GitHub Desktop.
Save ak47/1112258 to your computer and use it in GitHub Desktop.
Monkey patch activerecord-3.0.9/lib/active_record/railties/databases.rake enables parallel_tests to work on my PG Jenkins CI
# #{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.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.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
ActiveRecord::Base.connection.drop_database config['database']
end
end
@jch
Copy link

jch commented Oct 30, 2012

Thanks for the gist! To get this to work with the standalone db:drop task, you can also add:

Rake::Task['environment'].invoke

to the beginning of the method. Otherwise, just invoke the command with:

rake environment db:drop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment