Skip to content

Commit 77d8ebe

Browse files
BuonOmorafiss
authored andcommitted
fix: support multiple databases
We used to monkeypatch freely some classes. However, Rails is now supporting multiple databases, and these patches are in the way of this usage. Fixes #251
1 parent 939cc6f commit 77d8ebe

File tree

8 files changed

+50
-14
lines changed

8 files changed

+50
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
## Ongoing
44

5-
- Add support for sql load in rake tasks (#275).
6-
- Add support for sql dump in rake tasks (#273).
7-
- Add support for table optimize hints (#266).
5+
- Fix Multiple Database connections ([#283](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/)).
6+
- Add support for sql load in rake tasks ([#275](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/)).
7+
- Add support for sql dump in rake tasks ([#273](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/)).
8+
- Add support for table optimize hints ([#266](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/)).
89

910
## 7.0.2 - 2023-05-23
1011

bin/console

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require "active_record/connection_adapters/cockroachdb/database_tasks"
1515
begin
1616
retried = false
1717
ActiveRecord::Base.establish_connection(
18+
#Alternative version: "cockroachdb://root@localhost:26257/ar_crdb_console"
1819
adapter: "cockroachdb",
1920
host: "localhost",
2021
port: 26257,

lib/active_record/connection_adapters/cockroachdb/column.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module ActiveRecord
22
module ConnectionAdapters
33
module CockroachDB
4-
module PostgreSQLColumnMonkeyPatch
4+
class Column < PostgreSQLColumn
55
# most functions taken from activerecord-postgis-adapter spatial_column
66
# https://github.com/rgeo/activerecord-postgis-adapter/blob/master/lib/active_record/connection_adapters/postgis/spatial_column.rb
77
def initialize(name, default, sql_type_metadata = nil, null = true,
@@ -93,9 +93,5 @@ def to_type_name(geometric_type)
9393
end
9494
end
9595
end
96-
97-
class PostgreSQLColumn
98-
prepend CockroachDB::PostgreSQLColumnMonkeyPatch
99-
end
10096
end
10197
end

lib/active_record/connection_adapters/cockroachdb/quoting.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ module Quoting
1919
# converting to WKB, so this does it automatically.
2020
def quote(value)
2121
if value.is_a?(Numeric)
22+
# NOTE: The fact that integers are quoted is important and helps
23+
# mitigate a potential vulnerability.
24+
#
25+
# See
26+
# - https://nvd.nist.gov/vuln/detail/CVE-2022-44566
27+
# - https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/280#discussion_r1288692977
2228
"'#{quote_string(value.to_s)}'"
2329
elsif RGeo::Feature::Geometry.check_type(value)
2430
"'#{RGeo::WKRep::WKBGenerator.new(hex_format: true, type_format: :ewkb, emit_ewkb_srid: true).generate(value)}'"

lib/active_record/connection_adapters/cockroachdb/schema_statements.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def new_column_from_field(table_name, field)
9090
# {:dimension=>2, :has_m=>false, :has_z=>false, :name=>"latlon", :srid=>0, :type=>"GEOMETRY"}
9191
spatial = spatial_column_info(table_name).get(column_name, type_metadata.sql_type)
9292

93-
PostgreSQL::Column.new(
93+
CockroachDB::Column.new(
9494
column_name,
9595
default_value,
9696
type_metadata,

lib/active_record/connection_adapters/cockroachdb/type.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ class << self
44
# Return :postgresql instead of :cockroachdb for current_adapter_name so
55
# we can continue using the ActiveRecord::Types defined in
66
# PostgreSQLAdapter.
7-
def adapter_name_from(_model)
8-
:postgresql
7+
def adapter_name_from(model)
8+
name = model.connection_db_config.adapter.to_sym
9+
return :postgresql if name == :cockroachdb
10+
11+
name
912
end
1013
end
1114
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require "cases/helper_cockroachdb"
2+
require "models/account"
3+
4+
module CockroachDB
5+
module ConnectionAdapters
6+
class TypeTest < ActiveRecord::TestCase
7+
fixtures :accounts
8+
class SqliteModel < ActiveRecord::Base
9+
establish_connection(
10+
adapter: "sqlite3",
11+
database: "tmp/some"
12+
)
13+
end
14+
def test_type_can_be_used_with_various_db
15+
assert_equal(
16+
:postgresql,
17+
ActiveRecord::Type.adapter_name_from(Account)
18+
)
19+
assert_equal(
20+
:sqlite3,
21+
ActiveRecord::Type.adapter_name_from(SqliteModel)
22+
)
23+
end
24+
end
25+
end
26+
end
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
exclude :test_do_not_raise_when_int_is_not_wider_than_64bit, "replaced for correct quoting"
2-
exclude :test_do_not_raise_when_raise_int_wider_than_64bit_is_false, "replaced for correct quoting"
3-
exclude :test_raise_when_int_is_wider_than_64bit, "since integers are stringified there is not 64bit limit"
1+
comment = "This adapter quotes integers as string, as CRDB is capable of " \
2+
"implicitely converting, and checking for out of bound errors. " \
3+
"See quoting.rb for more information."
4+
exclude :test_do_not_raise_when_int_is_not_wider_than_64bit, comment
5+
exclude :test_do_not_raise_when_raise_int_wider_than_64bit_is_false, comment
6+
exclude :test_raise_when_int_is_wider_than_64bit, comment

0 commit comments

Comments
 (0)