2021-02-24 11:03:36 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
module ActiveRecord
|
|
|
|
module Tasks
|
2021-02-24 11:03:36 +01:00
|
|
|
# The following magic allows to drop a PG database even if a connection exists
|
|
|
|
# @see https://stackoverflow.com/a/38710021
|
2016-03-23 18:39:41 +01:00
|
|
|
class PostgreSQLDatabaseTasks
|
2021-02-24 11:03:36 +01:00
|
|
|
include ActiveRecord::Sanitization::ClassMethods
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def drop
|
|
|
|
establish_master_connection
|
2021-02-24 11:03:36 +01:00
|
|
|
q = sanitize_sql_array [
|
|
|
|
"select pg_terminate_backend(pg_stat_activity.pid) from pg_stat_activity where datname= ? AND state='idle';",
|
|
|
|
configuration['database']
|
|
|
|
]
|
|
|
|
connection.select_all q
|
2016-03-23 18:39:41 +01:00
|
|
|
connection.drop_database configuration['database']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-02-24 11:03:36 +01:00
|
|
|
end
|