class Dynamo::Record::TaskHelpers::MigrationRunner

Public Class Methods

migration(file, filename_regexp, constants) click to toggle source
# File lib/dynamo/record/task_helpers/migration_runner.rb, line 33
def self.migration(file, filename_regexp, constants)
  raise "Non-numeric prefix: #{file}" if File.basename(file).scan(filename_regexp).first.nil?

  require file

  # 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') { |"Migrating: #{migration}"| ... } click to toggle source
# File lib/dynamo/record/task_helpers/migration_runner.rb, line 7
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 |file|
    migration = migration(file, 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) click to toggle source
# File lib/dynamo/record/task_helpers/migration_runner.rb, line 45
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) click to toggle source
# File lib/dynamo/record/task_helpers/migration_runner.rb, line 62
def self.table_config_check(migration)
  return unless migration.respond_to? :table_config

  status_message migration.table_config_check
end
up(migration) click to toggle source
# File lib/dynamo/record/task_helpers/migration_runner.rb, line 56
def self.up(migration)
  return unless migration.respond_to? :up

  status_message migration.up
end
update(migration) click to toggle source
# File lib/dynamo/record/task_helpers/migration_runner.rb, line 68
def self.update(migration)
  return unless migration.respond_to? :update

  status = migration.update
  return 'Migration successful' if status == :updated

  status
end