class ROM::SQL::Migration::Runner

@api private

Attributes

writer[R]

Public Class Methods

new(writer) click to toggle source
# File lib/rom/sql/migration/runner.rb, line 10
def initialize(writer)
  @writer = writer
end

Public Instance Methods

alter_foreign_keys(diff, foreign_key_changes) click to toggle source
# File lib/rom/sql/migration/runner.rb, line 98
def alter_foreign_keys(diff, foreign_key_changes)
  return if foreign_key_changes.empty?

  writer.migration do |connection|
    connection.alter_table(diff.table_name) do
      foreign_key_changes.map do |fk|
        case fk
        when SchemaDiff::ForeignKeyAdded
          add_foreign_key fk.child_keys, fk.parent
        when SchemaDiff::ForeignKeyRemoved
          drop_foreign_key fk.child_keys
        end
      end
    end
  end
end
alter_table(diff) click to toggle source
# File lib/rom/sql/migration/runner.rb, line 57
def alter_table(diff)
  return if diff.meta?

  writer.migration do |connection|
    connection.alter_table(diff.table_name) do
      diff.attribute_changes.each do |attribute|
        case attribute
        when SchemaDiff::AttributeAdded
          add_column attribute.name, attribute.type, null: attribute.null?
        when SchemaDiff::AttributeRemoved
          drop_column attribute.name
        when SchemaDiff::AttributeChanged
          if attribute.type_changed?
            from, to = attribute.current.unwrap, attribute.target.unwrap
            raise UnsupportedConversion.new(
              "Don't know how to convert #{from.inspect} to #{to.inspect}"
            )
          end

          if attribute.nullability_changed?
            if attribute.null?
              set_column_allow_null attribute.name
            else
              set_column_not_null attribute.name
            end
          end
        end
      end

      diff.index_changes.each do |index|
        case index
        when SchemaDiff::IndexAdded
          add_index index.attributes, index.options
        when SchemaDiff::IndexRemoved
          drop_index index.attributes, index.options
        end
      end
    end
  end
end
apply_constraints(diff) click to toggle source
# File lib/rom/sql/migration/runner.rb, line 30
def apply_constraints(diff)
  case diff
  when SchemaDiff::TableCreated
    alter_foreign_keys(diff, diff.foreign_keys)
  when SchemaDiff::TableAltered
    alter_foreign_keys(diff, diff.foreign_key_changes)
  end
end
apply_schema(diff) click to toggle source
# File lib/rom/sql/migration/runner.rb, line 21
def apply_schema(diff)
  case diff
  when SchemaDiff::TableCreated
    create_table(diff)
  when SchemaDiff::TableAltered
    alter_table(diff)
  end
end
call(changes) click to toggle source
# File lib/rom/sql/migration/runner.rb, line 14
def call(changes)
  changes.each { |diff| apply_schema(diff) }
  changes.each { |diff| apply_constraints(diff) }

  self
end
create_table(diff) click to toggle source
# File lib/rom/sql/migration/runner.rb, line 39
def create_table(diff)
  writer.migration do |connection|
    connection.create_table(diff.table_name) do
      diff.attributes.each do |attribute|
        if attribute.primary_key?
          primary_key attribute.name
        else
          column attribute.name, attribute.type, null: attribute.null?
        end
      end

      diff.indexes.each do |index|
        index index.attributes, index.options
      end
    end
  end
end