Module: Dbviewer::ConnectionManagement
- Extended by:
- ActiveSupport::Concern
- Included in:
- DatabaseOperations
- Defined in:
- app/controllers/concerns/dbviewer/connection_management.rb
Instance Method Summary collapse
-
#available_connections ⇒ Object
Get list of available connections.
-
#current_connection_key ⇒ Object
Get the current active connection key.
-
#switch_connection(connection_key) ⇒ Object
Set the current connection to use.
Instance Method Details
#available_connections ⇒ Object
Get list of available connections
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/controllers/concerns/dbviewer/connection_management.rb', line 53 def available_connections connections = Dbviewer.configuration.database_connections.map do |key, config| # Try to determine the adapter name if it's not already stored adapter_name = nil if config[:adapter_name].present? adapter_name = config[:adapter_name] elsif config[:connection].present? begin adapter_name = config[:connection].connection.adapter_name rescue => e Rails.logger.error("Error getting adapter name: #{e.}") end end { key: key, name: config[:name] || key.to_s.humanize, adapter_name: adapter_name, current: key.to_sym == current_connection_key.to_sym } end # Ensure at least one connection is marked as current unless connections.any? { |c| c[:current] } # If no connection is current, mark the first one as current if connections.any? connections.first[:current] = true # Also update the session session[:dbviewer_connection] = connections.first[:key] end end connections end |
#current_connection_key ⇒ Object
Get the current active connection key
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'app/controllers/concerns/dbviewer/connection_management.rb', line 10 def current_connection_key key = session[:dbviewer_connection] || Dbviewer.configuration.current_connection return key.to_sym if key && Dbviewer.configuration.database_connections.key?(key.to_sym) first_key = Dbviewer.configuration.database_connections.keys.first if first_key session[:dbviewer_connection] = first_key return first_key end :default end |
#switch_connection(connection_key) ⇒ Object
Set the current connection to use
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/controllers/concerns/dbviewer/connection_management.rb', line 24 def switch_connection(connection_key) connection_key = connection_key.to_sym if connection_key.respond_to?(:to_sym) if connection_key && Dbviewer.configuration.database_connections.key?(connection_key) session[:dbviewer_connection] = connection_key # Clear the database manager to force it to be recreated with the new connection @database_manager = nil return true else # If the connection key doesn't exist, reset to default connection if Dbviewer.configuration.database_connections.key?(Dbviewer.configuration.current_connection) session[:dbviewer_connection] = Dbviewer.configuration.current_connection @database_manager = nil return true else # If even the default connection isn't valid, try the first available connection first_key = Dbviewer.configuration.database_connections.keys.first if first_key session[:dbviewer_connection] = first_key @database_manager = nil return true end end end false # Return false if we couldn't set a valid connection end |