From 240351dee04374d776ceb8a1b6c76968ade4fbc7 Mon Sep 17 00:00:00 2001 From: Arne De Herdt Date: Fri, 15 Nov 2024 09:33:43 +0100 Subject: [PATCH] Remove calls to `ApplicationRecord.establish_connection` --- lib/msf/core/db_manager/connection.rb | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/lib/msf/core/db_manager/connection.rb b/lib/msf/core/db_manager/connection.rb index 97c878b8867d..81935e1da55c 100644 --- a/lib/msf/core/db_manager/connection.rb +++ b/lib/msf/core/db_manager/connection.rb @@ -82,31 +82,15 @@ def create_db(opts) begin case opts["adapter"] when 'postgresql' - # Try to force a connection to be made to the database, if it succeeds - # then we know we don't need to create it :) - ApplicationRecord.establish_connection(opts) - # Do the checkout, checkin dance here to make sure this thread doesn't - # hold on to a connection we don't need - conn = ApplicationRecord.connection_pool.checkout - ApplicationRecord.connection_pool.checkin(conn) + existing_db = ::ApplicationRecord.connection_pool.with_connection(&:active) rescue false + ::ApplicationRecord.connection.create_database(opts['database']) unless existing_db + else + ilog("Unknown database adapter: #{opts['adapter']}") end rescue ::Exception => e errstr = e.to_s - if errstr =~ /does not exist/i or errstr =~ /Unknown database/ - ilog("Database doesn't exist \"#{opts['database']}\", attempting to create it.") - ApplicationRecord.establish_connection( - opts.merge( - 'database' => 'postgres', - 'schema_search_path' => 'public' - ) - ) - - ApplicationRecord.connection.create_database(opts['database']) - else - ilog("Trying to continue despite failed database creation: #{e}") - end + ilog("Trying to continue despite failed database creation: #{e}") end - ApplicationRecord.remove_connection end # Checks if the spec passed to `ApplicationRecord.establish_connection` can connect to the database.