ActiveRecord enum behaves differently between rails 4 and rails 5
Summary
ActiveRecord::Enum
behaves differently between rails 4 and rails 5 as @vsizov reported at https://gitlab.com/gitlab-org/gitlab-ce/commit/6c0d5618050691b5e06b9f576e45bd7ccfaa7c46#note_39933822.
Specifically, rails 5 enum can't evaluate correctly when it has nil
value. For example,
enum failure_reason: {
unknown_failure: nil,
script_failure: 1,
api_failure: 2,
stuck_or_timeout_failure: 3,
runner_system_failure: 4
}
commit_status.unknown_failure?
returns false, even if it's true in rails 4.
This has been patched at https://gitlab.com/gitlab-org/gitlab-ce/commit/6c0d5618050691b5e06b9f576e45bd7ccfaa7c46#note_39933822 as
# This can be considered as a hack as nil value is not documented (as a enum value)
# in Rails Enum documentation and it behave defferently in rails 4 and rails 5.
# In particular, unknown_failure? always returns false even if value of failure_reason is nil.
# This method just overwrites the autogenerated unknown_failure? enum method.
def unknown_failure?
failure_reason.nil?
end
Ideally, we want to fix rails 5 itself or something better solution.
Also, we have some enum
which have nil
. We should take care of them as well.
- https://gitlab.com/gitlab-org/gitlab-ce/blob/rails5/app/models/ci/pipeline.rb#L45
- https://gitlab.com/gitlab-org/gitlab-ce/blob/rails5/app/models/ci/pipeline.rb#L55
/cc @ayufan @bikebilly
Related https://gitlab.com/gitlab-org/gitlab-ce/issues/14286