Class: Duple::Configuration
- Inherits:
-
Object
- Object
- Duple::Configuration
- 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
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#raw_config ⇒ Object
readonly
Returns the value of attribute raw_config.
Instance Method Summary collapse
- #capture? ⇒ Boolean
- #db_config(appname, options = nil) ⇒ Object
- #db_config_for_app(appname) ⇒ Object
- #db_config_for_dry_run(appname) ⇒ Object
- #default_source_name ⇒ Object
- #default_target_name ⇒ Object
- #dry_run? ⇒ Boolean
- #environment(env_name) ⇒ Object
- #environments ⇒ Object
-
#excluded_tables ⇒ Object
Returns an array of tables to exclude, based on the group config and the –tables option.
- #filtered_tables? ⇒ Boolean
- #group(group_name) ⇒ Object
- #group_name ⇒ Object
- #groups ⇒ Object
- #heroku?(env) ⇒ Boolean
- #heroku_name(env) ⇒ Object
- #heroku_source? ⇒ Boolean
- #heroku_target? ⇒ Boolean
-
#included_tables ⇒ Object
Returns an array of tables to include, based on the group config and the –tables option.
-
#initialize(config_hash, options) ⇒ Configuration
constructor
A new instance of Configuration.
- #local?(env) ⇒ Boolean
- #local_source? ⇒ Boolean
- #local_target? ⇒ Boolean
- #other_options ⇒ Object
- #post_refresh_task(task_name) ⇒ Object
- #post_refresh_tasks ⇒ Object
- #pre_refresh_task(task_name) ⇒ Object
- #pre_refresh_tasks ⇒ Object
- #source_environment ⇒ Object
- #source_name ⇒ Object
- #table_names ⇒ Object
- #target_environment ⇒ Object
- #target_name ⇒ Object
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, ) @raw_config = config_hash @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/duple/configuration.rb', line 17 def @options end |
#raw_config ⇒ Object (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
84 85 86 |
# File 'lib/duple/configuration.rb', line 84 def capture? [: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, = nil) ||= {} = {dry_run: false}.merge() if [: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_name ⇒ Object
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_name ⇒ Object
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
80 81 82 |
# File 'lib/duple/configuration.rb', line 80 def dry_run? [:dry_run] end |
#environment(env_name) ⇒ Object
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 |
#environments ⇒ Object
163 164 165 |
# File 'lib/duple/configuration.rb', line 163 def environments raw_config['environments'] || {} end |
#excluded_tables ⇒ Object
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
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
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_name ⇒ Object
88 89 90 |
# File 'lib/duple/configuration.rb', line 88 def group_name [:group] end |
#groups ⇒ Object
173 174 175 |
# File 'lib/duple/configuration.rb', line 173 def groups raw_config['groups'] || {} end |
#heroku?(env) ⇒ 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
60 61 62 |
# File 'lib/duple/configuration.rb', line 60 def heroku_source? heroku?(source_environment) end |
#heroku_target? ⇒ Boolean
40 41 42 |
# File 'lib/duple/configuration.rb', line 40 def heroku_target? heroku?(target_environment) end |
#included_tables ⇒ Object
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
72 73 74 |
# File 'lib/duple/configuration.rb', line 72 def local?(env) env['type'] == Duple::Configuration::LOCAL end |
#local_source? ⇒ Boolean
64 65 66 |
# File 'lib/duple/configuration.rb', line 64 def local_source? local?(source_environment) end |
#local_target? ⇒ Boolean
44 45 46 |
# File 'lib/duple/configuration.rb', line 44 def local_target? local?(target_environment) end |
#other_options ⇒ Object
203 204 205 |
# File 'lib/duple/configuration.rb', line 203 def raw_config.reject { |k,v| %w{environments groups pre_refresh post_refresh}.include?(k) } end |
#post_refresh_task(task_name) ⇒ Object
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_tasks ⇒ Object
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
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_tasks ⇒ Object
183 184 185 |
# File 'lib/duple/configuration.rb', line 183 def pre_refresh_tasks raw_config['pre_refresh'] || {} end |
#source_environment ⇒ Object
56 57 58 |
# File 'lib/duple/configuration.rb', line 56 def source_environment environment(source_name) end |
#source_name ⇒ Object
52 53 54 |
# File 'lib/duple/configuration.rb', line 52 def source_name [:source] || default_source_name end |
#table_names ⇒ Object
92 93 94 |
# File 'lib/duple/configuration.rb', line 92 def table_names [:tables] || [] end |
#target_environment ⇒ Object
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_name ⇒ Object
28 29 30 |
# File 'lib/duple/configuration.rb', line 28 def target_name [:target] || default_target_name end |