Class: Duple::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/duple/configuration.rb

Overview

Represents the configuration that will be used to perform the data operations.

This class should be the only place in the system that knows about the structure of the config file.

This class should not have any knowledge of any particular database system. For example, this class can know about the concept of a “tables”, but it should know nothing about flags for PostgreSQL commands.

Constant Summary collapse

HEROKU =
'heroku'
LOCAL =
'local'
VALID_TYPES =
[HEROKU, LOCAL]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_hash, options) ⇒ Configuration

Returns a new instance of Configuration.



19
20
21
22
# File 'lib/duple/configuration.rb', line 19

def initialize(config_hash, options)
  @raw_config = config_hash
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/duple/configuration.rb', line 17

def options
  @options
end

#raw_configObject (readonly)

Returns the value of attribute raw_config.



17
18
19
# File 'lib/duple/configuration.rb', line 17

def raw_config
  @raw_config
end

Instance Method Details

#capture?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/duple/configuration.rb', line 84

def capture?
  options[:capture]
end

#db_config(appname, options = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/duple/configuration.rb', line 128

def db_config(appname, options = nil)
  options ||= {}
  options = {dry_run: false}.merge(options)

  if options[:dry_run]
    db_config_for_dry_run(appname)
  else
    db_config_for_app(appname)
  end
end

#db_config_for_app(appname) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/duple/configuration.rb', line 149

def db_config_for_app(appname)
  env = environments[appname]
  if env['database'].nil?
    raise ArgumentError.new('Invalid config: "database" is required for a local environment.')
  end
  {
    username: env['username'] || 'postgres',
    password: env['password'] || '',
    host:     env['host'] || 'localhost',
    port:     env['port'] || '5432',
    database: env['database']
  }
end

#db_config_for_dry_run(appname) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/duple/configuration.rb', line 139

def db_config_for_dry_run(appname)
  {
    username: "[#{envname}.USER]",
    password: "[#{envname}.PASS]",
    host:     "[#{envname}.HOST]",
    port:     "[#{envname}.PORT]",
    database: "[#{envname}.DB]"
  }
end

#default_source_nameObject



48
49
50
# File 'lib/duple/configuration.rb', line 48

def default_source_name
  env_names_by_flag('default_source', true).first
end

#default_target_nameObject



24
25
26
# File 'lib/duple/configuration.rb', line 24

def default_target_name
  env_names_by_flag('default_target', true).first
end

#dry_run?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/duple/configuration.rb', line 80

def dry_run?
  options[:dry_run]
end

#environment(env_name) ⇒ Object

Raises:

  • (ArgumentError)


167
168
169
170
171
# File 'lib/duple/configuration.rb', line 167

def environment(env_name)
  env = environments[env_name]
  raise ArgumentError.new("Invalid environment: #{env_name}") if env.nil?
  env
end

#environmentsObject



163
164
165
# File 'lib/duple/configuration.rb', line 163

def environments
  raw_config['environments'] || {}
end

#excluded_tablesObject

Returns an array of tables to exclude, based on the group config and the –tables option. The –tables option takes precedence over the –group option, so if a table is excluded from a group, but specified in the –tables option the table will NOT be excluded.



118
119
120
121
122
123
124
125
126
# File 'lib/duple/configuration.rb', line 118

def excluded_tables
  tables = []
  if group_name
    g = group(group_name)
    tables += (g['exclude_tables'] || [])
  end
  tables -= table_names
  tables
end

#filtered_tables?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/duple/configuration.rb', line 96

def filtered_tables?
  included_tables.size > 0 || excluded_tables.size > 0
end

#group(group_name) ⇒ Object

Raises:

  • (ArgumentError)


177
178
179
180
181
# File 'lib/duple/configuration.rb', line 177

def group(group_name)
  group = groups[group_name]
  raise ArgumentError.new("Invalid group: #{group_name}") if group.nil?
  group
end

#group_nameObject



88
89
90
# File 'lib/duple/configuration.rb', line 88

def group_name
  options[:group]
end

#groupsObject



173
174
175
# File 'lib/duple/configuration.rb', line 173

def groups
  raw_config['groups'] || {}
end

#heroku?(env) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/duple/configuration.rb', line 68

def heroku?(env)
  env['type'] == Duple::Configuration::HEROKU
end

#heroku_name(env) ⇒ Object



76
77
78
# File 'lib/duple/configuration.rb', line 76

def heroku_name(env)
  env['appname']
end

#heroku_source?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/duple/configuration.rb', line 60

def heroku_source?
  heroku?(source_environment)
end

#heroku_target?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/duple/configuration.rb', line 40

def heroku_target?
  heroku?(target_environment)
end

#included_tablesObject

Returns an array of tables to include, based on the group config and the –tables option. An empty array indicates that ALL tables should be included. If the group has the include_all flag, an empty array will be returned.



104
105
106
107
108
109
110
111
112
# File 'lib/duple/configuration.rb', line 104

def included_tables
  tables = table_names
  if group_name
    g = group(group_name)
    return [] if g['include_all']
    tables += (g['include_tables'] || [])
  end
  tables.uniq.sort
end

#local?(env) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/duple/configuration.rb', line 72

def local?(env)
  env['type'] == Duple::Configuration::LOCAL
end

#local_source?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/duple/configuration.rb', line 64

def local_source?
  local?(source_environment)
end

#local_target?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/duple/configuration.rb', line 44

def local_target?
  local?(target_environment)
end

#other_optionsObject



203
204
205
# File 'lib/duple/configuration.rb', line 203

def other_options
  raw_config.reject { |k,v| %w{environments groups pre_refresh post_refresh}.include?(k) }
end

#post_refresh_task(task_name) ⇒ Object

Raises:

  • (ArgumentError)


197
198
199
200
201
# File 'lib/duple/configuration.rb', line 197

def post_refresh_task(task_name)
  task = post_refresh_tasks[task_name]
  raise ArgumentError.new("Invalid post_refresh task: #{task_name}") if task.nil?
  task
end

#post_refresh_tasksObject



193
194
195
# File 'lib/duple/configuration.rb', line 193

def post_refresh_tasks
  raw_config['post_refresh'] || {}
end

#pre_refresh_task(task_name) ⇒ Object

Raises:

  • (ArgumentError)


187
188
189
190
191
# File 'lib/duple/configuration.rb', line 187

def pre_refresh_task(task_name)
  task = pre_refresh_tasks[task_name]
  raise ArgumentError.new("Invalid pre_refresh task: #{task_name}") if task.nil?
  task
end

#pre_refresh_tasksObject



183
184
185
# File 'lib/duple/configuration.rb', line 183

def pre_refresh_tasks
  raw_config['pre_refresh'] || {}
end

#source_environmentObject



56
57
58
# File 'lib/duple/configuration.rb', line 56

def source_environment
  environment(source_name)
end

#source_nameObject



52
53
54
# File 'lib/duple/configuration.rb', line 52

def source_name
  options[:source] || default_source_name
end

#table_namesObject



92
93
94
# File 'lib/duple/configuration.rb', line 92

def table_names
  options[:tables] || []
end

#target_environmentObject



32
33
34
35
36
37
38
# File 'lib/duple/configuration.rb', line 32

def target_environment
  invalid_target_names = env_names_by_flag('allow_target', false, true)
  if invalid_target_names.include?(target_name)
    raise ArgumentError.new("Invalid target: #{target_name} is not allowed to be a target.")
  end
  environment(target_name)
end

#target_nameObject



28
29
30
# File 'lib/duple/configuration.rb', line 28

def target_name
  options[:target] || default_target_name
end