Skip to content

Instantly share code, notes, and snippets.

@ichi
Created January 25, 2016 05:36
Show Gist options
  • Save ichi/56b1c379bb8697edb7c7 to your computer and use it in GitHub Desktop.
Save ichi/56b1c379bb8697edb7c7 to your computer and use it in GitHub Desktop.
postgresのセッションぶっとばすrakeタスク。 test環境にするならRAILS_ENV=testの指定も忘れずにね。
# lib/tasks/postgres.rake
namespace :db do
namespace :pg do
namespace :session do
desc 'reset postgres session/connection'
task :reset do
sql = %{
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = '#{ActiveRecord::Base.connection_config[:database]}'
AND pid <> pg_backend_pid();
}
ActiveRecord::Base.connection.execute(sql)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment