diff options
Diffstat (limited to 'activerecord/lib')
4 files changed, 20 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index fda51b3d76..67c7e714e6 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -1,5 +1,7 @@ module ActiveRecord module Tasks # :nodoc: + class DatabaseAlreadyExists < StandardError; end # :nodoc: + module DatabaseTasks # :nodoc: extend self @@ -32,6 +34,8 @@ module ActiveRecord def create(*arguments) configuration = arguments.first class_for_adapter(configuration['adapter']).new(*arguments).create + rescue DatabaseAlreadyExists + $stderr.puts "#{configuration['database']} already exists" rescue Exception => error $stderr.puts error, *(error.backtrace) $stderr.puts "Couldn't create database for #{configuration.inspect}" diff --git a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb index 3d27c97254..3ae5aa2cea 100644 --- a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb @@ -24,10 +24,13 @@ module ActiveRecord connection.create_database configuration['database'], creation_options connection.execute grant_statement.gsub(/\s+/, ' ').strip establish_connection configuration - rescue error_class => error - $stderr.puts error.error - $stderr.puts "Couldn't create database for #{configuration.inspect}, #{creation_options.inspect}" - $stderr.puts "(If you set the charset manually, make sure you have a matching collation)" if configuration['encoding'] + rescue error_class, ActiveRecord::StatementInvalid => error + if /database exists/ === error.message + raise DatabaseAlreadyExists + else + $stderr.puts "Couldn't create database for #{configuration.inspect}, #{creation_options.inspect}" + $stderr.puts "(If you set the charset manually, make sure you have a matching collation)" if configuration['encoding'] + end end def drop @@ -87,14 +90,11 @@ module ActiveRecord end def error_class - case configuration['adapter'] - when /jdbc/ + if configuration['adapter'] =~ /jdbc/ require 'active_record/railties/jdbcmysql_error' ArJdbcMySQL::Error - when /mysql2/ - Mysql2::Error else - Mysql::Error + defined?(Mysql2) ? Mysql2::Error : Mysql::Error end end diff --git a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb index ea5cb888fb..e6a85b1917 100644 --- a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb @@ -18,6 +18,12 @@ module ActiveRecord connection.create_database configuration['database'], configuration.merge('encoding' => encoding) establish_connection configuration + rescue ActiveRecord::StatementInvalid => error + if /database .* already exists/ === error.message + raise DatabaseAlreadyExists + else + raise + end end def drop diff --git a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb index da01058a82..33c43c7774 100644 --- a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb @@ -9,10 +9,7 @@ module ActiveRecord end def create - if File.exist?(configuration['database']) - $stderr.puts "#{configuration['database']} already exists" - return - end + raise DatabaseAlreadyExists if File.exist?(configuration['database']) establish_connection configuration connection |