mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Clarify 'database does not exist' message and implementation.
* Clarify what the situation is and what to do.
* Advise loading schema using `rake db:setup` instead of migrating.
* Use a rescue in the initializer rather than extending the error
message in-place.
* Preserve the original backtrace of other errors by using `raise`
rather than raising again with `raise error`.
References 0ec45cd15d
This commit is contained in:
parent
5d0a4e0cb6
commit
9aa7c25c28
6 changed files with 24 additions and 27 deletions
|
@ -20,9 +20,9 @@ module ActiveRecord
|
|||
ConnectionAdapters::Mysql2Adapter.new(client, logger, options, config)
|
||||
rescue Mysql2::Error => error
|
||||
if error.message.include?("Unknown database")
|
||||
raise ActiveRecord::NoDatabaseError.new(error.message)
|
||||
raise ActiveRecord::NoDatabaseError.new(error.message, error)
|
||||
else
|
||||
raise error
|
||||
raise
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -36,9 +36,9 @@ module ActiveRecord
|
|||
ConnectionAdapters::MysqlAdapter.new(mysql, logger, options, config)
|
||||
rescue Mysql::Error => error
|
||||
if error.message.include?("Unknown database")
|
||||
raise ActiveRecord::NoDatabaseError.new(error.message)
|
||||
raise ActiveRecord::NoDatabaseError.new(error.message, error)
|
||||
else
|
||||
raise error
|
||||
raise
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -718,9 +718,9 @@ module ActiveRecord
|
|||
configure_connection
|
||||
rescue ::PG::Error => error
|
||||
if error.message.include?("does not exist")
|
||||
raise ActiveRecord::NoDatabaseError.new(error.message)
|
||||
raise ActiveRecord::NoDatabaseError.new(error.message, error)
|
||||
else
|
||||
raise error
|
||||
raise
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -33,9 +33,9 @@ module ActiveRecord
|
|||
ConnectionAdapters::SQLite3Adapter.new(db, logger, config)
|
||||
rescue Errno::ENOENT => error
|
||||
if error.message.include?("No such file or directory")
|
||||
raise ActiveRecord::NoDatabaseError.new(error.message)
|
||||
raise ActiveRecord::NoDatabaseError.new(error.message, error)
|
||||
else
|
||||
raise error
|
||||
raise
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -95,15 +95,7 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
# Raised when a given database does not exist
|
||||
class NoDatabaseError < ActiveRecordError
|
||||
def initialize(message)
|
||||
super extend_message(message)
|
||||
end
|
||||
|
||||
# can be over written to add additional error information.
|
||||
def extend_message(message)
|
||||
message
|
||||
end
|
||||
class NoDatabaseError < StatementInvalid
|
||||
end
|
||||
|
||||
# Raised on attempt to save stale record. Record is stale when it's being saved in another query after
|
||||
|
|
|
@ -116,17 +116,22 @@ module ActiveRecord
|
|||
# and then establishes the connection.
|
||||
initializer "active_record.initialize_database" do |app|
|
||||
ActiveSupport.on_load(:active_record) do
|
||||
|
||||
class ActiveRecord::NoDatabaseError
|
||||
remove_possible_method :extend_message
|
||||
def extend_message(message)
|
||||
message << "Run `$ bin/rake db:create db:migrate` to create your database"
|
||||
message
|
||||
end
|
||||
end
|
||||
|
||||
self.configurations = Rails.application.config.database_configuration
|
||||
establish_connection
|
||||
|
||||
begin
|
||||
establish_connection
|
||||
rescue ActiveRecord::NoDatabaseError
|
||||
warn <<-end_warning
|
||||
Oops - You have a database configured, but it doesn't exist yet!
|
||||
|
||||
Here's how to get started:
|
||||
|
||||
1. Configure your database in config/database.yml.
|
||||
2. Run `bin/rake db:create` to create the database.
|
||||
3. Run `bin/rake db:setup` to load your database schema.
|
||||
end_warning
|
||||
raise
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue