diff options
author | John Hawthorn <john@hawthorn.email> | 2019-07-25 13:59:15 -0700 |
---|---|---|
committer | John Hawthorn <john@hawthorn.email> | 2019-07-25 14:15:39 -0700 |
commit | 15c81c8ed4930574d9900a6f947d4e0ebaa82a2f (patch) | |
tree | 5181c4435fef10ca4367b786e3a9c6dcc76242b1 /activerecord | |
parent | bd5edc29709116104345cf09c17f1db89428753e (diff) | |
download | rails-15c81c8ed4930574d9900a6f947d4e0ebaa82a2f.tar.gz rails-15c81c8ed4930574d9900a6f947d4e0ebaa82a2f.tar.bz2 rails-15c81c8ed4930574d9900a6f947d4e0ebaa82a2f.zip |
Use connection.error_number in MySQLDatabaseTasks
MySQLDatabaseTasks, like AbstractMysqlAdapter, should be able to operate
on any mysql adapter, not just mysql2. Errors having a .error_number
attribute is a mysql2 specific API, which we (Rails) don't control, so
we should instead use connection.error_number(err), which we do.
This also updates tests to better test how this really works, previously
it stubbed create_database to raise Tasks::DatabaseAlreadyExists, which
can never happen.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/tasks/mysql_database_tasks.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/tasks/mysql_rake_test.rb | 11 |
2 files changed, 9 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb index a7e04007a9..e3efeb75b5 100644 --- a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb @@ -16,7 +16,7 @@ module ActiveRecord connection.create_database configuration["database"], creation_options establish_connection configuration rescue ActiveRecord::StatementInvalid => error - if error.cause.error_number == ER_DB_CREATE_EXISTS + if connection.error_number(error.cause) == ER_DB_CREATE_EXISTS raise DatabaseAlreadyExists else raise diff --git a/activerecord/test/cases/tasks/mysql_rake_test.rb b/activerecord/test/cases/tasks/mysql_rake_test.rb index 258132835f..ac3c5bc26e 100644 --- a/activerecord/test/cases/tasks/mysql_rake_test.rb +++ b/activerecord/test/cases/tasks/mysql_rake_test.rb @@ -7,7 +7,10 @@ if current_adapter?(:Mysql2Adapter) module ActiveRecord class MysqlDBCreateTest < ActiveRecord::TestCase def setup - @connection = Class.new { def create_database(*); end }.new + @connection = Class.new do + def create_database(*); end + def error_number(_); end + end.new @configuration = { "adapter" => "mysql2", "database" => "my-app-db" @@ -90,9 +93,11 @@ if current_adapter?(:Mysql2Adapter) with_stubbed_connection_establish_connection do ActiveRecord::Base.connection.stub( :create_database, - proc { raise ActiveRecord::Tasks::DatabaseAlreadyExists } + proc { raise ActiveRecord::StatementInvalid } ) do - ActiveRecord::Tasks::DatabaseTasks.create @configuration + ActiveRecord::Base.connection.stub(:error_number, 1007) do + ActiveRecord::Tasks::DatabaseTasks.create @configuration + end assert_equal "Database 'my-app-db' already exists\n", $stderr.string end |