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_tasks ⇒ 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.
21 22 23 24 |
# File 'lib/duple/configuration.rb', line 21 def initialize(config_hash, ) @raw_config = config_hash @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
19 20 21 |
# File 'lib/duple/configuration.rb', line 19 def @options end |
#raw_config ⇒ Object (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
86 87 88 |
# File 'lib/duple/configuration.rb', line 86 def capture? [: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, = 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
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_name ⇒ Object
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_name ⇒ Object
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
82 83 84 |
# File 'lib/duple/configuration.rb', line 82 def dry_run? [:dry_run] end |
#environment(env_name) ⇒ Object
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 |
#environments ⇒ Object
165 166 167 |
# File 'lib/duple/configuration.rb', line 165 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.
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
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
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_name ⇒ Object
90 91 92 |
# File 'lib/duple/configuration.rb', line 90 def group_name [:group] end |
#groups ⇒ Object
175 176 177 |
# File 'lib/duple/configuration.rb', line 175 def groups raw_config['groups'] || {} end |
#heroku?(env) ⇒ 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
62 63 64 |
# File 'lib/duple/configuration.rb', line 62 def heroku_source? heroku?(source_environment) end |
#heroku_target? ⇒ Boolean
42 43 44 |
# File 'lib/duple/configuration.rb', line 42 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.
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
74 75 76 |
# File 'lib/duple/configuration.rb', line 74 def local?(env) env['type'] == Duple::Configuration::LOCAL end |
#local_source? ⇒ Boolean
66 67 68 |
# File 'lib/duple/configuration.rb', line 66 def local_source? local?(source_environment) end |
#local_target? ⇒ Boolean
46 47 48 |
# File 'lib/duple/configuration.rb', line 46 def local_target? local?(target_environment) end |
#other_options ⇒ Object
193 194 195 |
# File 'lib/duple/configuration.rb', line 193 def raw_config.reject { |k,v| %w{environments groups pre_refresh post_refresh}.include?(k) } end |
#post_refresh_tasks ⇒ Object
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_tasks ⇒ Object
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_environment ⇒ Object
58 59 60 |
# File 'lib/duple/configuration.rb', line 58 def source_environment environment(source_name) end |
#source_name ⇒ Object
54 55 56 |
# File 'lib/duple/configuration.rb', line 54 def source_name [:source] || default_source_name end |
#table_names ⇒ Object
94 95 96 |
# File 'lib/duple/configuration.rb', line 94 def table_names [:tables] || [] end |
#target_environment ⇒ Object
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_name ⇒ Object
30 31 32 |
# File 'lib/duple/configuration.rb', line 30 def target_name [:target] || default_target_name end |