Class: Dynamo::Record::TaskHelpers::MigrationRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamo/record/task_helpers/migration_runner.rb

Class Method Summary collapse

Class Method Details

.migration(f, filename_regexp, constants) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/dynamo/record/task_helpers/migration_runner.rb', line 31

def self.migration(f, filename_regexp, constants)
  raise "Non-numeric prefix: #{f}" if File.basename(f).scan(filename_regexp).first.nil?
  require f

  # finds the constant that was added on the require statement above
  migration_sym = (DynamoMigrate.constants - constants).first
  migration = DynamoMigrate.const_get(migration_sym)
  constants.push migration_sym
  migration
end

.run(path = 'db/dynamo_migrate') ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dynamo/record/task_helpers/migration_runner.rb', line 5

def self.run(path = 'db/dynamo_migrate')
  constants = []
  filename_regexp = /\A([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/

  # Sorts the files located in `db/dynamo_migrate` to ensure order is preserved
  Dir[Rails.root.join("#{path}/*.rb")].sort.each do |f|
    migration = migration(f, filename_regexp, constants)

    # starts the migration
    yield "Migrating: #{migration}"

    begin
      status = table_config_check(migration)
      yield status if status

      status = up(migration)
      yield status if status

      status = update(migration)
      yield status if status
    rescue StandardError => e
      yield "Migration failed: #{e}"
    end
  end
end

.status_message(status) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/dynamo/record/task_helpers/migration_runner.rb', line 42

def self.status_message(status)
  case status
  when :exists
    'Table already exists'
  when :migrated
    'Migration successful'
  else
    raise 'Migration failed'
  end
end

.table_config_check(migration) ⇒ Object



58
59
60
61
# File 'lib/dynamo/record/task_helpers/migration_runner.rb', line 58

def self.table_config_check(migration)
  return unless migration.respond_to? :table_config
  status_message migration.table_config_check
end

.up(migration) ⇒ Object



53
54
55
56
# File 'lib/dynamo/record/task_helpers/migration_runner.rb', line 53

def self.up(migration)
  return unless migration.respond_to? :up
  status_message migration.up
end

.update(migration) ⇒ Object



63
64
65
66
67
68
# File 'lib/dynamo/record/task_helpers/migration_runner.rb', line 63

def self.update(migration)
  return unless migration.respond_to? :update
  status = migration.update
  return 'Migration successful' if status == :updated
  status
end