Class: Dister::DbAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/dister/db_adapter.rb

Overview

Wrapper class for the database adapter specified in the application’s database.yml. Also handles database credentials, and dump/restore. Currently this only works with ActiveRecord.

Instance Method Summary collapse

Constructor Details

#initialize(db_config_file, dump = nil) ⇒ DbAdapter

Initialize a new DbAdapter. May raise exceptions if the input is erronous.

Parameters:

  • db_config_file (String)

    path to the database configuration (.yml)

  • dump (String) (defaults to: nil)

    filename of dump



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dister/db_adapter.rb', line 12

def initialize db_config_file, dump=nil
  config = YAML.load_file(db_config_file)
  if !config.has_key?("production")
    STDERR.puts "There's no configuration for the production environment"
  end

  @adapter  = config["production"]["adapter"]
  @user     = config["production"]["username"]
  @password = config["production"]["password"]
  @dbname   = config["production"]["adapter"]
  @dump     = dump

  filename = File.expand_path("../../adapters/#{@adapter}.yml", __FILE__)
  raise "There's no adapter for #{@adapter}" if !File.exists?(filename)

  @adapter_config = YAML.load_file(filename)
end

Instance Method Details

#cmdline_tool



38
39
40
# File 'lib/dister/db_adapter.rb', line 38

def cmdline_tool
  @adapter_config["cmdline_tool"]
end

#create_user_cmd



50
51
52
# File 'lib/dister/db_adapter.rb', line 50

def create_user_cmd
  compile_cmd @adapter_config["create_user_cmd"]
end

#daemon_name



46
47
48
# File 'lib/dister/db_adapter.rb', line 46

def daemon_name
  @adapter_config["daemon_name"]
end

#has_dump?Boolean

Checks if there is a db dump

Returns:

  • (Boolean)

    true if there is a dump file, false otherwise



33
34
35
36
# File 'lib/dister/db_adapter.rb', line 33

def has_dump?
  return false if @dump.nil?
  return File.exists? @dump
end

#packages



42
43
44
# File 'lib/dister/db_adapter.rb', line 42

def packages
  @adapter_config["packages"]
end

#restore_dump_cmd



54
55
56
# File 'lib/dister/db_adapter.rb', line 54

def restore_dump_cmd
  compile_cmd @adapter_config["restore_dump_cmd"]
end