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)


244
245
246
# File 'lib/rom/sql/migration/schema_diff.rb', line 244

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.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# 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) ⇒ 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.



207
208
209
210
211
212
213
214
215
216
217
218
219
# 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) ⇒ 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.



233
234
235
236
237
238
239
240
241
242
# 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) ⇒ 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.



221
222
223
224
225
226
227
228
229
230
231
# 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) ⇒ 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.



248
249
250
# 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