Browse Source

Fixes #5430 - Migration is not working: 20241113154014 Issue5409WrongDbColumnArrayType: undefined method `type` for nil:NilClass.

Dusan Vuckovic 3 months ago
parent
commit
507e0ff2fb

+ 4 - 1
db/migrate/20241113154014_issue5409_wrong_db_column_array_type.rb

@@ -54,7 +54,10 @@ class Issue5409WrongDbColumnArrayType < ActiveRecord::Migration[7.1]
       object_table = object.table_name.to_sym
 
       table_column = ActiveRecord::Base.connection.columns(object_table).find { |c| c.name == attribute.name }
-      next if table_column.type == :string
+
+      # In case the table column does not exist in the schema, skip the check and data type change (#5430).
+      #   This can happen if the table column migration was not executed after adding an object manager attribute.
+      next if !table_column || table_column.type == :string
 
       change_column object_table, attribute.name.to_sym, :string, null: table_column.null, array: table_column.array # rubocop:disable Zammad/ExistsResetColumnInformation
 

+ 13 - 1
spec/db/migrate/issue_5409_wrong_db_column_array_type_spec.rb

@@ -22,12 +22,15 @@ RSpec.describe Issue5409WrongDbColumnArrayType, type: :db_migration do
     end
 
     context 'with multiselect and multi_tree_select object attributes' do
-      let(:screens) { { create_middle: { '-all-' => { shown: true, required: false } } } }
+      let(:screens)              { { create_middle: { '-all-' => { shown: true, required: false } } } }
+      let(:no_migration_execute) { false }
 
       before do
         create(:object_manager_attribute_multiselect, object_name: object.to_s, name: 'multi_select', display: 'Multi Select', screens: screens, additional_data_options: { options: { '1' => 'Option 1', '2' => 'Option 2', '3' => 'Option 3' } })
         create(:object_manager_attribute_multi_tree_select, object_name: object.to_s, name: 'multi_tree_select', display: 'Multi Tree Select', screens: screens, additional_data_options: { options: [ { name: 'Parent 1', value: '1', children: [ { name: 'Option A', value: '1::a' }, { name: 'Option B', value: '1::b' } ] }, { name: 'Parent 2', value: '2', children: [ { name: 'Option C', value: '2::c' } ] }, { name: 'Option 3', value: '3' } ], default: '', null: true, relation: '', maxlength: 255, nulloption: true })
 
+        next if no_migration_execute
+
         ObjectManager::Attribute.migration_execute
 
         change_column object.table_name.to_sym, :multi_select, :text, null: true, array: true
@@ -67,6 +70,15 @@ RSpec.describe Issue5409WrongDbColumnArrayType, type: :db_migration do
 
         it_behaves_like 'migrating column array type'
       end
+
+      context 'without executing table column migration' do
+        let(:object)               { Ticket }
+        let(:no_migration_execute) { true }
+
+        it 'does not throw errors (#5430)' do
+          expect { migrate }.not_to raise_error
+        end
+      end
     end
   end