Class: Shogun::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/shogun/database.rb

Constant Summary collapse

URL_NAME =
"DATABASE_URL"
TIMEZONE_NAME =
"DATABASE_TIMEZONE"
REAP_FREQUENCY_NAME =
"DATABASE_REAP_FREQUENCY"
POOL_NAME =
"DATABASE_POOL"
TIMEOUT_NAME =
"DATABASE_TIMEOUT"
URL =
ENV[URL_NAME]
TIMEZONE =
ENV[TIMEZONE_NAME]
REAP_FREQUENCY =
ENV[REAP_FREQUENCY_NAME]
POOL =
ENV[POOL_NAME]
TIMEOUT =
ENV[TIMEOUT_NAME]
DEFAULT_TIMEOUT =
20
DEFAULT_REAP_FREQUENCY =
15
DEFAULT_POOL =
10
DEFAULT_TIMEZONE =
"utc"
CONFIGURATION =
{
  "reaping_frequency" => Integer(REAP_FREQUENCY || DEFAULT_REAP_FREQUENCY),
  "pool" => Integer(POOL || DEFAULT_POOL),
  "connect_timeout" => Integer(TIMEOUT || DEFAULT_TIMEOUT)
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger:) ⇒ Database

Returns a new instance of Database.



34
35
36
37
38
39
40
41
42
# File 'lib/shogun/database.rb', line 34

def initialize(logger:)
  @logger = logger
  ActiveRecord::Base.time_zone_aware_attributes = true
  ActiveRecord::Base.default_timezone = (timezone || default_timezone).to_sym
  ActiveRecord::Base.logger = @logger
  ActiveRecord::LogSubscriber.logger = @logger
  ActiveRecord::LogSubscriber.colorize_logging = false
  ActiveRecord::Base.establish_connection(uri)
end

Class Method Details

.connectionObject



23
24
25
# File 'lib/shogun/database.rb', line 23

def self.connection
  ActiveRecord::Base.connection
end

.setup!(logger:) ⇒ Object



27
28
29
30
31
32
# File 'lib/shogun/database.rb', line 27

def self.setup!(logger:)
  new(logger: logger).tap do
    connection.enable_extension("uuid-ossp")
    ActiveRecord::Base.descendants.each(&:setup!)
  end
end