diff options
author | Yuji Yaginuma <yuuji.yaginuma@gmail.com> | 2019-07-11 07:52:49 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-11 07:52:49 +0900 |
commit | 3fd5bf81c033eb2110533c8a713f38c0245356fb (patch) | |
tree | 46a373d58f228166ed59efb1b4800884dcf9dae0 /activerecord/lib | |
parent | bf19b8774e20e98f7fdcd3ac82ee17f9adee22d8 (diff) | |
parent | ac41b73d97a80f2552196516e29d7e3335e2d556 (diff) | |
download | rails-3fd5bf81c033eb2110533c8a713f38c0245356fb.tar.gz rails-3fd5bf81c033eb2110533c8a713f38c0245356fb.tar.bz2 rails-3fd5bf81c033eb2110533c8a713f38c0245356fb.zip |
Merge pull request #36653 from y-yagi/check_error_number_instead_of_message
MySQL: Check error number instead of a message
Diffstat (limited to 'activerecord/lib')
-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 |