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.



21
22
23
24
# File 'lib/duple/configuration.rb', line 21

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#raw_configObject (readonly)

Returns the value of attribute raw_config.



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

def raw_config
  @raw_config
end

Instance Method Details

#capture?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/duple/configuration.rb', line 86

def capture?
  options[:capture]
end

#db_config(appname, options = nil) ⇒ Object



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

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



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

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



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

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



50
51
52
# File 'lib/duple/configuration.rb', line 50

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

#default_target_nameObject



26
27
28
# File 'lib/duple/configuration.rb', line 26

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

#dry_run?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/duple/configuration.rb', line 82

def dry_run?
  options[:dry_run]
end

#environment(env_name) ⇒ Object

Raises:

  • (ArgumentError)


169
170
171
172
173
# File 'lib/duple/configuration.rb', line 169

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

#environmentsObject



165
166
167
# File 'lib/duple/configuration.rb', line 165

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.



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

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)


98
99
100
# File 'lib/duple/configuration.rb', line 98

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

#group(group_name) ⇒ Object

Raises:

  • (ArgumentError)


179
180
181
182
183
# File 'lib/duple/configuration.rb', line 179

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

#group_nameObject



90
91
92
# File 'lib/duple/configuration.rb', line 90

def group_name
  options[:group]
end

#groupsObject



175
176
177
# File 'lib/duple/configuration.rb', line 175

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

#heroku?(env) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/duple/configuration.rb', line 70

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

#heroku_name(env) ⇒ Object



78
79
80
# File 'lib/duple/configuration.rb', line 78

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

#heroku_source?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/duple/configuration.rb', line 62

def heroku_source?
  heroku?(source_environment)
end

#heroku_target?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/duple/configuration.rb', line 42

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.



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

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)


74
75
76
# File 'lib/duple/configuration.rb', line 74

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

#local_source?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/duple/configuration.rb', line 66

def local_source?
  local?(source_environment)
end

#local_target?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/duple/configuration.rb', line 46

def local_target?
  local?(target_environment)
end

#other_optionsObject



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

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

#post_refresh_tasksObject



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

def post_refresh_tasks
  @post_refresh_tasks ||= build_tasks(raw_config['post_refresh'])
end

#pre_refresh_tasksObject



185
186
187
# File 'lib/duple/configuration.rb', line 185

def pre_refresh_tasks
  @pre_refresh_tasks ||= build_tasks(raw_config['pre_refresh'])
end

#source_environmentObject



58
59
60
# File 'lib/duple/configuration.rb', line 58

def source_environment
  environment(source_name)
end

#source_nameObject



54
55
56
# File 'lib/duple/configuration.rb', line 54

def source_name
  options[:source] || default_source_name
end

#table_namesObject



94
95
96
# File 'lib/duple/configuration.rb', line 94

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

#target_environmentObject



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

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



30
31
32
# File 'lib/duple/configuration.rb', line 30

def target_name
  options[:target] || default_target_name
end