Skip to content

Commit

Permalink
Remove calls to ApplicationRecord.establish_connection
Browse files Browse the repository at this point in the history
  • Loading branch information
adeherdt-r7 committed Nov 15, 2024
1 parent 25f8592 commit 240351d
Showing 1 changed file with 5 additions and 21 deletions.
26 changes: 5 additions & 21 deletions lib/msf/core/db_manager/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 240351d

Please sign in to comment.