module SeedDump::Environment
Constants
- ACTIVE_RECORD_INTERNAL_MODELS
Internal: Array of Strings corresponding to Active Record model class names that should be excluded from the dump.
Public Instance Methods
# File lib/seed_dump/environment.rb, line 3 def dump_using_environment(env = {}) Rails.application.eager_load! models = retrieve_models(env) - retrieve_models_exclude(env) current_file_index = 1 limit = retrieve_limit_value(env) append = retrieve_append_value(env) # Eliminate HABTM models that have the same underlying table; otherwise # they'll be dumped twice, once in each direction. Probably should apply # to all models, but it's possible there are edge cases in which this # is not the right behavior. habtm, non_habtm = models.partition { |m| m.name =~ /^HABTM_/ } models = non_habtm + habtm.uniq(&:table_name) resolved_dependencies = DependencyUnwrangler.new(models).evaluation_order - retrieve_models_exclude(env) models = resolved_dependencies unless resolved_dependencies.empty? models.each do |model| model = model.limit(limit) if limit.present? options = { append: append, batch_size: retrieve_batch_size_value(env), exclude: retrieve_dump_all_value(env) ? [] : retrieve_exclude_value(env), insert_all: retrieve_insert_all_value(env), file_split_limit: retreive_file_split_limit_value(env), file: retrieve_file_value(env), import: retrieve_import_value(env), current_file_index: current_file_index, import_options: retrieve_import_options(env) } SeedDump.dump(model, options) append = true # Always append for every model after the first # (append for the first model is determined by # the APPEND environment variable). current_file_index = options[:current_file_index] end end
Private Instance Methods
Internal: Parses a Boolean from the given value.
# File lib/seed_dump/environment.rb, line 178 def parse_boolean_value(value) value.to_s.casecmp('true').zero? end
Internal: Returns a Boolean indicating whether the value for the “FILE_SPLIT_COUNT” key in the given Hash is equal to the String “true” (ignoring case), false if no value exists.
# File lib/seed_dump/environment.rb, line 109 def retreive_file_split_limit_value(env) retrieve_integer_value('FILE_SPLIT_LIMIT', env) end
Internal: Returns a Boolean indicating whether the value for the “APPEND” key in the given Hash is equal to the String “true” (ignoring case), false if no value exists.
# File lib/seed_dump/environment.rb, line 102 def retrieve_append_value(env) parse_boolean_value(env['APPEND']) end
Internal: Retrieves an Integer from the value for the “BATCH_SIZE” key in the given Hash, and nil if no such key exists.
# File lib/seed_dump/environment.rb, line 167 def retrieve_batch_size_value(env) retrieve_integer_value('BATCH_SIZE', env) end
Internal: Returns a Boolean indicating whether the value for the “DUMP_ALL” key in the given Hash is equal to the String “true” (ignoring case), false if no value exists.
# File lib/seed_dump/environment.rb, line 134 def retrieve_dump_all_value(env) parse_boolean_value(env['DUMP_ALL']) end
Internal: Retrieves an Array of Symbols from the value for the “EXCLUDE” key from the given Hash, and nil if no such key exists.
# File lib/seed_dump/environment.rb, line 155 def retrieve_exclude_value(env) env['EXCLUDE'] ? env['EXCLUDE'].split(',').map { |e| e.strip.to_sym } : nil end
Internal: Retrieves the value for the “FILE” key from the given Hash, and ‘db/seeds.rb’ if no such key exists.
# File lib/seed_dump/environment.rb, line 161 def retrieve_file_value(env) env['FILE'] || 'db/seeds.rb' end
# File lib/seed_dump/environment.rb, line 120 def retrieve_import_options(env) env['IMPORT_OPTIONS'] end
Internal: Returns a Boolean indicating whether the value for the “IMPORT” key in the given Hash is equal to the String “true” (ignoring case), false if no value exists.
# File lib/seed_dump/environment.rb, line 116 def retrieve_import_value(env) parse_boolean_value(env['IMPORT']) end
Internal: Returns a Boolean indicating whether the value for the “INSERT_ALL” key in the given Hash is equal to the String “true” (ignoring case), false if no value exists.
# File lib/seed_dump/environment.rb, line 127 def retrieve_insert_all_value(env) parse_boolean_value(env['INSERT_ALL']) end
Internal: Retrieves an Integer from the value for the given key in the given Hash, and nil if no such key exists.
# File lib/seed_dump/environment.rb, line 173 def retrieve_integer_value(key, hash) hash[key]&.to_i end
Internal: Retrieves an Integer from the value for the “LIMIT” key in the given Hash, and nil if no such key exists.
# File lib/seed_dump/environment.rb, line 149 def retrieve_limit_value(env) retrieve_integer_value('LIMIT', env) end
Internal: Retrieves an Array of Active Record model class constants to be dumped.
If a “MODEL” or “MODELS” environment variable is specified, there will be an attempt to parse the environment variable String by splitting it on commmas and then converting it to constant.
Model classes that do not have corresponding database tables or database records will be filtered out, as will model classes internal to Active Record.
env - Hash of environment variables from which to parse Active Record
model classes. The Hash is not optional but the "MODEL" and "MODELS" keys are optional.
Returns the Array of Active Record model classes to be dumped.
# File lib/seed_dump/environment.rb, line 68 def retrieve_models(env) # Parse either the "MODEL" environment variable or the "MODELS" # environment variable, with "MODEL" taking precedence. models_env = env['MODEL'] || env['MODELS'] # If there was a use models environment variable, split it and # convert the given model string (e.g. "User") to an actual # model constant (e.g. User). # # If a models environment variable was not given, use descendants of # ActiveRecord::Base as the target set of models. This should be all # model classes in the project. models = if models_env models_env.split(',') .collect { |x| x.strip.underscore.singularize.camelize.constantize } else ActiveRecord::Base.descendants end # Filter the set of models to exclude: # - The ActiveRecord::SchemaMigration model which is internal to Rails # and should not be part of the dumped data. # - Models that don't have a corresponding table in the database. # - Models whose corresponding database tables are empty. filtered_models = models.select do |model| !ACTIVE_RECORD_INTERNAL_MODELS.include?(model.to_s) && \ model.table_exists? && \ model.exists? end end
Internal: Retrieves an Array of Class constants parsed from the value for the “MODELS_EXCLUDE” key in the given Hash, and an empty Array if such key exists.
# File lib/seed_dump/environment.rb, line 141 def retrieve_models_exclude(env) env['MODELS_EXCLUDE'].to_s .split(',') .collect { |x| x.strip.underscore.singularize.camelize.constantize } end