lib/ridgepole/diff.rb in ridgepole-0.7.4 vs lib/ridgepole/diff.rb in ridgepole-0.7.5.beta

- old
+ new

@@ -58,10 +58,11 @@ def scan_table_rename(from, to, delta, _options = {}) to.dup.each do |table_name, to_attrs| next unless target?(table_name) next unless (from_table_name = (to_attrs[:options] || {}).delete(:renamed_from)) + from_table_name = from_table_name.to_s if from_table_name # Already renamed if from[table_name] @logger.warn("[WARNING] The table `#{from_table_name}` has already been renamed to the table `#{table_name}`.") @@ -110,13 +111,11 @@ from.delete(:comment) to.delete(:comment) end [from, to].each do |table_attrs| - if table_attrs.key?(:default) && table_attrs[:default].nil? - table_attrs.delete(:default) - end + table_attrs.delete(:default) if table_attrs.key?(:default) && table_attrs[:default].nil? end if Ridgepole::ConnectionAdapters.mysql? if @options[:mysql_change_table_options] && (from_options != to_options) from.delete(:options) @@ -174,31 +173,28 @@ options.delete(:id) else Ridgepole::DSLParser::TableDefinition::DEFAULT_PRIMARY_KEY_TYPE end - if %i[integer bigint].include?(type) && !options.key?(:default) && !Ridgepole::ConnectionAdapters.postgresql? - options[:auto_increment] = true - end + options[:auto_increment] = true if %i[integer bigint].include?(type) && !options.key?(:default) && !Ridgepole::ConnectionAdapters.postgresql? { type: type, options: options } end def build_attrs_if_changed(to_attrs, from_attrs, table_name, primary_key: false) normalize_column_options!(from_attrs, primary_key) normalize_column_options!(to_attrs, primary_key) - unless compare_column_attrs(from_attrs, to_attrs) - new_to_attrs = fix_change_column_options(table_name, from_attrs, to_attrs) - end + new_to_attrs = fix_change_column_options(table_name, from_attrs, to_attrs) unless compare_column_attrs(from_attrs, to_attrs) new_to_attrs end def build_primary_key_attrs_if_changed(from, to, table_name) from_column_attrs = convert_to_primary_key_attrs(from.slice(*PRIMARY_KEY_OPTIONS)) to_column_attrs = convert_to_primary_key_attrs(to.slice(*PRIMARY_KEY_OPTIONS)) return if from_column_attrs == to_column_attrs + build_attrs_if_changed(to_column_attrs, from_column_attrs, table_name, primary_key: true) end def scan_definition_change(from, to, from_indices, table_name, table_options, table_delta) from = (from || {}).dup @@ -238,13 +234,11 @@ if Ridgepole::ConnectionAdapters.postgresql? added_size = 0 to.reverse_each.with_index do |(column_name, to_attrs), i| if to_attrs[:options].delete(:after) - if added_size != i - @logger.warn("[WARNING] PostgreSQL doesn't support adding a new column except for the last position. #{table_name}.#{column_name} will be added to the last.") - end + @logger.warn("[WARNING] PostgreSQL doesn't support adding a new column except for the last position. #{table_name}.#{column_name} will be added to the last.") if added_size != i added_size += 1 end end end @@ -252,16 +246,15 @@ from.each do |column_name, from_attrs| definition_delta[:delete] ||= {} definition_delta[:delete][column_name] = from_attrs next unless from_indices + modified_indices = [] from_indices.each do |name, attrs| - if attrs[:column_name].is_a?(Array) && attrs[:column_name].delete(column_name) - modified_indices << name - end + modified_indices << name if attrs[:column_name].is_a?(Array) && attrs[:column_name].delete(column_name) end # In PostgreSQL, the index is deleted when the column is deleted if @options[:index_removed_drop_column] from_indices.reject! do |name, _attrs| @@ -279,18 +272,17 @@ end def scan_column_rename(from, to, definition_delta) to.dup.each do |column_name, to_attrs| next unless (from_column_name = (to_attrs[:options] || {}).delete(:renamed_from)) + from_column_name = from_column_name.to_s if from_column_name # Already renamed next if from[column_name] - unless from.key?(from_column_name) - raise "Column `#{from_column_name}` not found" - end + raise "Column `#{from_column_name}` not found" unless from.key?(from_column_name) definition_delta[:rename] ||= {} definition_delta[:rename][column_name] = from_column_name from.delete(from_column_name) @@ -386,13 +378,11 @@ end def columns_all_include?(expected_columns, actual_columns, table_options) return true unless expected_columns.is_a?(Array) - if (table_options[:id] != false) && !table_options[:primary_key].is_a?(Array) - actual_columns += [(table_options[:primary_key] || 'id').to_s] - end + actual_columns += [(table_options[:primary_key] || 'id').to_s] if (table_options[:id] != false) && !table_options[:primary_key].is_a?(Array) expected_columns.all? { |i| actual_columns.include?(i) } end def scan_foreign_keys_change(from, to, table_delta, options) @@ -424,13 +414,11 @@ foreign_keys_delta[:delete] ||= {} foreign_keys_delta[:delete][foreign_key_name_or_tables] = from_attrs end end - unless foreign_keys_delta.empty? - table_delta[:foreign_keys] = foreign_keys_delta - end + table_delta[:foreign_keys] = foreign_keys_delta unless foreign_keys_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 @@ -448,13 +436,11 @@ to_attrs = to_attrs.deep_dup to_attrs[:options].delete(:null) end if Ridgepole::ConnectionAdapters.mysql? && ActiveRecord::VERSION::STRING.start_with?('5.0.') - if to_attrs[:options][:default].nil? && (to_attrs[:options][:null] == false) - Ridgepole::Logger.instance.warn("[WARNING] Table `#{table_name}`: `default: nil` is ignored when `null: false`. Please apply twice") - end + Ridgepole::Logger.instance.warn("[WARNING] Table `#{table_name}`: `default: nil` is ignored when `null: false`. Please apply twice") if to_attrs[:options][:default].nil? && (to_attrs[:options][:null] == false) end to_attrs end @@ -496,18 +482,16 @@ attrs_by_column = {} definition = table_attr[:definition] || {} definition.each do |column_name, column_attrs| - if column_name =~ /\w+_id\z/ - attrs_by_column[column_name] = column_attrs.dup - end + attrs_by_column[column_name] = column_attrs.dup if column_name =~ /\w+_id\z/ end relation_info[table_name] = { options: table_attr[:options] || {}, - columns: attrs_by_column + columns: attrs_by_column, } end def scan_relation_info(relation_info) return unless @options[:check_relation_type] @@ -535,29 +519,30 @@ table_options = parent_table_info.fetch(:options) next if table_options[:id] == false parent_column_info = { type: table_options[:id] || @options[:check_relation_type].to_sym, - unsigned: table_options[:unsigned] + unsigned: table_options[:unsigned], } child_column_info = { type: column_attrs[:type], - unsigned: column_attrs.fetch(:options, {})[:unsigned] + unsigned: column_attrs.fetch(:options, {})[:unsigned], } [parent_column_info, child_column_info].each do |column_info| column_info.delete(:unsigned) unless column_info[:unsigned] # for PostgreSQL column_info[:type] = { serial: :integer, - bigserial: :bigint + bigserial: :bigint, }.fetch(column_info[:type], column_info[:type]) end next unless parent_column_info != child_column_info + parent_label = "#{parent_table}.id" child_label = "#{child_table}.#{column_name}" label_len = [parent_label.length, child_label.length].max @logger.warn(format(<<-MSG, label_len, parent_label, label_len, child_label)) @@ -569,9 +554,10 @@ end end def check_table_existence(definition) return unless @options[:tables] + @options[:tables].each do |table_name| @logger.warn "[WARNING] '#{table_name}' definition is not found" unless definition.key?(table_name) end end end