Module: DatabaseConnectionHelpers
- Included in:
- Databases::Management, Inform::Persistence
- Defined in:
- lib/runtime/database.rb
Overview
The DatabaseConnectionHelpers module
Constant Summary collapse
- ConnectionAttempts =
Struct.new(:memo).new(0)
- DefaultConnectionOptions =
{ loggerLevel: 'OFF' }.freeze
- DefaultConnectionPoolSize =
4- DefaultConnectionValidationTimeoutSeconds =
240- URLTemplate =
'%<scheme>s://%<host>s%<colon>s%<port>s'.freeze
- KeyValueTemplate =
'%<key>s=%<value>s'.freeze
- EmptyString =
''.freeze
- AmpersandString =
'&'.freeze
- ColonString =
':'.freeze
- ForwardSlashString =
'/'.freeze
- QuestionMarkString =
'?'.freeze
- JavaPattern =
/java/i.freeze
- DatabaseOrRoleDoesNotExistPattern =
rubocop: enable Metrics/AbcSize rubocop: enable Metrics/CyclomaticComplexity rubocop: enable Metrics/MethodLength rubocop: enable Metrics/PerceivedComplexity
/(database|role) ".*" does not exist/.freeze
Instance Method Summary collapse
-
#assemble_url ⇒ Object
Previously: url = “#adapter://#host/#database?user=#username” url << “&password=#password” unless password.nil? || password.empty? url << “&loggerLevel=OFF” rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/MethodLength rubocop: disable Metrics/PerceivedComplexity.
-
#connect(database = config.fetch('database', nil), url = assemble_url, connection_options = {}) ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/MethodLength.
-
#sanitize_url(url) ⇒ Object
rubocop: enable Metrics/AbcSize rubocop: enable Metrics/MethodLength.
Instance Method Details
#assemble_url ⇒ Object
Previously: url = “#adapter://#host/#database?user=#username” url << “&password=#password” unless password.nil? || password.empty? url << “&loggerLevel=OFF” rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/MethodLength rubocop: disable Metrics/PerceivedComplexity
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/runtime/database.rb', line 149 def assemble_url adapter = config.fetch('adapter') adapter.gsub!(/^postgres/, 'jdbc:postgresql') if JavaPattern.match?(RUBY_PLATFORM) host = config.fetch('host', nil) port = config.fetch('port', nil) colon = port.nil? ? EmptyString : ColonString database = config.fetch('database', nil) username = config.fetch('username', nil) password = config.fetch('password', nil) parameters = DefaultConnectionOptions.dup parameters.merge!(user: username) unless username.nil? parameters.merge!(password: password) unless password.nil? || password.empty? parameter_values = parameters.map { |key, value| format(KeyValueTemplate, key: key, value: value) } query = parameter_values.join(AmpersandString) url = [format(URLTemplate, scheme: adapter, host: host, colon: colon, port: port)] url << database unless database.nil? || database.empty? url = [url.join(ForwardSlashString)] url << query unless query.nil? || query.empty? url.join(QuestionMarkString) end |
#connect(database = config.fetch('database', nil), url = assemble_url, connection_options = {}) ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/MethodLength
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/runtime/database.rb', line 178 def connect(database = config.fetch('database', nil), url = assemble_url, = {}) log.debug "Connecting to database #{sanitize_url(url)}" if defined? log ConnectionAttempts.memo += 1 [:max_connections] = config.fetch('pool', DefaultConnectionPoolSize) connection = Sequel.connect(url, **) connection.extension(:connection_validator) connection.pool.connection_validation_timeout = DefaultConnectionValidationTimeoutSeconds connection.test_connection log.debug "Connected to database #{database}" if defined? log Sequel::Model.require_valid_table = false Sequel::Model.db = connection rescue Sequel::DatabaseConnectionError => e raise if e.cause.is_a?(PG::ConnectionBad) && DatabaseOrRoleDoesNotExistPattern.match?(e.cause.to_s) = e..gsub(/Java::OrgPostgresqlUtil::PSQLException: /, '') = "Error connecting to database: #{}" log.error abort rescue Sequel::AdapterNotFound => e = "Adapter not found: #{e.}" log.error abort rescue StandardError => e = "Unexpected error connecting to database: #{e.}" log.error , e abort end |
#sanitize_url(url) ⇒ Object
rubocop: enable Metrics/AbcSize rubocop: enable Metrics/MethodLength
207 208 209 |
# File 'lib/runtime/database.rb', line 207 def sanitize_url(url) url.gsub(/&?(password|loggerLevel)=[^&]+&?/, '').gsub(/\?$/, '') end |