class ROM::SQL::Migration::SchemaDiff

@api private

Public Instance Methods

attributes_equal?(a, b) click to toggle source
# File lib/rom/sql/migration/schema_diff.rb, line 244
def attributes_equal?(a, b)
  a.qualified == b.qualified
end
call(current, target) click to toggle source
# File lib/rom/sql/migration/schema_diff.rb, line 180
def call(current, target)
  if current.empty?
    TableCreated.new(
      target_schema: target,
      attributes: map_attributes(target.to_h, AttributeAdded),
      indexes: target.indexes.map { |idx| IndexAdded.new(idx) },
      foreign_keys: target.foreign_keys.map { |fk| ForeignKeyAdded.new(fk) }
    )
  else
    attribute_changes = compare_attributes(current.to_h, target.to_h)
    index_changes = compare_indexes(current, target)
    fk_changes = compare_foreign_key_constraints(current, target)

    if attribute_changes.empty? && index_changes.empty? && fk_changes.empty?
      Empty.new(current_schema: current, target_schema: target)
    else
      TableAltered.new(
        current_schema: current,
        target_schema: target,
        attribute_changes: attribute_changes,
        index_changes: index_changes,
        foreign_key_changes: fk_changes
      )
    end
  end
end
compare_attributes(current, target) click to toggle source
# File lib/rom/sql/migration/schema_diff.rb, line 207
def compare_attributes(current, target)
  changed_attributes = target.select { |name, attr|
    current.key?(name) && !attributes_equal?(current[name], attr)
  }.map { |name, target_attr|
    [name, [target_attr, current[name]]]
  }.to_h
  added_attributes = target.select { |name, _| !current.key?(name) }
  removed_attributes = current.select { |name, _| !target.key?(name) }

  map_attributes(removed_attributes, AttributeRemoved) +
    map_attributes(added_attributes, AttributeAdded) +
    map_attributes(changed_attributes, AttributeChanged)
end
compare_foreign_key_constraints(current, target) click to toggle source
# File lib/rom/sql/migration/schema_diff.rb, line 233
def compare_foreign_key_constraints(current, target)
  target_fks = target.foreign_keys
  current_fks = current.foreign_keys

  added_fks = target_fks - current_fks
  removed_fks = current_fks - target_fks

  removed_fks.map { |fk| ForeignKeyRemoved.new(fk) } +
    added_fks.map { |fk| ForeignKeyAdded.new(fk) }
end
compare_indexes(current, target) click to toggle source
# File lib/rom/sql/migration/schema_diff.rb, line 221
def compare_indexes(current, target)
  added_indexes = target.indexes.reject { |idx|
    current.indexes.any? { |curr_idx| curr_idx.attributes == idx.attributes }
  }
  removed_indexes = current.indexes.select { |idx|
    target.indexes.none? { |tgt_idx| idx.attributes == tgt_idx.attributes }
  }

  removed_indexes.map { |idx| IndexRemoved.new(idx) } +
    added_indexes.map { |idx| IndexAdded.new(idx) }
end
map_attributes(attributes, change_type) click to toggle source
# File lib/rom/sql/migration/schema_diff.rb, line 248
def map_attributes(attributes, change_type)
  attributes.values.map { |args| change_type.new(*args, type_serializer: type_serializer) }
end