Module: Polyrun::Database::UrlBuilder::ConnectionInfer

Defined in:
lib/polyrun/database/url_builder/connection/infer.rb

Overview

Infer canonical adapter name from polyrun.yml databases: hash.

Constant Summary collapse

INFER_ADAPTER_FROM_NESTED =
[
  %w[postgresql postgresql],
  %w[trilogy trilogy],
  %w[mysql2 mysql2],
  %w[mysql mysql2],
  %w[sqlserver sqlserver],
  %w[mssql sqlserver],
  %w[sqlite3 sqlite3],
  %w[sqlite sqlite3],
  %w[mongodb mongodb],
  %w[mongo mongodb]
].freeze

Class Method Summary collapse

Class Method Details

.infer_adapter_name(dh) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/polyrun/database/url_builder/connection/infer.rb', line 21

def infer_adapter_name(dh)
  explicit = (dh["adapter"] || dh[:adapter]).to_s.strip.downcase
  explicit = normalize_adapter_alias(explicit)
  return explicit unless explicit.empty?

  INFER_ADAPTER_FROM_NESTED.each do |key, name|
    return name if nested_hash?(dh, key)
  end
  "postgresql"
end

.nested_hash?(dh, key) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/polyrun/database/url_builder/connection/infer.rb', line 43

def nested_hash?(dh, key)
  dh[key].is_a?(Hash) || dh[key.to_sym].is_a?(Hash)
end

.normalize_adapter_alias(name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/polyrun/database/url_builder/connection/infer.rb', line 32

def normalize_adapter_alias(name)
  case name
  when "postgres", "pg" then "postgresql"
  when "mysql" then "mysql2"
  when "mongo" then "mongodb"
  when "mssql" then "sqlserver"
  when "sqlite" then "sqlite3"
  else name
  end
end