mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
MySQL: Check error number instead of a message
To be able to check regardless of locale.
This commit is contained in:
parent
6e40b131d2
commit
ac41b73d97
2 changed files with 6 additions and 2 deletions
|
@ -8,6 +8,8 @@ require "mysql2"
|
|||
|
||||
module ActiveRecord
|
||||
module ConnectionHandling # :nodoc:
|
||||
ER_BAD_DB_ERROR = 1049
|
||||
|
||||
# Establishes a connection to the database that's used by all Active Record objects.
|
||||
def mysql2_connection(config)
|
||||
config = config.symbolize_keys
|
||||
|
@ -22,7 +24,7 @@ module ActiveRecord
|
|||
client = Mysql2::Client.new(config)
|
||||
ConnectionAdapters::Mysql2Adapter.new(client, logger, nil, config)
|
||||
rescue Mysql2::Error => error
|
||||
if error.message.include?("Unknown database")
|
||||
if error.error_number == ER_BAD_DB_ERROR
|
||||
raise ActiveRecord::NoDatabaseError
|
||||
else
|
||||
raise
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
module ActiveRecord
|
||||
module Tasks # :nodoc:
|
||||
class MySQLDatabaseTasks # :nodoc:
|
||||
ER_DB_CREATE_EXISTS = 1007
|
||||
|
||||
delegate :connection, :establish_connection, to: ActiveRecord::Base
|
||||
|
||||
def initialize(configuration)
|
||||
|
@ -14,7 +16,7 @@ module ActiveRecord
|
|||
connection.create_database configuration["database"], creation_options
|
||||
establish_connection configuration
|
||||
rescue ActiveRecord::StatementInvalid => error
|
||||
if error.message.include?("database exists")
|
||||
if error.cause.error_number == ER_DB_CREATE_EXISTS
|
||||
raise DatabaseAlreadyExists
|
||||
else
|
||||
raise
|
||||
|
|
Loading…
Reference in a new issue