Class: SsmConfig::MigrationHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/ssm_config/migration_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ MigrationHelper

Returns a new instance of MigrationHelper.



3
4
5
6
# File 'lib/ssm_config/migration_helper.rb', line 3

def initialize(file_name)
  @file_name = file_name
  @model_name = SsmConfig::SsmStorage::Db::ACTIVE_RECORD_MODEL
end

Instance Method Details

#downObject



21
22
23
# File 'lib/ssm_config/migration_helper.rb', line 21

def down
  @model_name.constantize.where(:file => @file_name).destroy_all
end

#upObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ssm_config/migration_helper.rb', line 8

def up
  added = []
  keys_hash = accessor_key_hash(hash) # starting layer is always hash
  last = nil
  keys_hash.each do |accessor_key, value|
    last = accessor_key
    added.push(@model_name.constantize.create!(:file => @file_name, :accessor_keys => accessor_key, :value => value.to_s, :datatype => determine_class(value)))
  end
rescue ActiveRecord::RecordInvalid => e
  Rails.logger.error("#{e.message} was raised because of faulty data with accessor_key #{last}")
  added.each(&:delete)
end