MySQL: Check error number instead of a message

To be able to check regardless of locale.
This commit is contained in:
yuuji.yaginuma 2019-07-11 07:16:54 +09:00
parent 6e40b131d2
commit ac41b73d97
2 changed files with 6 additions and 2 deletions

@ -8,6 +8,8 @@
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 @@ def mysql2_connection(config)
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 @@ def create
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