activerecord-cockroachdb-adapter 6.0.0beta3 → 6.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +39 -0
- data/README.md +7 -0
- data/activerecord-cockroachdb-adapter.gemspec +1 -1
- data/build/config.teamcity.yml +3 -0
- data/lib/active_record/connection_adapters/cockroachdb/oid/type_map_initializer.rb +26 -0
- data/lib/active_record/connection_adapters/cockroachdb_adapter.rb +129 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 872261a41d4e766a58bd7ea377e28d7dd9507031cfb88288843c5ef0484b8773
|
4
|
+
data.tar.gz: 9c56d0bb58eccc334d3ab5657226bb397076c3bf363a22b339fda0e92a5a99bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ff0ba0686a110f4f23621ca68474763122933e4611111fa4d5d87a67cf96364e08b09d393b980598424e4507fd2834e98b43c40aab8945551c52317c60f91fe
|
7
|
+
data.tar.gz: 7369deb96883410baca55b5c2464efc38f36ce291eb4a20eebac6bfe6a1b0304aa0f2e4174baa5608f099695c0742d27898ba9e8a16e07a4e5b68663f9d8d5db
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## 6.0.2 - 2021-05-20
|
4
|
+
|
5
|
+
- Fix a bug where starting the driver can result in a ConnectionNotEstablished error.
|
6
|
+
|
7
|
+
## 6.0.1 - 2021-05-14
|
8
|
+
|
9
|
+
- Fix a bug where starting the driver can result in a NoDatabaseError.
|
10
|
+
|
11
|
+
## 6.0.0 - 2021-04-26
|
12
|
+
|
13
|
+
- Add a telemetry query on start-up. This helps the Cockroach Labs team
|
14
|
+
prioritize support for the adapter. It can be disabled by setting the
|
15
|
+
`disable_cockroachdb_telemetry` configuration option to false.
|
16
|
+
|
17
|
+
## 6.0.0-beta.5 - 2021-04-02
|
18
|
+
|
19
|
+
- Added a configuration option named `use_follower_reads_for_type_introspection`.
|
20
|
+
If true, it improves the speed of type introspection by allowing potentially stale
|
21
|
+
type metadata to be read. Defaults to false.
|
22
|
+
|
23
|
+
## 6.0.0-beta.4 - 2021-03-06
|
24
|
+
|
25
|
+
- Improved connection performance by refactoring an introspection
|
26
|
+
that loads types.
|
27
|
+
- Changed version numbers to semver.
|
28
|
+
|
29
|
+
## 6.0.0beta3
|
30
|
+
|
31
|
+
- Added support for spatial features.
|
32
|
+
|
33
|
+
## 6.0.0beta2
|
34
|
+
|
35
|
+
- Updated transaction retry logic to work with Rails 6.
|
36
|
+
|
37
|
+
## 6.0.0beta1
|
38
|
+
|
39
|
+
- Initial support for Rails 6.
|
data/README.md
CHANGED
@@ -22,6 +22,13 @@ development:
|
|
22
22
|
user: <username>
|
23
23
|
```
|
24
24
|
|
25
|
+
## Configuration
|
26
|
+
|
27
|
+
In addition to the standard adapter settings, CockroachDB also supports the following:
|
28
|
+
|
29
|
+
- `use_follower_reads_for_type_introspection`: Use follower reads on queries to the `pg_type` catalog when set to `true`. This helps to speed up initialization by reading historical data, but may not find recently created user-defined types.
|
30
|
+
- `disable_cockroachdb_telemetry`: Determines if a telemetry call is made to the database when the connection pool is initialized. Setting this to `true` will prevent the call from being made.
|
31
|
+
|
25
32
|
## Working with Spatial Data
|
26
33
|
|
27
34
|
The adapter uses [RGeo](https://github.com/rgeo/rgeo) and [RGeo-ActiveRecord](https://github.com/rgeo/rgeo-activerecord) to represent geometric and geographic data as Ruby objects and easily interface them with the adapter. The following is a brief introduction to RGeo and tips to help setup your spatial application. More documentation about RGeo can be found in the [YARD Docs](https://rubydoc.info/github/rgeo/rgeo) and [wiki](https://github.com/rgeo/rgeo/wiki).
|
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "activerecord-cockroachdb-adapter"
|
7
|
-
spec.version = "6.0.
|
7
|
+
spec.version = "6.0.2"
|
8
8
|
spec.licenses = ["Apache-2.0"]
|
9
9
|
spec.authors = ["Cockroach Labs"]
|
10
10
|
spec.email = ["cockroach-db@googlegroups.com"]
|
data/build/config.teamcity.yml
CHANGED
@@ -11,6 +11,7 @@ connections:
|
|
11
11
|
user: root
|
12
12
|
requiressl: disable
|
13
13
|
min_messages: warning
|
14
|
+
disable_cockroachdb_telemetry: true
|
14
15
|
arunit_without_prepared_statements:
|
15
16
|
database: activerecord_unittest
|
16
17
|
host: localhost
|
@@ -19,6 +20,7 @@ connections:
|
|
19
20
|
requiressl: disable
|
20
21
|
min_messages: warning
|
21
22
|
prepared_statements: false
|
23
|
+
disable_cockroachdb_telemetry: true
|
22
24
|
arunit2:
|
23
25
|
database: activerecord_unittest2
|
24
26
|
host: localhost
|
@@ -26,3 +28,4 @@ connections:
|
|
26
28
|
user: root
|
27
29
|
requiressl: disable
|
28
30
|
min_messages: warning
|
31
|
+
disable_cockroachdb_telemetry: true
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters
|
3
|
+
module CockroachDB
|
4
|
+
module OID
|
5
|
+
module TypeMapInitializer
|
6
|
+
# override
|
7
|
+
# Replaces the query with a faster version that doesn't rely on the
|
8
|
+
# use of 'array_in(cstring,oid,integer)'::regprocedure.
|
9
|
+
def query_conditions_for_initial_load
|
10
|
+
known_type_names = @store.keys.map { |n| "'#{n}'" }
|
11
|
+
known_type_types = %w('r' 'e' 'd')
|
12
|
+
<<~SQL % [known_type_names.join(", "), known_type_types.join(", ")]
|
13
|
+
WHERE
|
14
|
+
t.typname IN (%s)
|
15
|
+
OR t.typtype IN (%s)
|
16
|
+
OR (t.typarray = 0 AND t.typcategory='A')
|
17
|
+
OR t.typelem != 0
|
18
|
+
SQL
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
PostgreSQL::OID::TypeMapInitializer.prepend(TypeMapInitializer)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -13,6 +13,7 @@ require "active_record/connection_adapters/cockroachdb/attribute_methods"
|
|
13
13
|
require "active_record/connection_adapters/cockroachdb/column"
|
14
14
|
require "active_record/connection_adapters/cockroachdb/spatial_column_info"
|
15
15
|
require "active_record/connection_adapters/cockroachdb/setup"
|
16
|
+
require "active_record/connection_adapters/cockroachdb/oid/type_map_initializer"
|
16
17
|
require "active_record/connection_adapters/cockroachdb/oid/spatial"
|
17
18
|
require "active_record/connection_adapters/cockroachdb/arel_tosql"
|
18
19
|
|
@@ -50,6 +51,35 @@ end
|
|
50
51
|
|
51
52
|
module ActiveRecord
|
52
53
|
module ConnectionAdapters
|
54
|
+
module CockroachDBConnectionPool
|
55
|
+
def initialize(spec)
|
56
|
+
super(spec)
|
57
|
+
disable_telemetry = spec.config[:disable_cockroachdb_telemetry]
|
58
|
+
adapter = spec.config[:adapter]
|
59
|
+
return if disable_telemetry || adapter != "cockroachdb"
|
60
|
+
|
61
|
+
|
62
|
+
begin
|
63
|
+
with_connection do |conn|
|
64
|
+
if conn.active?
|
65
|
+
begin
|
66
|
+
query = "SELECT crdb_internal.increment_feature_counter('ActiveRecord %d.%d')"
|
67
|
+
conn.execute(query % [ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR])
|
68
|
+
rescue ActiveRecord::StatementInvalid
|
69
|
+
# The increment_feature_counter built-in is not supported on this
|
70
|
+
# CockroachDB version. Ignore.
|
71
|
+
rescue StandardError => e
|
72
|
+
conn.logger.warn "Unexpected error when incrementing feature counter: #{e}"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
rescue StandardError
|
77
|
+
# Prevent failures on db creation and parallel testing.
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
ConnectionPool.prepend(CockroachDBConnectionPool)
|
82
|
+
|
53
83
|
class CockroachDBAdapter < PostgreSQLAdapter
|
54
84
|
ADAPTER_NAME = "CockroachDB".freeze
|
55
85
|
DEFAULT_PRIMARY_KEY = "rowid"
|
@@ -217,6 +247,10 @@ module ActiveRecord
|
|
217
247
|
@crdb_version >= 202
|
218
248
|
end
|
219
249
|
|
250
|
+
def supports_partitioned_indexes?
|
251
|
+
false
|
252
|
+
end
|
253
|
+
|
220
254
|
# This is hardcoded to 63 (as previously was in ActiveRecord 5.0) to aid in
|
221
255
|
# migration from PostgreSQL to CockroachDB. In practice, this limitation
|
222
256
|
# is arbitrary since CockroachDB supports index name lengths and table alias
|
@@ -234,6 +268,7 @@ module ActiveRecord
|
|
234
268
|
|
235
269
|
def initialize(connection, logger, conn_params, config)
|
236
270
|
super(connection, logger, conn_params, config)
|
271
|
+
|
237
272
|
crdb_version_string = query_value("SHOW crdb_version")
|
238
273
|
if crdb_version_string.include? "v1."
|
239
274
|
version_num = 1
|
@@ -398,6 +433,100 @@ module ActiveRecord
|
|
398
433
|
return "{}"
|
399
434
|
end
|
400
435
|
|
436
|
+
# override
|
437
|
+
# This method loads info about data types from the database to
|
438
|
+
# populate the TypeMap.
|
439
|
+
#
|
440
|
+
# Currently, querying from the pg_type catalog can be slow due to geo-partitioning
|
441
|
+
# so this modified query uses AS OF SYSTEM TIME '-10s' to read historical data.
|
442
|
+
def load_additional_types(oids = nil)
|
443
|
+
if @config[:use_follower_reads_for_type_introspection]
|
444
|
+
initializer = OID::TypeMapInitializer.new(type_map)
|
445
|
+
|
446
|
+
query = <<~SQL
|
447
|
+
SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, r.rngsubtype, t.typtype, t.typbasetype
|
448
|
+
FROM pg_type as t
|
449
|
+
LEFT JOIN pg_range as r ON oid = rngtypid AS OF SYSTEM TIME '-10s'
|
450
|
+
SQL
|
451
|
+
|
452
|
+
if oids
|
453
|
+
query += "WHERE t.oid IN (%s)" % oids.join(", ")
|
454
|
+
else
|
455
|
+
query += initializer.query_conditions_for_initial_load
|
456
|
+
end
|
457
|
+
|
458
|
+
execute_and_clear(query, "SCHEMA", []) do |records|
|
459
|
+
initializer.run(records)
|
460
|
+
end
|
461
|
+
else
|
462
|
+
super
|
463
|
+
end
|
464
|
+
rescue ActiveRecord::StatementInvalid => e
|
465
|
+
raise e unless e.cause.is_a? PG::InvalidCatalogName
|
466
|
+
# use original if database is younger than 10s
|
467
|
+
super
|
468
|
+
end
|
469
|
+
|
470
|
+
# override
|
471
|
+
# This method maps data types to their proper decoder.
|
472
|
+
#
|
473
|
+
# Currently, querying from the pg_type catalog can be slow due to geo-partitioning
|
474
|
+
# so this modified query uses AS OF SYSTEM TIME '-10s' to read historical data.
|
475
|
+
def add_pg_decoders
|
476
|
+
if @config[:use_follower_reads_for_type_introspection]
|
477
|
+
@default_timezone = nil
|
478
|
+
@timestamp_decoder = nil
|
479
|
+
|
480
|
+
coders_by_name = {
|
481
|
+
"int2" => PG::TextDecoder::Integer,
|
482
|
+
"int4" => PG::TextDecoder::Integer,
|
483
|
+
"int8" => PG::TextDecoder::Integer,
|
484
|
+
"oid" => PG::TextDecoder::Integer,
|
485
|
+
"float4" => PG::TextDecoder::Float,
|
486
|
+
"float8" => PG::TextDecoder::Float,
|
487
|
+
"numeric" => PG::TextDecoder::Numeric,
|
488
|
+
"bool" => PG::TextDecoder::Boolean,
|
489
|
+
"timestamp" => PG::TextDecoder::TimestampUtc,
|
490
|
+
"timestamptz" => PG::TextDecoder::TimestampWithTimeZone,
|
491
|
+
}
|
492
|
+
|
493
|
+
known_coder_types = coders_by_name.keys.map { |n| quote(n) }
|
494
|
+
query = <<~SQL % known_coder_types.join(", ")
|
495
|
+
SELECT t.oid, t.typname
|
496
|
+
FROM pg_type as t AS OF SYSTEM TIME '-10s'
|
497
|
+
WHERE t.typname IN (%s)
|
498
|
+
SQL
|
499
|
+
|
500
|
+
coders = execute_and_clear(query, "SCHEMA", []) do |result|
|
501
|
+
result
|
502
|
+
.map { |row| construct_coder(row, coders_by_name[row["typname"]]) }
|
503
|
+
.compact
|
504
|
+
end
|
505
|
+
|
506
|
+
map = PG::TypeMapByOid.new
|
507
|
+
coders.each { |coder| map.add_coder(coder) }
|
508
|
+
@connection.type_map_for_results = map
|
509
|
+
|
510
|
+
@type_map_for_results = PG::TypeMapByOid.new
|
511
|
+
@type_map_for_results.default_type_map = map
|
512
|
+
@type_map_for_results.add_coder(PG::TextDecoder::Bytea.new(oid: 17, name: "bytea"))
|
513
|
+
|
514
|
+
# extract timestamp decoder for use in update_typemap_for_default_timezone
|
515
|
+
@timestamp_decoder = coders.find { |coder| coder.name == "timestamp" }
|
516
|
+
update_typemap_for_default_timezone
|
517
|
+
else
|
518
|
+
super
|
519
|
+
end
|
520
|
+
rescue ActiveRecord::StatementInvalid => e
|
521
|
+
raise e unless e.cause.is_a? PG::InvalidCatalogName
|
522
|
+
# use original if database is younger than 10s
|
523
|
+
super
|
524
|
+
end
|
525
|
+
|
526
|
+
def arel_visitor
|
527
|
+
Arel::Visitors::CockroachDB.new(self)
|
528
|
+
end
|
529
|
+
|
401
530
|
# end private
|
402
531
|
end
|
403
532
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-cockroachdb-adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 6.0.
|
4
|
+
version: 6.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cockroach Labs
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -62,6 +62,7 @@ extra_rdoc_files: []
|
|
62
62
|
files:
|
63
63
|
- ".gitignore"
|
64
64
|
- ".gitmodules"
|
65
|
+
- CHANGELOG.md
|
65
66
|
- CONTRIBUTING.md
|
66
67
|
- Gemfile
|
67
68
|
- LICENSE
|
@@ -82,6 +83,7 @@ files:
|
|
82
83
|
- lib/active_record/connection_adapters/cockroachdb/database_statements.rb
|
83
84
|
- lib/active_record/connection_adapters/cockroachdb/database_tasks.rb
|
84
85
|
- lib/active_record/connection_adapters/cockroachdb/oid/spatial.rb
|
86
|
+
- lib/active_record/connection_adapters/cockroachdb/oid/type_map_initializer.rb
|
85
87
|
- lib/active_record/connection_adapters/cockroachdb/quoting.rb
|
86
88
|
- lib/active_record/connection_adapters/cockroachdb/referential_integrity.rb
|
87
89
|
- lib/active_record/connection_adapters/cockroachdb/schema_statements.rb
|
@@ -108,9 +110,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
108
110
|
version: '0'
|
109
111
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
112
|
requirements:
|
111
|
-
- - "
|
113
|
+
- - ">="
|
112
114
|
- !ruby/object:Gem::Version
|
113
|
-
version:
|
115
|
+
version: '0'
|
114
116
|
requirements: []
|
115
117
|
rubygems_version: 3.0.3
|
116
118
|
signing_key:
|