lib/ridgepole/diff.rb in ridgepole-1.2.1 vs lib/ridgepole/diff.rb in ridgepole-2.0.0.beta

- old
+ new

@@ -100,10 +100,12 @@ scan_options_change(table_name, from[:options], to[:options], table_delta) scan_definition_change(from[:definition], to[:definition], from[:indices], table_name, from[:options], table_delta) scan_indices_change(from[:indices], to[:indices], to[:definition], table_delta, from[:options], to[:options]) scan_foreign_keys_change(from[:foreign_keys], to[:foreign_keys], table_delta, @options) scan_check_constraints_change(from[:check_constraints], to[:check_constraints], table_delta) + scan_exclusion_constraints_change(from[:exclusion_constraints], to[:exclusion_constraints], table_delta) + scan_unique_constraints_change(from[:unique_constraints], to[:unique_constraints], table_delta) unless table_delta.empty? delta[:change] ||= {} delta[:change][table_name] = table_delta end @@ -501,9 +503,73 @@ check_constraints_delta[:delete][name] = from_attrs end end table_delta[:check_constraints] = check_constraints_delta unless check_constraints_delta.empty? + end + + def scan_exclusion_constraints_change(from, to, table_delta) + from = (from || {}).dup + to = (to || {}).dup + exclusion_constraints_delta = {} + + to.each do |name, to_attrs| + from_attrs = from.delete(name) + + if from_attrs + if from_attrs != to_attrs + exclusion_constraints_delta[:add] ||= {} + exclusion_constraints_delta[:add][name] = to_attrs + + exclusion_constraints_delta[:delete] ||= {} + exclusion_constraints_delta[:delete][name] = from_attrs + end + else + exclusion_constraints_delta[:add] ||= {} + exclusion_constraints_delta[:add][name] = to_attrs + end + end + + unless @options[:merge] + from.each do |name, from_attrs| + exclusion_constraints_delta[:delete] ||= {} + exclusion_constraints_delta[:delete][name] = from_attrs + end + end + + table_delta[:exclusion_constraints] = exclusion_constraints_delta unless exclusion_constraints_delta.empty? + end + + def scan_unique_constraints_change(from, to, table_delta) + from = (from || {}).dup + to = (to || {}).dup + unique_constraints_delta = {} + + to.each do |name, to_attrs| + from_attrs = from.delete(name) + + if from_attrs + if from_attrs != to_attrs + unique_constraints_delta[:add] ||= {} + unique_constraints_delta[:add][name] = to_attrs + + unique_constraints_delta[:delete] ||= {} + unique_constraints_delta[:delete][name] = from_attrs + end + else + unique_constraints_delta[:add] ||= {} + unique_constraints_delta[:add][name] = to_attrs + end + end + + unless @options[:merge] + from.each do |name, from_attrs| + unique_constraints_delta[:delete] ||= {} + unique_constraints_delta[:delete][name] = from_attrs + end + end + + table_delta[:unique_constraints] = unique_constraints_delta unless unique_constraints_delta.empty? 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