Class: Pgbus::Generators::DatabaseTargetDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/pgbus/generators/database_target_detector.rb

Overview

Detects whether the host application has configured pgbus to use a separate database via connects_to, and returns the database name that –database flags should be set to for sub-generators.

Detection sources, in priority order:

1. Pgbus.configuration.connects_to (runtime — authoritative if
   the initializer has already booted)
2. config/initializers/pgbus.rb (text scan)
3. config/application.rb (text scan — fallback for apps that
   call connects_to in application config instead of the pgbus
   initializer)

Returns a String (the database name, e.g. “pgbus”) or nil if no separate database is configured.

Constant Summary collapse

CONNECTS_TO_PATTERN =

Matches:

c.connects_to = { database: { writing: :pgbus } }
c.connects_to(database: { writing: :queue_db })
Pgbus.configuration.connects_to = { database: { writing: :pgbus } }

Rejects:

c.connects_to = { role: :writing }        (different API shape)
connects_to :something                    (no database: key)

The [^;n]*? and [^}]*? laziness keeps the scan within a reasonable window so a stray “writing:” later in the file can’t cross-contaminate.

/connects_to\b[^;\n]*?database\s*:\s*\{[^}]*?writing\s*:\s*:?(?<name>[a-zA-Z_][a-zA-Z0-9_]*)/m

Instance Method Summary collapse

Constructor Details

#initialize(destination_root:) ⇒ DatabaseTargetDetector

Returns a new instance of DatabaseTargetDetector.



35
36
37
# File 'lib/pgbus/generators/database_target_detector.rb', line 35

def initialize(destination_root:)
  @destination_root = destination_root
end

Instance Method Details

#detectObject

Returns the database name string if a separate DB is configured, nil otherwise. Checks runtime config first, then falls back to static file scanning.



42
43
44
# File 'lib/pgbus/generators/database_target_detector.rb', line 42

def detect
  runtime_database_name || scan_initializer || scan_application_config
end