Class: Worktree::DbManager

Inherits:
Object
  • Object
show all
Defined in:
lib/worktree/db_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec_file, environment = 'development') ⇒ DbManager

Returns a new instance of DbManager.



9
10
11
12
# File 'lib/worktree/db_manager.rb', line 9

def initialize(spec_file, environment = 'development')
  @spec = YAML.load_file(spec_file)
  @environment = environment
end

Instance Attribute Details

#specObject (readonly)

Returns the value of attribute spec.



7
8
9
# File 'lib/worktree/db_manager.rb', line 7

def spec
  @spec
end

Instance Method Details

#createdb!(db_name) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/worktree/db_manager.rb', line 46

def createdb!(db_name)
  cmd = if db_port
          "createdb -h #{db_host} -p #{db_port} -T #{template} #{db_name}"
        else
          "createdb -h #{db_host} -T #{template} #{db_name}"
        end
  Worktree.run_command cmd
end

#db_hostObject



30
31
32
33
34
35
36
# File 'lib/worktree/db_manager.rb', line 30

def db_host
  if multi?
    environment_spec.dig('primary', 'host')
  else
    environment_spec['host']
  end
end

#db_portObject



22
23
24
25
26
27
28
# File 'lib/worktree/db_manager.rb', line 22

def db_port
  if multi?
    environment_spec.dig('primary', 'port')
  else
    environment_spec['port']
  end
end

#dropdb!Object



55
56
57
58
59
60
61
62
# File 'lib/worktree/db_manager.rb', line 55

def dropdb!
  cmd = if db_port
          "dropdb -h #{db_host} -p #{db_port} #{template}"
        else
          "dropdb -h #{db_host} #{template}"
        end
  Worktree.run_command cmd
end

#environment_specObject



14
15
16
# File 'lib/worktree/db_manager.rb', line 14

def environment_spec
  @spec.fetch(@environment, {})
end

#multi?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/worktree/db_manager.rb', line 18

def multi?
  environment_spec.key? 'primary'
end

#templateObject



38
39
40
41
42
43
44
# File 'lib/worktree/db_manager.rb', line 38

def template
  if multi?
    environment_spec.dig('primary', 'database')
  else
    environment_spec['database']
  end
end