Skip to content

Commit d860d3c

Browse files
committed
Fix database_exists test
1 parent 229afab commit d860d3c

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

lib/active_record/connection_adapters/cockroachdb_adapter.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@ def cockroachdb_connection(config)
2424
valid_conn_param_keys = PG::Connection.conndefaults_hash.keys + [:sslmode, :application_name]
2525
conn_params.slice!(*valid_conn_param_keys)
2626

27-
# The postgres drivers don't allow the creation of an unconnected
28-
# PG::Connection object, so just pass a nil connection object for the
29-
# time being.
3027
conn = PG.connect(conn_params)
3128
ConnectionAdapters::CockroachDBAdapter.new(conn, logger, conn_params, config)
32-
rescue ::PG::Error => error
29+
rescue ::PG::Error, ActiveRecord::ActiveRecordError => error
3330
if error.message.include?("does not exist")
3431
raise ActiveRecord::NoDatabaseError
3532
else
@@ -154,6 +151,12 @@ def initialize(connection, logger, conn_params, config)
154151
@crdb_version = version_num
155152
end
156153

154+
def self.database_exists?(config)
155+
!!ActiveRecord::Base.cockroachdb_connection(config)
156+
rescue ActiveRecord::NoDatabaseError
157+
false
158+
end
159+
157160
private
158161

159162
def initialize_type_map(m = type_map)

test/cases/adapters/postgresql/postgresql_adapter_test.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ def setup
1616
end
1717

1818
def test_database_exists_returns_false_when_the_database_does_not_exist
19-
config = { database: "non_extant_database", adapter: "cockroachdb" }
20-
assert_not ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.database_exists?(config),
21-
"expected database #{config[:database]} to not exist"
19+
config = ActiveRecord::Base.configurations["arunit"]
20+
bad_config = config.dup
21+
bad_config[:database] = "non_extant_database"
22+
assert_not ActiveRecord::ConnectionAdapters::CockroachDBAdapter.database_exists?(bad_config),
23+
"expected database #{bad_config[:database]} to not exist"
2224
end
2325
end
2426
end
25-
end
27+
end

test/excludes/ActiveRecord/ConnectionAdapters/PostgreSQLAdapterTest.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
exclude :test_exec_insert_default_values_quoted_schema_with_returning_disabled_and_no_sequence_name_given, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
1515
exclude :test_expression_index, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
1616
exclude :test_serial_sequence, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
17+
exclude :test_database_exists_returns_false_when_the_database_does_not_exist, "Test is reimplemented to use cockroachdb adapter"

test/excludes/ActiveRecord/PostgreSQLAdapterTest.rb

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)