Module: Libertree::DB

Defined in:
lib/libertree/db.rb

Constant Summary collapse

LAST_MIGRATION =
"2014-08-31-2050-search-indices.sql"

Class Method Summary collapse

Class Method Details

.configObject



8
9
10
# File 'lib/libertree/db.rb', line 8

def self.config
  @config
end

.config=(hash) ⇒ Object



12
13
14
# File 'lib/libertree/db.rb', line 12

def self.config=(hash)
  @config = hash
end

.dbhObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/libertree/db.rb', line 23

def self.dbh
  @dbh ||= Sequel.postgres(host:     config['host'],
                           database: config['database'],
                           user:     config['username'],
                           password: config['password'],
                           max_connections: 8)

  # ensure the latest required migration exists
  if @dbh[:schema_migrations].where(filename: LAST_MIGRATION).count == 0
    fail "Libertree::DB: migration `#{LAST_MIGRATION}' not found.  Please update libertree-db."
  end
  @dbh
end

.load_config(filename) ⇒ Object



16
17
18
19
20
21
# File 'lib/libertree/db.rb', line 16

def self.load_config(filename)
  config_file = filename
  configs ||= YAML.load( IO.read(config_file) )
  env = ENV['LIBERTREE_ENV'] || 'development'
  @config = configs[env]
end