Browse Source

Fix #2318 - migration 20180220171219 CheckForObjectAttributes failed

Billy Zhou 6 years ago
parent
commit
f711e1019e

+ 9 - 0
db/migrate/20180220171219_check_for_object_attributes.rb

@@ -7,6 +7,7 @@ class CheckForObjectAttributes < ActiveRecord::Migration[5.1]
       fix_nil_data_option(attribute)
       fix_options(attribute)
       fix_relation(attribute)
+      fix_interger_missing_min_max(attribute)
 
       next if !attribute.changed?
 
@@ -38,4 +39,12 @@ class CheckForObjectAttributes < ActiveRecord::Migration[5.1]
 
     attribute[:data_option][:relation] = ''
   end
+
+  # fixes issue #2318 - Upgrade to Zammad 2.7 was not possible (migration 20180220171219 CheckForObjectAttributes failed)
+  def fix_interger_missing_min_max(attribute)
+    return if attribute[:data_type] != 'integer'
+
+    attribute[:data_option][:min] = 0 if !attribute[:data_option][:min]
+    attribute[:data_option][:max] = 1_000_000 if !attribute[:data_option][:max]
+  end
 end

+ 15 - 0
spec/db/migrate/check_for_object_attributes_spec.rb

@@ -112,4 +112,19 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
       end
     end
   end
+
+  # regression test for issue #2318 - Upgrade to Zammad 2.7 was not possible (migration 20180220171219 CheckForObjectAttributes failed)
+  context 'for interger attributes' do
+    it 'missing :min and :max' do
+      attribute = create(:object_manager_attribute_integer)
+      attribute.update_columns(data_option: {}) # rubocop:disable Rails/SkipsModelValidations
+
+      expect { migrate }.not_to raise_error
+
+      attribute.reload
+
+      expect(attribute[:data_option][:min]).to be_a(Integer)
+      expect(attribute[:data_option][:max]).to be_a(Integer)
+    end
+  end
 end

+ 11 - 0
spec/factories/object_manager_attribute.rb

@@ -53,6 +53,17 @@ FactoryBot.define do
     end
   end
 
+  factory :object_manager_attribute_integer, parent: :object_manager_attribute do
+    data_type   'integer'
+    data_option do
+      {
+        'default'   => 0,
+        'min'       => 0,
+        'max'       => 9999,
+      }
+    end
+  end
+
   factory :object_manager_attribute_date, parent: :object_manager_attribute do
     name        'date_attribute'
     data_type   'date'