A platform for community discussion. Free, open, simple.
DEV: Change UserField#field_type to an ActiveRecord enum (#27444)
Ted Johansson5963c0364385
diff --git a/app/models/user_field.rb b/app/models/user_field.rbindex 1987bcb224e..56d9da8a220 100644--- a/app/models/user_field.rb+++ b/app/models/user_field.rb@@ -7,7 +7,7 @@ class UserField < ActiveRecord::Basedeprecate_column :required, drop_from: "3.3"- validates_presence_of :description, :field_type+ validates_presence_of :descriptionvalidates_presence_of :name, unless: -> { field_type == "confirm" }has_many :user_field_options, dependent: :destroyhas_one :directory_column, dependent: :destroy@@ -20,6 +20,10 @@ class UserField < ActiveRecord::Baseenum :requirement, { optional: 0, for_all_users: 1, on_signup: 2 }.freeze+ # TODO: Drop old field_type and rename this column into field_type and remove alias.+ enum :field_type_enum, { text: 0, confirm: 1, dropdown: 2, multiselect: 3 }.freeze+ alias_attribute :field_type, :field_type_enum+def self.max_length2048end@@ -47,7 +51,7 @@ end## id :integer not null, primary key# name :string not null-# field_type :string not null+# field_type :string# created_at :datetime not null# updated_at :datetime not null# editable :boolean default(FALSE), not null@@ -60,4 +64,5 @@ end# external_type :string# searchable :boolean default(FALSE), not null# requirement :integer default("optional"), not null+# field_type_enum :integer not null#diff --git a/db/migrate/20240612063735_add_field_type_enum_to_user_fields.rb b/db/migrate/20240612063735_add_field_type_enum_to_user_fields.rbnew file mode 100644index 00000000000..096c0dfa047--- /dev/null+++ b/db/migrate/20240612063735_add_field_type_enum_to_user_fields.rb@@ -0,0 +1,23 @@+# frozen_string_literal: true++class AddFieldTypeEnumToUserFields < ActiveRecord::Migration[7.0]+ def change+ add_column :user_fields, :field_type_enum, :integer++ up_only do+ execute(<<~SQL)+ UPDATE user_fields+ SET field_type_enum =+ CASE+ WHEN field_type = 'text' THEN 0+ WHEN field_type = 'confirm' THEN 1+ WHEN field_type = 'dropdown' THEN 2+ WHEN field_type = 'multiselect' THEN 3+ END+ SQL++ change_column_null :user_fields, :field_type, true+ change_column_null :user_fields, :field_type_enum, false+ end+ end+end