Skip to content

Instantly share code, notes, and snippets.

@ericboehs
Forked from ak47/databases.rake
Created October 5, 2012 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ericboehs/3838349 to your computer and use it in GitHub Desktop.
Save ericboehs/3838349 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 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.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
ActiveRecord::Base.connection.drop_database config['database']
end
end
@ericboehs
Copy link
Author

"procpid" is now just "pid", "current_query" is now "state" and "" is now just "idle" in Postgres 9.2. I've forked a fix here: https://gist.github.com/3838349

@ericboehs
Copy link
Author

Err "" is now "idle". Can't edit gist comments. :(

@ericboehs
Copy link
Author

Apparently I did type it right. It just gets filtered as github thinks I'm typing HTML.

Also you can't delete comments. ಠ_ಠ

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