Skip to content

Commit e1f566a

Browse files
authored
Merge pull request rails#45734 from fatkodima/fix-columnless-enums
Raise when defining an enum not backed by a database column
2 parents d0e26bf + 6c5fab0 commit e1f566a

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

activerecord/lib/active_record/enum.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ def inherited(base) # :nodoc:
121121
super
122122
end
123123

124+
def load_schema! # :nodoc:
125+
attributes_to_define_after_schema_loads.each do |name, (cast_type, _default)|
126+
unless columns_hash.key?(name)
127+
cast_type = cast_type[type_for_attribute(name)] if Proc === cast_type
128+
raise "Unknown enum attribute '#{name}' for #{self.name}" if Enum::EnumType === cast_type
129+
end
130+
end
131+
end
132+
124133
class EnumType < Type::Value # :nodoc:
125134
delegate :type, to: :subtype
126135

activerecord/lib/active_record/model_schema.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,8 @@ def load_schema!
591591
user_provided_default: false
592592
)
593593
end
594+
595+
super
594596
end
595597

596598
def reload_schema_from_cache

activerecord/test/cases/enum_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,4 +1006,18 @@ def self.name
10061006
ensure
10071007
ActiveRecord::Base.logger = old_logger
10081008
end
1009+
1010+
test "raises for columnless enums" do
1011+
klass = Class.new(ActiveRecord::Base) do
1012+
def self.name
1013+
"Book"
1014+
end
1015+
enum columnless_genre: [:adventure, :comic]
1016+
end
1017+
1018+
error = assert_raises(RuntimeError) do
1019+
klass.columns # load schema
1020+
end
1021+
assert_equal "Unknown enum attribute 'columnless_genre' for Book", error.message
1022+
end
10091023
end

activerecord/test/cases/statement_cache_test.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,29 +112,29 @@ def test_unprepared_statements_dont_share_a_cache_with_prepared_statements
112112
end
113113

114114
def test_find_by_does_not_use_statement_cache_if_table_name_is_changed
115-
book = Book.create(name: "my book")
115+
liquid = Liquid.create(name: "salty")
116116

117-
Book.find_by(name: book.name) # warming the statement cache.
117+
Liquid.find_by(name: liquid.name) # warming the statement cache.
118118

119119
# changing the table name should change the query that is not cached.
120-
Book.table_name = :birds
121-
assert_nil Book.find_by(name: book.name)
120+
Liquid.table_name = :birds
121+
assert_nil Liquid.find_by(name: liquid.name)
122122
ensure
123-
Book.table_name = :books
123+
Liquid.table_name = :liquid
124124
end
125125

126126
def test_find_does_not_use_statement_cache_if_table_name_is_changed
127-
book = Book.create(name: "my book")
127+
liquid = Liquid.create(name: "salty")
128128

129-
Book.find(book.id) # warming the statement cache.
129+
Liquid.find(liquid.id) # warming the statement cache.
130130

131131
# changing the table name should change the query that is not cached.
132-
Book.table_name = :birds
132+
Liquid.table_name = :birds
133133
assert_raise ActiveRecord::RecordNotFound do
134-
Book.find(book.id)
134+
Liquid.find(liquid.id)
135135
end
136136
ensure
137-
Book.table_name = :books
137+
Liquid.table_name = :liquid
138138
end
139139

140140
def test_find_association_does_not_use_statement_cache_if_table_name_is_changed

0 commit comments

Comments
 (0)