From 3cc62a977626bc9964d9f4bad85a997edf3a02c8 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Mon, 29 Feb 2016 13:26:12 +0900 Subject: Fix `NoMethodError: undefined method `fields' for nil:NilClass` Currently `exec_query` raises `NoMethodError` when executing no result queries (`INSERT`, `UPDATE`, `DELETE`, and all DDL) in mysql2 adapter. ``` irb(main):002:0> conn.execute("create table t(a int)") (43.3ms) create table t(a int) => nil irb(main):003:0> conn.execute("insert into t values (1)") (19.3ms) insert into t values (1) => nil irb(main):004:0> conn.exec_query("insert into t values (1)") SQL (28.6ms) insert into t values (1) NoMethodError: undefined method `fields' for nil:NilClass ``` --- .../test/cases/adapters/mysql2/mysql2_adapter_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'activerecord/test/cases/adapters') diff --git a/activerecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb b/activerecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb index 4efd728754..00d23740b6 100644 --- a/activerecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb +++ b/activerecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb @@ -1,10 +1,22 @@ require "cases/helper" +require "support/ddl_helper" class Mysql2AdapterTest < ActiveRecord::Mysql2TestCase + include DdlHelper + def setup @conn = ActiveRecord::Base.connection end + def test_exec_query_nothing_raises_with_no_result_queries + assert_nothing_raised do + with_example_table do + @conn.exec_query('INSERT INTO ex (number) VALUES (1)') + @conn.exec_query('DELETE FROM ex WHERE number = 1') + end + end + end + def test_columns_for_distinct_zero_orders assert_equal "posts.id", @conn.columns_for_distinct("posts.id", []) @@ -41,4 +53,10 @@ class Mysql2AdapterTest < ActiveRecord::Mysql2TestCase assert_equal "posts.id, posts.created_at AS alias_0", @conn.columns_for_distinct("posts.id", [order]) end + + private + + def with_example_table(definition = 'id int auto_increment primary key, number int, data varchar(255)', &block) + super(@conn, 'ex', definition, &block) + end end -- cgit v1.2.3