Permalink
Browse files

Merge pull request #13469 from schneems/schneems/no-database-errors-all

Raise NoDatabaseError when db does not exist
  • Loading branch information...
2 parents 959cfce + f0311c2 commit 9705df5c2bf29058c8e7c8d4fa91b95ffd3a74c9 @josevalim josevalim committed Dec 24, 2013
@@ -27,10 +27,10 @@
*Carlos Antonio da Silva*
-* When connecting to a non-existant postgresql database, the error:
+* When connecting to a non-existant database, the error:
`ActiveRecord::NoDatabaseError` will now be raised. When being used with Rails
the error message will include information on how to create a database:
- `rake db:create`
+ `rake db:create`. Supported adapters: postgresql, mysql, mysql2, sqlite3
*Richard Schneeman*
@@ -18,6 +18,12 @@ def mysql2_connection(config)
client = Mysql2::Client.new(config)
options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
ConnectionAdapters::Mysql2Adapter.new(client, logger, options, config)
+ rescue Mysql2::Error => error
+ if error.message.include?("Unknown database")
+ raise ActiveRecord::NoDatabaseError.new(error.message)
+ else
+ raise error
+ end
end
end
@@ -34,6 +34,12 @@ def mysql_connection(config)
default_flags |= Mysql::CLIENT_FOUND_ROWS if Mysql.const_defined?(:CLIENT_FOUND_ROWS)
options = [host, username, password, database, port, socket, default_flags]
ConnectionAdapters::MysqlAdapter.new(mysql, logger, options, config)
+ rescue Mysql::Error => error
+ if error.message.include?("Unknown database")
+ raise ActiveRecord::NoDatabaseError.new(error.message)
+ else
+ raise error
+ end
end
end
@@ -31,6 +31,12 @@ def sqlite3_connection(config)
db.busy_timeout(ConnectionAdapters::SQLite3Adapter.type_cast_config_to_integer(config[:timeout])) if config[:timeout]
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)
+ else
+ raise error
+ end
end
end
@@ -16,6 +16,20 @@ def setup
eosql
end
+ def test_bad_connection_mysql
+ assert_raise ActiveRecord::NoDatabaseError do
+ connection = ActiveRecord::Base.mysql_connection(adapter: "mysql", database: "should_not_exist-cinco-dog-db")
+ connection.exec_query('drop table if exists ex')
+ end
+ end
+
+ def test_bad_connection_mysql2
+ assert_raise ActiveRecord::NoDatabaseError do
+ connection = ActiveRecord::Base.mysql2_connection(adapter: "mysql2", database: "should_not_exist-cinco-dog-db")
+ connection.exec_query('drop table if exists ex')
+ end
+ end
+
def test_valid_column
column = @conn.columns('ex').find { |col| col.name == 'id' }
assert @conn.valid_type?(column.type)
@@ -26,6 +26,13 @@ def setup
ActiveSupport::Notifications.subscribe('sql.active_record', @subscriber)
end
+ def test_bad_connection
+ assert_raise ActiveRecord::NoDatabaseError do
+ connection = ActiveRecord::Base.sqlite3_connection(adapter: "sqlite3", database: "/tmp/should/_not/_exist/-cinco-dog.db")
+ connection.exec_query('drop table if exists ex')
+ end
+ end
+
def test_connect_with_url
original_connection = ActiveRecord::Base.remove_connection
tf = Tempfile.open 'whatever'

0 comments on commit 9705df5

Please sign in to comment.