Skip to content

Commit

Permalink
Raise when defining an enum not backed by a database column
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Aug 3, 2022
1 parent 887dc9a commit 6c5fab0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
9 changes: 9 additions & 0 deletions activerecord/lib/active_record/enum.rb
Expand Up @@ -121,6 +121,15 @@ def inherited(base) # :nodoc:
super
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:
delegate :type, to: :subtype

Expand Down
2 changes: 2 additions & 0 deletions activerecord/lib/active_record/model_schema.rb
Expand Up @@ -591,6 +591,8 @@ def load_schema!
user_provided_default: false
)
end

super
end

def reload_schema_from_cache
Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/enum_test.rb
Expand Up @@ -1006,4 +1006,18 @@ def self.name
ensure
ActiveRecord::Base.logger = old_logger
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
20 changes: 10 additions & 10 deletions activerecord/test/cases/statement_cache_test.rb
Expand Up @@ -112,29 +112,29 @@ def test_unprepared_statements_dont_share_a_cache_with_prepared_statements
end

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.
Book.table_name = :birds
assert_nil Book.find_by(name: book.name)
Liquid.table_name = :birds
assert_nil Liquid.find_by(name: liquid.name)
ensure
Book.table_name = :books
Liquid.table_name = :liquid
end

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.
Book.table_name = :birds
Liquid.table_name = :birds
assert_raise ActiveRecord::RecordNotFound do
Book.find(book.id)
Liquid.find(liquid.id)
end
ensure
Book.table_name = :books
Liquid.table_name = :liquid
end

def test_find_association_does_not_use_statement_cache_if_table_name_is_changed
Expand Down

0 comments on commit 6c5fab0

Please sign in to comment.