mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #36471 from itsWill/add_database_exist_method
Add database_exists? method to connection adapters
This commit is contained in:
commit
bc8911690a
8 changed files with 71 additions and 0 deletions
|
@ -1,3 +1,7 @@
|
|||
* Add database_exists? method to connection adapters to check if a database exists.
|
||||
|
||||
*Guilherme Mansur*
|
||||
|
||||
* Loading the schema for a model that has no `table_name` raises a `TableNotSpecified` error.
|
||||
|
||||
*Guilherme Mansur*, *Eugene Kenny*
|
||||
|
|
|
@ -264,6 +264,11 @@ module ActiveRecord
|
|||
self.class::ADAPTER_NAME
|
||||
end
|
||||
|
||||
# Does the database for this adapter exist?
|
||||
def self.database_exists?(config)
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# Does this adapter support DDL rollbacks in transactions? That is, would
|
||||
# CREATE TABLE or ALTER TABLE get rolled back by a transaction?
|
||||
def supports_ddl_transactions?
|
||||
|
|
|
@ -42,6 +42,12 @@ module ActiveRecord
|
|||
configure_connection
|
||||
end
|
||||
|
||||
def self.database_exists?(config)
|
||||
!!ActiveRecord::Base.mysql2_connection(config)
|
||||
rescue ActiveRecord::NoDatabaseError
|
||||
false
|
||||
end
|
||||
|
||||
def supports_json?
|
||||
!mariadb? && database_version >= "5.7.8"
|
||||
end
|
||||
|
|
|
@ -259,6 +259,12 @@ module ActiveRecord
|
|||
@use_insert_returning = @config.key?(:insert_returning) ? self.class.type_cast_config_to_boolean(@config[:insert_returning]) : true
|
||||
end
|
||||
|
||||
def self.database_exists?(config)
|
||||
!!ActiveRecord::Base.postgresql_connection(config)
|
||||
rescue ActiveRecord::NoDatabaseError
|
||||
false
|
||||
end
|
||||
|
||||
# Is this connection alive and ready for queries?
|
||||
def active?
|
||||
@lock.synchronize do
|
||||
|
|
|
@ -98,6 +98,16 @@ module ActiveRecord
|
|||
configure_connection
|
||||
end
|
||||
|
||||
def self.database_exists?(config)
|
||||
config = config.symbolize_keys
|
||||
if config[:database] == ":memory:"
|
||||
return true
|
||||
else
|
||||
database_file = defined?(Rails.root) ? File.expand_path(config[:database], Rails.root) : config[:database]
|
||||
File.exist?(database_file)
|
||||
end
|
||||
end
|
||||
|
||||
def supports_ddl_transactions?
|
||||
true
|
||||
end
|
||||
|
|
|
@ -20,6 +20,18 @@ class Mysql2AdapterTest < ActiveRecord::Mysql2TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_database_exists_returns_false_if_database_does_not_exist
|
||||
config = ActiveRecord::Base.configurations["arunit"].merge(database: "inexistent_activerecord_unittest")
|
||||
assert_not ActiveRecord::ConnectionAdapters::Mysql2Adapter.database_exists?(config),
|
||||
"expected database to not exist"
|
||||
end
|
||||
|
||||
def test_database_exists_returns_true_when_the_database_exists
|
||||
config = ActiveRecord::Base.configurations["arunit"]
|
||||
assert ActiveRecord::ConnectionAdapters::Mysql2Adapter.database_exists?(config),
|
||||
"expected database #{config[:database]} to exist"
|
||||
end
|
||||
|
||||
def test_columns_for_distinct_zero_orders
|
||||
assert_equal "posts.id",
|
||||
@conn.columns_for_distinct("posts.id", [])
|
||||
|
|
|
@ -24,6 +24,18 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
def test_database_exists_returns_false_when_the_database_does_not_exist
|
||||
config = { database: "non_extant_database", adapter: "postgresql" }
|
||||
assert_not ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.database_exists?(config),
|
||||
"expected database #{config[:database]} to not exist"
|
||||
end
|
||||
|
||||
def test_database_exists_returns_true_when_the_database_exists
|
||||
config = ActiveRecord::Base.configurations["arunit"]
|
||||
assert ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.database_exists?(config),
|
||||
"expected database #{config[:database]} to exist"
|
||||
end
|
||||
|
||||
def test_primary_key
|
||||
with_example_table do
|
||||
assert_equal "id", @connection.primary_key("ex")
|
||||
|
|
|
@ -30,6 +30,17 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
def test_database_exists_returns_false_when_the_database_does_not_exist
|
||||
assert_not SQLite3Adapter.database_exists?(adapter: "sqlite3", database: "non_extant_db"),
|
||||
"expected non_extant_db to not exist"
|
||||
end
|
||||
|
||||
def test_database_exists_returns_true_when_databae_exists
|
||||
config = ActiveRecord::Base.configurations["arunit"]
|
||||
assert SQLite3Adapter.database_exists?(config),
|
||||
"expected #{config[:database]} to exist"
|
||||
end
|
||||
|
||||
unless in_memory_db?
|
||||
def test_connect_with_url
|
||||
original_connection = ActiveRecord::Base.remove_connection
|
||||
|
@ -53,6 +64,11 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
def test_database_exists_returns_true_for_an_in_memory_db
|
||||
assert SQLite3Adapter.database_exists?(database: ":memory:"),
|
||||
"Expected in memory database to exist"
|
||||
end
|
||||
|
||||
def test_column_types
|
||||
owner = Owner.create!(name: "hello".encode("ascii-8bit"))
|
||||
owner.reload
|
||||
|
|
Loading…
Reference in a new issue