lib/ridgepole/diff.rb in ridgepole-0.5.3.beta4 vs lib/ridgepole/diff.rb in ridgepole-0.6.0.beta

- old
+ new

@@ -76,15 +76,12 @@ table_delta = {} scan_options_change(table_name, from[:options], to[:options], table_delta) scan_definition_change(from[:definition], to[:definition], from[:indices], 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) - if @options[:enable_foreigner] - Ridgepole::ForeignKey.scan_foreign_keys_change(from[:foreign_keys], to[:foreign_keys], table_delta, @options) - end - unless table_delta.empty? delta[:change] ||= {} delta[:change][table_name] = table_delta end end @@ -241,29 +238,27 @@ def normalize_column_options!(attrs) opts = attrs[:options] opts[:null] = true unless opts.has_key?(:null) - # XXX: MySQL only? - case attrs[:type] - when :string - opts.delete(:limit) if opts[:limit] == 255 + Ridgepole::DEFAULTS_LIMITS.keys.each do |column_type| + default_limit = @options[:"default_#{column_type}_limit"] + next if default_limit <= 0 + + if attrs[:type] == column_type and opts[:limit] == default_limit + opts.delete(:limit) + end end # XXX: MySQL only? if not opts.has_key?(:default) and opts[:null] opts[:default] = nil end - # XXX: MySQL only? - if @options[:enable_mysql_unsigned] or @options[:enable_mysql_awesome] + if @options[:enable_mysql_awesome] opts[:unsigned] = false unless opts.has_key?(:unsigned) end - - if @options[:normalize_mysql_float] and attrs[:type] == :float - opts.delete(:limit) if opts[:limit] == 24 - end end def normalize_index_options!(opts) # XXX: MySQL only? opts[:using] = :btree unless opts.has_key?(:using) @@ -273,7 +268,43 @@ if table_options[:id] != false actual_columns = actual_columns + [(table_options[:primary_key] || 'id').to_s] end expected_columns.all? {|i| actual_columns.include?(i) } + end + + def scan_foreign_keys_change(from, to, table_delta, options) + from = (from || {}).dup + to = (to || {}).dup + foreign_keys_delta = {} + + to.each do |foreign_key_name, to_attrs| + from_attrs = from.delete(foreign_key_name) + + if from_attrs + if from_attrs != to_attrs + foreign_keys_delta[:add] ||= {} + foreign_keys_delta[:add][foreign_key_name] = to_attrs + + unless options[:merge] + foreign_keys_delta[:delete] ||= {} + foreign_keys_delta[:delete][foreign_key_name] = from_attrs + end + end + else + foreign_keys_delta[:add] ||= {} + foreign_keys_delta[:add][foreign_key_name] = to_attrs + end + end + + unless options[:merge] + from.each do |foreign_key_name, from_attrs| + foreign_keys_delta[:delete] ||= {} + foreign_keys_delta[:delete][foreign_key_name] = from_attrs + end + end + + unless foreign_keys_delta.empty? + table_delta[:foreign_keys] = foreign_keys_delta + end end end