Class: Infrastructure::Databases

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/infrastructure/databases.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configurable

#after_assignment, #after_configure, #before_assignment, #before_configure, #callback, #callbacks, #configuration, #configuration_accessor, included

Constructor Details

#initialize(orm, environment) ⇒ Databases

Returns a new instance of Databases.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/infrastructure/databases.rb', line 9

def initialize(orm, environment)
  @orm = orm
  @environment = environment.to_sym
  @global_defaults = {}
  @environment_defaults = {}
  @system = {
    id: 'system'
   }
  callback :after_configure, -> { merge_defaults }
  callback :after_assignment, -> (key, value) { merge_defaults }
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



6
7
8
# File 'lib/infrastructure/databases.rb', line 6

def environment
  @environment
end

#global_defaultsObject (readonly)

Returns the value of attribute global_defaults.



7
8
9
# File 'lib/infrastructure/databases.rb', line 7

def global_defaults
  @global_defaults
end

#ormObject (readonly)

Returns the value of attribute orm.



6
7
8
# File 'lib/infrastructure/databases.rb', line 6

def orm
  @orm
end

#systemObject (readonly)

Returns the value of attribute system.



7
8
9
# File 'lib/infrastructure/databases.rb', line 7

def system
  @system
end

Instance Method Details

#availableObject



43
44
45
# File 'lib/infrastructure/databases.rb', line 43

def available
  connection.execute("SELECT datname FROM pg_database").map { |result| result['datname'] }
end

#available?(id) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/infrastructure/databases.rb', line 39

def available?(id)
  available.include? database(id)
end

#connect(id, params = {}) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/infrastructure/databases.rb', line 99

def connect(id, params = {})
  config = config(id)
  unless params[:fallback]
    raise ArgumentError, "database configuration for #{id} in environment #{environment} is not configured" if config[:id] != id.to_s
  end
  orm.establish_connection config
end

#connect!(id, params = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/infrastructure/databases.rb', line 107

def connect!(id, params = {})
  begin
    connect id, params
  rescue StandardError
    connect system
    create id
    connect id, params
  end
end

#connectionObject



47
48
49
# File 'lib/infrastructure/databases.rb', line 47

def connection
  orm.connection
end

#create(id) ⇒ Object



95
96
97
# File 'lib/infrastructure/databases.rb', line 95

def create(id)
  connection.create_database database(id)
end

#databases(environment = environment) ⇒ Object



117
118
119
# File 'lib/infrastructure/databases.rb', line 117

def databases(environment = environment)
  configs(environment).map { |id, configs| configs[:database] }.compact.uniq.sort
end

#drop(id) ⇒ Object



91
92
93
# File 'lib/infrastructure/databases.rb', line 91

def drop(id)
  connection.drop_database database(id)
end

#environment_config(id) ⇒ Object Also known as: config

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/infrastructure/databases.rb', line 70

def environment_config(id)
  if id.is_a?(String) || id.is_a?(Symbol)
    id = id.to_sym
    if config = configs.fetch(id)
      return config.to_h 
    end
    if id.to_s == defaults[:id]
      if config = configs.fetch(defaults[:id])
        return config.to_h
      else
        raise ArgumentError, "default database #{defaults[:id]} in environment #{environment} is not configured"
      end
    end
    return system if id == :system
  elsif id.is_a? Hash
    return id
  end
  raise ArgumentError, "database configuration for #{id} in environment #{environment} is not configured"
end

#environment_configs(environment = environment) ⇒ Object Also known as: configs



55
56
57
# File 'lib/infrastructure/databases.rb', line 55

def environment_configs(environment = environment)
  fetch(environment) || raise(ArgumentError, "database configurations for environment #{environment} are not configured")
end

#environment_database(id) ⇒ Object Also known as: database



60
61
62
# File 'lib/infrastructure/databases.rb', line 60

def environment_database(id)
  config(id)[:database]
end

#environment_defaults(environment = environment) ⇒ Object Also known as: defaults



65
66
67
# File 'lib/infrastructure/databases.rb', line 65

def environment_defaults(environment = environment)
  @environment_defaults[environment] ||= Hash.new
end

#merge_defaultsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/infrastructure/databases.rb', line 21

def merge_defaults
  @global_defaults = @global_defaults.merge(configuration.fetch(:defaults) || {})
  configuration.delete :defaults
  @system = @system.merge(configuration.fetch(:system) || {}).merge @global_defaults
  configuration.delete :system
  configuration.each do |environment, configs|
    @environment_defaults[environment] ||= Hash.new
    @environment_defaults[environment] = @environment_defaults[environment].merge(configs.fetch(:defaults) || {})
    configs.delete :defaults
    configs.each do |id, config|
      config.assign :database, "#{environment}_#{id}" unless config.fetch :database
      config.assign :id, id.to_s unless config.fetch(:id) == id.to_s
      @environment_defaults[environment].each { |k,v| config.assign k, v unless config.fetch k }
      @global_defaults.each { |k,v| config.assign k, v unless config.fetch k }
    end
  end
end

#tablesObject



51
52
53
# File 'lib/infrastructure/databases.rb', line 51

def tables
  connection.tables
end