Skip to content

Commit 6fe490b

Browse files
committed
Wrap connection with rescue to properly raise NoDatabaseError. Fix formatting
1 parent 5e54a22 commit 6fe490b

22 files changed

+65
-18
lines changed

lib/active_record/connection_adapters/cockroachdb_adapter.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ def cockroachdb_connection(config)
4141
conn_params,
4242
config
4343
)
44+
# This rescue flow appears in new_client, but it is needed here as well
45+
# since Cockroach will sometimes not raise until a query is made.
46+
rescue ActiveRecord::StatementInvalid => error
47+
if conn_params && conn_params[:dbname] && error.cause.message.include?(conn_params[:dbname])
48+
raise ActiveRecord::NoDatabaseError
49+
else
50+
raise ActiveRecord::ConnectionNotEstablished, error.message
51+
end
4452
end
4553
end
4654
end
@@ -234,6 +242,9 @@ def initialize_type_map(m = type_map)
234242
precision = extract_precision(sql_type)
235243
scale = extract_scale(sql_type)
236244

245+
# TODO(#178) this should never use DecimalWithoutScale since scale
246+
# is assumed to be 0 if it is not explicitly defined.
247+
#
237248
# If fmod is -1, that means that precision is defined but not
238249
# scale, or neither is defined.
239250
if fmod && fmod == -1

test/cases/adapters/postgresql/interval_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,4 @@ def test_schema_dump_with_default_value
139139
assert_match %r{t\.interval "default_term", default: "3 years 0 mons 0 days 0 hours 0 minutes 0 seconds"}, output
140140
end
141141
end
142-
end
142+
end

test/cases/adapters/postgresql/postgresql_adapter_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ 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: "cockroach" }
19+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
20+
config = db_config.configuration_hash.dup
2021
config[:database] = "non_extant_database"
2122
assert_not ActiveRecord::ConnectionAdapters::CockroachDBAdapter.database_exists?(config),
2223
"expected database #{config[:database]} to not exist"

test/cases/adapters/postgresql/uuid_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ def test_uuid_change_case_does_not_mark_dirty
5353
assert_not_predicate model, :changed?
5454
end
5555
end
56-
end
56+
end

test/cases/helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,4 @@ def in_time_zone(zone)
228228
Time.zone = old_zone
229229
ActiveRecord::Base.time_zone_aware_attributes = old_tz
230230
end
231-
end
231+
end

test/cases/marshal_serialization_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ def marshal_fixture_path(file_name)
4242
)
4343
end
4444
end
45-
end
45+
end

test/cases/relation/merging_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ def test_merge_doesnt_duplicate_same_clauses
3535
end
3636
end
3737
end
38-
end
38+
end

test/cases/relation_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ def test_relation_with_annotation_filters_sql_comment_delimiters
2323
end
2424
end
2525
end
26-
end
26+
end

test/cases/sanitize_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ def bind(statement, *vars)
2222
end
2323
end
2424
end
25-
end
25+
end

test/cases/strict_loading_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require "cases/helper"
4+
require "models/developer"
5+
require "models/computer"
6+
require "models/mentor"
7+
require "models/project"
8+
require "models/ship"
9+
require "models/strict_zine"
10+
require "models/interest"
11+
12+
module CockroachDB
13+
class StrictLoadingFixturesTest < ActiveRecord::TestCase
14+
# This test is identical to the ActiveRecord version except
15+
# that transactional tests are disabled, so create_fixtures
16+
# will work.
17+
self.use_transactional_tests = false
18+
19+
fixtures :strict_zines
20+
21+
test "strict loading violations are ignored on fixtures" do
22+
ActiveRecord::FixtureSet.reset_cache
23+
create_fixtures("strict_zines")
24+
25+
assert_nothing_raised do
26+
strict_zines(:going_out).interests.to_a
27+
end
28+
29+
assert_raises(ActiveRecord::StrictLoadingViolationError) do
30+
StrictZine.first.interests.to_a
31+
end
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)