From e50fe85180648be0c4216bd0111f05be1df0988a Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Thu, 13 Aug 2015 16:35:09 +0200 Subject: [PATCH] descriptive error message when fixtures contian a missing column. Closes #21201. --- activerecord/CHANGELOG.md | 6 ++++++ .../connection_adapters/abstract/database_statements.rb | 8 ++++++-- activerecord/test/cases/fixtures_test.rb | 7 +++++++ activerecord/test/fixtures/naked/yml/parrots.yml | 2 ++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 activerecord/test/fixtures/naked/yml/parrots.yml diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index c3b2d157f3..bd86dfb88e 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Descriptive error message when fixtures contain a missing column. + + Closes #21201. + + *Yves Senn* + * `ActiveRecord::Tasks::PostgreSQLDatabaseTasks` fail if shellout to postgresql commands (like `pg_dump`) is not successful. diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb index 38dd9578fe..1e13b24867 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -289,8 +289,12 @@ module ActiveRecord columns = schema_cache.columns_hash(table_name) binds = fixture.map do |name, value| - type = lookup_cast_type_from_column(columns[name]) - Relation::QueryAttribute.new(name, value, type) + if column = columns[name] + type = lookup_cast_type_from_column(column) + Relation::QueryAttribute.new(name, value, type) + else + raise Fixture::FixtureError, %(table "#{table_name}" has no column named "#{name}".) + end end key_list = fixture.keys.map { |name| quote_column_name(name) } value_list = prepare_binds_for_database(binds).map do |value| diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 03a187ae92..76ea950fb1 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -217,6 +217,13 @@ class FixturesTest < ActiveRecord::TestCase end end + def test_yaml_file_with_invalid_column + e = assert_raise(ActiveRecord::Fixture::FixtureError) do + ActiveRecord::FixtureSet.create_fixtures(FIXTURES_ROOT + "/naked/yml", "parrots") + end + assert_equal(%(table "parrots" has no column named "arrr".), e.message) + end + def test_omap_fixtures assert_nothing_raised do fixtures = ActiveRecord::FixtureSet.new(Account.connection, 'categories', Category, FIXTURES_ROOT + "/categories_ordered") diff --git a/activerecord/test/fixtures/naked/yml/parrots.yml b/activerecord/test/fixtures/naked/yml/parrots.yml new file mode 100644 index 0000000000..3e10331105 --- /dev/null +++ b/activerecord/test/fixtures/naked/yml/parrots.yml @@ -0,0 +1,2 @@ +george: + arrr: "Curious George"