Class: ROM::SQL::Migration::SchemaDiff Private

Inherits:
Object
  • Object
show all
Extended by:
Initializer
Defined in:
lib/rom/sql/migration/schema_diff.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: AttributeAdded, AttributeChanged, AttributeDiff, AttributeRemoved, Empty, ForeignKeyAdded, ForeignKeyDiff, ForeignKeyRemoved, IndexAdded, IndexDiff, IndexRemoved, TableAltered, TableCreated, TableDiff

Instance Method Summary collapse

Instance Method Details

#attributes_equal?(a, b) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


249
250
251
# File 'lib/rom/sql/migration/schema_diff.rb', line 249

def attributes_equal?(a, b)
  a.qualified == b.qualified
end

#call(current, target) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/MethodLength



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/rom/sql/migration/schema_diff.rb', line 182

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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Style/MultilineBlockChain



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rom/sql/migration/schema_diff.rb', line 211

def compare_attributes(current, target)
  changed_attributes = target.select { |name, attr|
    current.key?(name) && !attributes_equal?(current[name], attr)
  }.to_h { |name, target_attr| [name, [target_attr, current[name]]] }
  added_attributes = target.reject { |name, _| current.key?(name) }
  removed_attributes = current.reject { |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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:enable Metrics/AbcSize



238
239
240
241
242
243
244
245
246
247
# File 'lib/rom/sql/migration/schema_diff.rb', line 238

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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/AbcSize



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/rom/sql/migration/schema_diff.rb', line 225

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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



253
254
255
# File 'lib/rom/sql/migration/schema_diff.rb', line 253

def map_attributes(attributes, change_type)
  attributes.values.map { |args| change_type.new(*args, type_serializer: type_serializer) }
end