1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #36766 from jhawthorn/call_connection_error_number

Use connection.error_number in MySQLDatabaseTasks
This commit is contained in:
John Hawthorn 2019-07-25 14:54:44 -07:00 committed by GitHub
commit ec7aa03c98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 4 deletions

View file

@ -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

View file

@ -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