Merge pull request #45734 from fatkodima/fix-columnless-enums
Raise when defining an enum not backed by a database column
This commit is contained in:
commit
e1f566a6f4
@ -121,6 +121,15 @@ def inherited(base) # :nodoc:
|
|||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def load_schema! # :nodoc:
|
||||||
|
attributes_to_define_after_schema_loads.each do |name, (cast_type, _default)|
|
||||||
|
unless columns_hash.key?(name)
|
||||||
|
cast_type = cast_type[type_for_attribute(name)] if Proc === cast_type
|
||||||
|
raise "Unknown enum attribute '#{name}' for #{self.name}" if Enum::EnumType === cast_type
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class EnumType < Type::Value # :nodoc:
|
class EnumType < Type::Value # :nodoc:
|
||||||
delegate :type, to: :subtype
|
delegate :type, to: :subtype
|
||||||
|
|
||||||
|
@ -591,6 +591,8 @@ def load_schema!
|
|||||||
user_provided_default: false
|
user_provided_default: false
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def reload_schema_from_cache
|
def reload_schema_from_cache
|
||||||
|
@ -1006,4 +1006,18 @@ def self.name
|
|||||||
ensure
|
ensure
|
||||||
ActiveRecord::Base.logger = old_logger
|
ActiveRecord::Base.logger = old_logger
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "raises for columnless enums" do
|
||||||
|
klass = Class.new(ActiveRecord::Base) do
|
||||||
|
def self.name
|
||||||
|
"Book"
|
||||||
|
end
|
||||||
|
enum columnless_genre: [:adventure, :comic]
|
||||||
|
end
|
||||||
|
|
||||||
|
error = assert_raises(RuntimeError) do
|
||||||
|
klass.columns # load schema
|
||||||
|
end
|
||||||
|
assert_equal "Unknown enum attribute 'columnless_genre' for Book", error.message
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -112,29 +112,29 @@ def test_unprepared_statements_dont_share_a_cache_with_prepared_statements
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_find_by_does_not_use_statement_cache_if_table_name_is_changed
|
def test_find_by_does_not_use_statement_cache_if_table_name_is_changed
|
||||||
book = Book.create(name: "my book")
|
liquid = Liquid.create(name: "salty")
|
||||||
|
|
||||||
Book.find_by(name: book.name) # warming the statement cache.
|
Liquid.find_by(name: liquid.name) # warming the statement cache.
|
||||||
|
|
||||||
# changing the table name should change the query that is not cached.
|
# changing the table name should change the query that is not cached.
|
||||||
Book.table_name = :birds
|
Liquid.table_name = :birds
|
||||||
assert_nil Book.find_by(name: book.name)
|
assert_nil Liquid.find_by(name: liquid.name)
|
||||||
ensure
|
ensure
|
||||||
Book.table_name = :books
|
Liquid.table_name = :liquid
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_find_does_not_use_statement_cache_if_table_name_is_changed
|
def test_find_does_not_use_statement_cache_if_table_name_is_changed
|
||||||
book = Book.create(name: "my book")
|
liquid = Liquid.create(name: "salty")
|
||||||
|
|
||||||
Book.find(book.id) # warming the statement cache.
|
Liquid.find(liquid.id) # warming the statement cache.
|
||||||
|
|
||||||
# changing the table name should change the query that is not cached.
|
# changing the table name should change the query that is not cached.
|
||||||
Book.table_name = :birds
|
Liquid.table_name = :birds
|
||||||
assert_raise ActiveRecord::RecordNotFound do
|
assert_raise ActiveRecord::RecordNotFound do
|
||||||
Book.find(book.id)
|
Liquid.find(liquid.id)
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
Book.table_name = :books
|
Liquid.table_name = :liquid
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_find_association_does_not_use_statement_cache_if_table_name_is_changed
|
def test_find_association_does_not_use_statement_cache_if_table_name_is_changed
|
||||||
|
Loading…
Reference in New Issue
Block a user