Skip to content

Instantly share code, notes, and snippets.

@qw3r
Forked from trekdemo/drop_connections.rake
Created November 13, 2012 15:10

Revisions

  1. @trekdemo trekdemo revised this gist Nov 13, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion drop_connections.rake
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    namespace :db do
    desc 'Drop all database connections'
    task :drop_connections => :environment do
    database = Procurementtool::Application.config.database_configuration[RAILS_ENV]["database"]
    database = Rails::Application.config.database_configuration[RAILS_ENV]["database"]
    field = if ActiveRecord::Base.connection.send( :postgresql_version ) < 90200
    'pg_stat_activity.procpic' # PostgreSQL <= 9.1.x
    else
  2. @trekdemo trekdemo revised this gist Nov 13, 2012. 1 changed file with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions drop_connections.rake
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,11 @@ namespace :db do
    desc 'Drop all database connections'
    task :drop_connections => :environment do
    database = Procurementtool::Application.config.database_configuration[RAILS_ENV]["database"]

    field = if ActiveRecord::Base.connection.send( :postgresql_version ) < 90200
    'pg_stat_activity.procpic' # PostgreSQL <= 9.1.x
    else
    'pg_stat_activity.pid' # PostgreSQL >= 9.2.x
    end
    field = if ActiveRecord::Base.connection.send( :postgresql_version ) < 90200
    'pg_stat_activity.procpic' # PostgreSQL <= 9.1.x
    else
    'pg_stat_activity.pid' # PostgreSQL >= 9.2.x
    end

    begin
    ActiveRecord::Base.connection.execute <<-SQL
  3. @trekdemo trekdemo created this gist Nov 13, 2012.
    22 changes: 22 additions & 0 deletions drop_connections.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    namespace :db do
    desc 'Drop all database connections'
    task :drop_connections => :environment do
    database = Procurementtool::Application.config.database_configuration[RAILS_ENV]["database"]

    field = if ActiveRecord::Base.connection.send( :postgresql_version ) < 90200
    'pg_stat_activity.procpic' # PostgreSQL <= 9.1.x
    else
    'pg_stat_activity.pid' # PostgreSQL >= 9.2.x
    end

    begin
    ActiveRecord::Base.connection.execute <<-SQL
    SELECT pg_terminate_backend(#{field})
    FROM pg_stat_activity
    WHERE pg_stat_activity.datname = '#{database}';
    SQL
    rescue ActiveRecord::ActiveRecordError => e
    puts 'Connection lost to the database'
    end
    end
    end