Module: Schked::DatabaseConnection
- Defined in:
- lib/schked/database_connection.rb
Overview
Auto-detects which Schked::Adapters::* class to use for the
database-backed coordination store. Returns a concrete JobRunStore
instance — the caller should not need to know which backend is in play.
Resolution order:
- An explicit connection/pool if provided.
ActiveRecord::Base.connection_poolwhen ActiveRecord is loaded and configured (the pool, not a single connection, so the adapter can check out per operation).- Sequel's first available
Sequel::DATABASESdatabase. - Otherwise raises a clear
NotFoundErrortelling the operator to setdatabase_connectionexplicitly.
Defined Under Namespace
Classes: NotFoundError
Class Method Summary collapse
- .auto_detect_connection ⇒ Object
- .build_for(raw, logger: Logger.new($stdout)) ⇒ Object
- .detect(connection: nil, logger: Logger.new($stdout)) ⇒ Object
- .wrap(raw, logger: Logger.new($stdout)) ⇒ Object
Class Method Details
.auto_detect_connection ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/schked/database_connection.rb', line 55 def auto_detect_connection if defined?(ActiveRecord) && ActiveRecord.const_defined?(:Base) begin return ActiveRecord::Base.connection_pool rescue # ActiveRecord is loaded but not configured (or the pool cannot # be resolved) — fall through to Sequel detection below. end end if defined?(Sequel) && Sequel.respond_to?(:DATABASES) && Sequel::DATABASES.any? return Sequel::DATABASES.first end raise NotFoundError, "Schked could not detect a database connection for the database-backed " \ "job run store. Load ActiveRecord or Sequel, or set `Schked.config.database_connection` " \ "explicitly." end |
.build_for(raw, logger: Logger.new($stdout)) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/schked/database_connection.rb', line 35 def build_for(raw, logger: Logger.new($stdout)) if defined?(::Sequel::Database) && raw.is_a?(::Sequel::Database) Adapters::Sequel.new(raw, logger: logger) elsif raw.respond_to?(:with_connection) # An ActiveRecord connection pool. Adapters::ActiveRecord.new(raw, logger: logger) elsif raw.respond_to?(:pool) && raw.respond_to?(:adapter_name) # A concrete ActiveRecord adapter connection — normalize to its pool. Adapters::ActiveRecord.new(raw.pool, logger: logger) elsif raw.respond_to?(:claim) && raw.respond_to?(:cleanup) # Already a store: a pre-built adapter or a custom object. raw else raise NotFoundError, "Schked could not detect an adapter for: #{raw.class}. " \ "Provide a Sequel::Database, an ActiveRecord connection pool, " \ "or an ActiveRecord connection (PostgreSQL, Mysql2, or Trilogy)." end end |
.detect(connection: nil, logger: Logger.new($stdout)) ⇒ Object
21 22 23 24 |
# File 'lib/schked/database_connection.rb', line 21 def detect(connection: nil, logger: Logger.new($stdout)) raw = connection || auto_detect_connection wrap(raw, logger: logger) end |
.wrap(raw, logger: Logger.new($stdout)) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/schked/database_connection.rb', line 26 def wrap(raw, logger: Logger.new($stdout)) case raw when Adapters::Sequel, Adapters::ActiveRecord raw else build_for(raw, logger: logger) end end |