Project

General

Profile

Migration 3.0.3 -> 3.3.3 : Custom Fields and default values

Added by Ema Nymton over 7 years ago

Hi,

I try to migrate from 3.0.3 to 3.3.3 and I have to handle a change in custom fields behavior.

My Redmine database is old and I have some issues where some custom fields does not exist at all (no "custom_value" entry)

In 3.0.3, Redmine display an empty value for these custom fields
In 3.3.3, Redmine display... the default value of the custom field !

I think the behavior change came from CustomValue.initialize() :
3.0.3 :

    if new_record? && custom_field && (customized_type.blank? || (customized && customized.new_record?))
      self.value ||= custom_field.default_value
    end

3.3.3 :
    if new_record? && custom_field && !attributes.key?(:value)
      self.value ||= custom_field.default_value
    end

My question is how to handle that :

A) Add a migration script to create all missing custom_value entries with a NULL value
=> If it's NOT a Redmine bug, I think it's the better thing to do

B) monkey patch CustomValue.initialize for restore 3.0.3 implementation
=> If it's a Redmine bug, it will restore old behavior until fix in future version


Replies (4)

RE: Migration 3.0.3 -> 3.3.3 : Custom Fields and default values - Added by Mischa The Evil over 7 years ago

Ema Nymton wrote:

In 3.0.3, Redmine display an empty value for these custom fields
In 3.3.3, Redmine display... the default value of the custom field !

If you are talking about the issue detail view here, then you're experiencing a known defect that got introduced with Redmine 3.2.0. See #25726.

RE: Migration 3.0.3 -> 3.3.3 : Custom Fields and default values - Added by Ema Nymton over 7 years ago

Ok, thanks, so it's a real bug and not the expected behavior of the new version of Redmine

I will locally patch my version for fix it ;o)

FYI, the change came from the fix for #21074 . I use only 1 tracker so I don't really need it

RE: Migration 3.0.3 -> 3.3.3 : Custom Fields and default values - Added by Mischa The Evil over 7 years ago

Ema Nymton wrote:

I will locally patch my version for fix it ;o)

FYI, the change came from the fix for #21074 . I use only 1 tracker so I don't really need it

Just be sure to know what you're doing before patching the core yourself to prevent (future) problems.

RE: Migration 3.0.3 -> 3.3.3 : Custom Fields and default values - Added by Ema Nymton over 7 years ago

don't worry ;o)

I patch it in a plugin and not in the core file :

module MyCore
  module Models
    module MyCustomValue

      # restore implementation BEFORE 3.2.0
      # The fix for http://www.redmine.org/issues/21074 have an unexpected behavior :
      # Custom fields with no values (no custom_value in database) are set to default value instead of empty
      # It's a redmine issue, followed by http://www.redmine.org/issues/25726 , not fixed yet.
      # 
      # With previous implementation, we fix this issue 25726 and we just have the 21074 issue which is more limited :
      # - We need 2 trackers
      # - We need a custom field on 1 tracker and not in the other
      # - We have to change the tracker of a Redmine issue
      def initialize(attributes=nil, *args)
        # replace "super" call by calling ActiveRecord initialize method and not the CustomValue initialize
        #super
        ActiveRecord::Base.instance_method(:initialize).bind(self).call(attributes, *args)
        #if new_record? && custom_field && !attributes.key?(:value)
        if new_record? && custom_field && (customized_type.blank? || (customized && customized.new_record?))
          self.value ||= custom_field.default_value
        end
      end

    end
  end
end

CustomValue.send(:prepend, MyCore::Models::MyCustomValue)
    (1-4/4)