lib/ridgepole/diff.rb in ridgepole-0.6.5.beta vs lib/ridgepole/diff.rb in ridgepole-0.6.5.beta2
- old
+ new
@@ -112,11 +112,11 @@
to.each do |column_name, to_attrs|
if (from_attrs = from.delete(column_name))
normalize_column_options!(from_attrs)
normalize_column_options!(to_attrs)
- if from_attrs != to_attrs
+ unless compare_column_attrs(from_attrs, to_attrs)
definition_delta[:change] ||= {}
to_attrs = fix_change_column_options(table_name, from_attrs, to_attrs)
definition_delta[:change][column_name] = to_attrs
end
else
@@ -320,11 +320,10 @@
unless foreign_keys_delta.empty?
table_delta[:foreign_keys] = foreign_keys_delta
end
end
-
# XXX: MySQL only?
# https://github.com/rails/rails/blob/v4.2.1/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L760
# https://github.com/rails/rails/blob/v4.2.1/activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb#L102
def fix_change_column_options(table_name, from_attrs, to_attrs)
# default: 0, null: false -> default: nil, null: false | default: nil
@@ -344,7 +343,19 @@
if to_attrs[:options][:default] == nil and to_attrs[:options][:null] == false
Ridgepole::Logger.instance.warn("[WARNING] Table `#{table_name}`: `default: nil` is ignored when `null: false`. Please apply twice")
end
to_attrs
+ end
+
+ def compare_column_attrs(attrs1, attrs2)
+ attrs1 = attrs1.merge(:options => attrs1.fetch(:options, {}).dup)
+ attrs2 = attrs2.merge(:options => attrs2.fetch(:options, {}).dup)
+
+ if attrs1[:options][:default].kind_of?(Proc) and attrs2[:options][:default].kind_of?(Proc)
+ attrs1[:options][:default] = attrs1[:options][:default].call
+ attrs2[:options][:default] = attrs2[:options][:default].call
+ end
+
+ attrs1 == attrs2
end
end