diff options
author | John Hawthorn <john@hawthorn.email> | 2019-07-29 08:40:57 -0700 |
---|---|---|
committer | John Hawthorn <john@hawthorn.email> | 2019-07-29 08:40:57 -0700 |
commit | 69700c9ee760c5880fc80af70a89b42bb791cf98 (patch) | |
tree | cdb954cd1ca7ea406be05278921a01fd6e0d97a8 /activerecord/test | |
parent | 2e6d9af6c8b9900481dabfca685b3e335c2f27aa (diff) | |
download | rails-69700c9ee760c5880fc80af70a89b42bb791cf98.tar.gz rails-69700c9ee760c5880fc80af70a89b42bb791cf98.tar.bz2 rails-69700c9ee760c5880fc80af70a89b42bb791cf98.zip |
Move DatabaseAlreadyExists detection to DB adapter
Previously it was the responsibility of the database tasks to translate
the invalid statement from creating a duplicate database into an
ActiveRecord::Tasks::DatabaseAlreadyExists error.
It's actually easier for us to do this detection inside of the adapter,
where we already do a case statement on the return code to translate the
error.
This commit introduces ActiveRecord::DatabaseAlreadyExists, a subclass
of StatementInvalid, and updates both AbstractMysqlAdapter and
PostgresqlAdapter to return this more specific exception in that case.
Because this is a subclass of the old exception, StatementInvalid, it
should be backwards compatible with any code expecting that from
create_database.
This works for both create_database and exectute("CREATE DATABASE")
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/tasks/mysql_rake_test.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/tasks/postgresql_rake_test.rb | 2 |
2 files changed, 3 insertions, 5 deletions
diff --git a/activerecord/test/cases/tasks/mysql_rake_test.rb b/activerecord/test/cases/tasks/mysql_rake_test.rb index ac3c5bc26e..4b039395e8 100644 --- a/activerecord/test/cases/tasks/mysql_rake_test.rb +++ b/activerecord/test/cases/tasks/mysql_rake_test.rb @@ -93,11 +93,9 @@ if current_adapter?(:Mysql2Adapter) with_stubbed_connection_establish_connection do ActiveRecord::Base.connection.stub( :create_database, - proc { raise ActiveRecord::StatementInvalid } + proc { raise ActiveRecord::DatabaseAlreadyExists } ) do - ActiveRecord::Base.connection.stub(:error_number, 1007) do - ActiveRecord::Tasks::DatabaseTasks.create @configuration - end + ActiveRecord::Tasks::DatabaseTasks.create @configuration assert_equal "Database 'my-app-db' already exists\n", $stderr.string end diff --git a/activerecord/test/cases/tasks/postgresql_rake_test.rb b/activerecord/test/cases/tasks/postgresql_rake_test.rb index f9df650687..d74ba0580d 100644 --- a/activerecord/test/cases/tasks/postgresql_rake_test.rb +++ b/activerecord/test/cases/tasks/postgresql_rake_test.rb @@ -129,7 +129,7 @@ if current_adapter?(:PostgreSQLAdapter) with_stubbed_connection_establish_connection do ActiveRecord::Base.connection.stub( :create_database, - proc { raise ActiveRecord::Tasks::DatabaseAlreadyExists } + proc { raise ActiveRecord::DatabaseAlreadyExists } ) do ActiveRecord::Tasks::DatabaseTasks.create @configuration |