class ROM::SQL::Migration::Migrator

@api private

Constants

DEFAULT_INFERRER
DEFAULT_PATH
VERSION_FORMAT

Public Instance Methods

auto_migrate!(gateway, schemas, options = EMPTY_HASH) click to toggle source

@api private

# File lib/rom/sql/migration/migrator.rb, line 70
def auto_migrate!(gateway, schemas, options = EMPTY_HASH)
  diff_finder = SchemaDiff.new(gateway.database_type)

  changes = schemas.map { |target|
    empty = SQL::Schema.define(target.name)
    current = target.with(**inferrer.(empty, gateway))

    diff_finder.(current, target)
  }.reject(&:empty?)

  runner = migration_runner(options)
  runner.(changes)
end
create_file(name, version = generate_version, **options) click to toggle source

@api private

# File lib/rom/sql/migration/migrator.rb, line 45
def create_file(name, version = generate_version, **options)
  sequence = options[:sequence] ? '%03d' % options[:sequence] : nil
  filename = "#{ version }#{ sequence }_#{ name }.rb"
  content = options[:content] || migration_file_content
  path = options[:path] || self.path
  dirname = Pathname(path)
  fullpath = dirname.join(filename)

  FileUtils.mkdir_p(dirname)
  File.write(fullpath, content)

  fullpath
end
generate_version() click to toggle source

@api private

# File lib/rom/sql/migration/migrator.rb, line 60
def generate_version
  Time.now.utc.strftime(VERSION_FORMAT)
end
migration(&block) click to toggle source

@api private

# File lib/rom/sql/migration/migrator.rb, line 40
def migration(&block)
  Sequel.migration(&block)
end
migration_file_content() click to toggle source

@api private

# File lib/rom/sql/migration/migrator.rb, line 65
def migration_file_content
  File.read(Pathname(__FILE__).dirname.join('template.rb').realpath)
end
migration_runner(options) click to toggle source

@api private

# File lib/rom/sql/migration/migrator.rb, line 85
def migration_runner(options)
  if options[:inline]
    Runner.new(InlineRunner.new(connection))
  else
    counter = 0
    writer = Writer.new do |name, content|
      create_file(name, **options, content: content, sequence: counter)
      counter += 1
    end

    Runner.new(writer)
  end
end
pending?() click to toggle source

@api private

# File lib/rom/sql/migration/migrator.rb, line 35
def pending?
  !Sequel::Migrator.is_current?(connection, path.to_s)
end
run(options = {}) click to toggle source

@api private

# File lib/rom/sql/migration/migrator.rb, line 30
def run(options = {})
  Sequel::Migrator.run(connection, path.to_s, options)
end