Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support multiple databases #283

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

## Ongoing

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

## 7.0.2 - 2023-05-23

Expand Down
1 change: 1 addition & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require "active_record/connection_adapters/cockroachdb/database_tasks"
begin
retried = false
ActiveRecord::Base.establish_connection(
#Alternative version: "cockroachdb://root@localhost:26257/ar_crdb_console"
adapter: "cockroachdb",
host: "localhost",
port: 26257,
Expand Down
6 changes: 1 addition & 5 deletions lib/active_record/connection_adapters/cockroachdb/column.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ActiveRecord
module ConnectionAdapters
module CockroachDB
module PostgreSQLColumnMonkeyPatch
class Column < PostgreSQLColumn
# most functions taken from activerecord-postgis-adapter spatial_column
# https://github.com/rgeo/activerecord-postgis-adapter/blob/master/lib/active_record/connection_adapters/postgis/spatial_column.rb
def initialize(name, default, sql_type_metadata = nil, null = true,
Expand Down Expand Up @@ -93,9 +93,5 @@ def to_type_name(geometric_type)
end
end
end

class PostgreSQLColumn
prepend CockroachDB::PostgreSQLColumnMonkeyPatch
end
end
end
6 changes: 6 additions & 0 deletions lib/active_record/connection_adapters/cockroachdb/quoting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ module Quoting
# converting to WKB, so this does it automatically.
def quote(value)
if value.is_a?(Numeric)
# NOTE: The fact that integers are quoted is important and helps
# mitigate a potential vulnerability.
#
# See
# - https://nvd.nist.gov/vuln/detail/CVE-2022-44566
# - https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/280#discussion_r1288692977
"'#{quote_string(value.to_s)}'"
elsif RGeo::Feature::Geometry.check_type(value)
"'#{RGeo::WKRep::WKBGenerator.new(hex_format: true, type_format: :ewkb, emit_ewkb_srid: true).generate(value)}'"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def new_column_from_field(table_name, field)
# {:dimension=>2, :has_m=>false, :has_z=>false, :name=>"latlon", :srid=>0, :type=>"GEOMETRY"}
spatial = spatial_column_info(table_name).get(column_name, type_metadata.sql_type)

PostgreSQL::Column.new(
CockroachDB::Column.new(
column_name,
default_value,
type_metadata,
Expand Down
7 changes: 5 additions & 2 deletions lib/active_record/connection_adapters/cockroachdb/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ class << self
# Return :postgresql instead of :cockroachdb for current_adapter_name so
# we can continue using the ActiveRecord::Types defined in
# PostgreSQLAdapter.
def adapter_name_from(_model)
:postgresql
def adapter_name_from(model)
name = model.connection_db_config.adapter.to_sym
return :postgresql if name == :cockroachdb

name
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions test/cases/connection_adapters/type_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "cases/helper_cockroachdb"
require "models/account"

module CockroachDB
module ConnectionAdapters
class TypeTest < ActiveRecord::TestCase
fixtures :accounts
class SqliteModel < ActiveRecord::Base
establish_connection(
adapter: "sqlite3",
database: "tmp/some"
)
end
def test_type_can_be_used_with_various_db
assert_equal(
:postgresql,
ActiveRecord::Type.adapter_name_from(Account)
)
assert_equal(
:sqlite3,
ActiveRecord::Type.adapter_name_from(SqliteModel)
)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
exclude :test_do_not_raise_when_int_is_not_wider_than_64bit, "replaced for correct quoting"
exclude :test_do_not_raise_when_raise_int_wider_than_64bit_is_false, "replaced for correct quoting"
exclude :test_raise_when_int_is_wider_than_64bit, "since integers are stringified there is not 64bit limit"
comment = "This adapter quotes integers as string, as CRDB is capable of " \
"implicitely converting, and checking for out of bound errors. " \
"See quoting.rb for more information."
exclude :test_do_not_raise_when_int_is_not_wider_than_64bit, comment
exclude :test_do_not_raise_when_raise_int_wider_than_64bit_is_false, comment
exclude :test_raise_when_int_is_wider_than_64bit, comment
Loading