File: 00_active_record_enum.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (31 lines) | stat: -rw-r--r-- 1,326 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true

# Override ActiveRecord::Enum.load_schema! to remove the check that verifies that
# defined enum attributes are backed by a database column.
#
# The check has been introduced in Rails 7.1: https://github.com/rails/rails/pull/45734
#
# However, there are valid use cases when the enum definition should not raise an error
# when the column does not exist in the column list:
#
# - Perform database migration on a particular version, the one that doesn't contain the column
# - Try performing another operation that loads Rails environment (for example, another migration):
#   - Rails environment is being initialized and configured
#   - Gitlab::CurrentSetting is being called to customize the configuration
#   - ApplicationSetting model tries to define a enum attribute that is not backed by a column on that patricular
#   migration version
#   - An error is raised

module ActiveRecordAttributesPatch
  def attribute(name, *args, **options)
    return super unless defined_enums.key?(name)
    return unless block_given?

    super do |subtype|
      subtype = subtype.subtype if ActiveRecord::Enum::EnumType === subtype
      ActiveRecord::Enum::EnumType.new(name, defined_enums[name], subtype)
    end
  end
end

ActiveRecord::Attributes::ClassMethods.prepend(ActiveRecordAttributesPatch)