diff options
author | yuuji.yaginuma <yuuji.yaginuma@gmail.com> | 2019-07-11 07:16:54 +0900 |
---|---|---|
committer | yuuji.yaginuma <yuuji.yaginuma@gmail.com> | 2019-07-11 07:18:11 +0900 |
commit | ac41b73d97a80f2552196516e29d7e3335e2d556 (patch) | |
tree | 0b8870ae91f73c58131f954bb663d8ac6ba8a7c4 | |
parent | 6e40b131d2c7208e116d267e4b40dcf621f8b442 (diff) | |
download | rails-ac41b73d97a80f2552196516e29d7e3335e2d556.tar.gz rails-ac41b73d97a80f2552196516e29d7e3335e2d556.tar.bz2 rails-ac41b73d97a80f2552196516e29d7e3335e2d556.zip |
MySQL: Check error number instead of a message
To be able to check regardless of locale.
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/tasks/mysql_database_tasks.rb | 4 |
2 files changed, 6 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb index d9eab9582e..1df9ac32c9 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb @@ -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 diff --git a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb index 0d9917a4db..a7e04007a9 100644 --- a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb @@ -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 |