activerecord-cockroachdb-adapter 6.1.10 → 7.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/docker.yml +57 -0
- data/.gitignore +7 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +9 -1
- data/CONTRIBUTING.md +33 -16
- data/Gemfile +7 -5
- data/README.md +4 -2
- data/activerecord-cockroachdb-adapter.gemspec +3 -1
- data/build/Dockerfile +2 -5
- data/build/local-test.sh +0 -6
- data/build/teamcity-test.sh +16 -18
- data/docker.sh +1 -1
- data/lib/active_record/connection_adapters/cockroachdb/database_statements.rb +2 -1
- data/lib/active_record/connection_adapters/cockroachdb/oid/date_time.rb +21 -0
- data/lib/active_record/connection_adapters/cockroachdb/quoting.rb +1 -3
- data/lib/active_record/connection_adapters/cockroachdb/referential_integrity.rb +23 -9
- data/lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb +27 -4
- data/lib/active_record/connection_adapters/cockroachdb_adapter.rb +24 -3
- data/lib/active_record/migration/cockroachdb/compatibility.rb +28 -0
- data/lib/version.rb +1 -1
- metadata +23 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 829d7c6d923f6d2949dfcca1c946be01286b79f643987696b0d271c696e3c55d
|
4
|
+
data.tar.gz: 558689db88c43aaa1fe4d10216796ee2754d9b206cc56feb846b0ca2e05e745c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b3ad08b9912080c5f324ef1b244e5f9d1ed22cd1e63f3c9ee9623399e5c92e12000681b0988e533a19e4e403325a13a419e534499c2d3fd6316b36525be57d9
|
7
|
+
data.tar.gz: c77f0016087842bdcfd7d8cbb0989b687653dcc4e6ce3e5c618b2fe2f1a44e1c995b84836235ba497f7623713987180796c1d07ae639f16dd4be6af747c1e9a4
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# This workflow is based off of the example at
|
2
|
+
# https://github.com/docker/metadata-action
|
3
|
+
#
|
4
|
+
# Multi-platform configuration from
|
5
|
+
# https://github.com/docker/build-push-action/blob/master/docs/advanced/multi-platform.md
|
6
|
+
#
|
7
|
+
# Caching from
|
8
|
+
# https://github.com/docker/build-push-action/blob/master/docs/advanced/cache.md
|
9
|
+
name: Docker
|
10
|
+
permissions:
|
11
|
+
contents: read
|
12
|
+
on:
|
13
|
+
push:
|
14
|
+
branches: [ master ]
|
15
|
+
paths:
|
16
|
+
- 'build/Dockerfile'
|
17
|
+
# Only build, but don't push, on a PR if it touches the Dockerfile,
|
18
|
+
# since this takes a while to execute.
|
19
|
+
pull_request:
|
20
|
+
paths:
|
21
|
+
- 'build/Dockerfile'
|
22
|
+
jobs:
|
23
|
+
docker:
|
24
|
+
runs-on: ubuntu-latest
|
25
|
+
steps:
|
26
|
+
- name: Checkout
|
27
|
+
uses: actions/checkout@v3
|
28
|
+
- name: Set up QEMU
|
29
|
+
uses: docker/setup-qemu-action@v2
|
30
|
+
- name: Set up Docker Buildx
|
31
|
+
uses: docker/setup-buildx-action@v2
|
32
|
+
- name: Docker meta
|
33
|
+
id: meta
|
34
|
+
uses: docker/metadata-action@v4
|
35
|
+
with:
|
36
|
+
images: cockroachdb/activerecord_test_container
|
37
|
+
labels: |
|
38
|
+
org.opencontainers.image.title=Active Record CockroachDB Adapter Test Image
|
39
|
+
org.opencontainers.image.vendor=Cockroach Labs Inc.
|
40
|
+
org.opencontainers.image.description=Environment for running tests
|
41
|
+
- name: Login to DockerHub
|
42
|
+
if: github.event_name != 'pull_request'
|
43
|
+
uses: docker/login-action@v2
|
44
|
+
with:
|
45
|
+
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
46
|
+
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
47
|
+
- name: Build and push
|
48
|
+
uses: docker/build-push-action@v4
|
49
|
+
with:
|
50
|
+
context: .
|
51
|
+
file: build/Dockerfile
|
52
|
+
platforms: linux/amd64,linux/arm64
|
53
|
+
push: ${{ github.event_name != 'pull_request' }}
|
54
|
+
tags: ${{ steps.meta.outputs.tags }}
|
55
|
+
labels: ${{ steps.meta.outputs.labels }}
|
56
|
+
cache-from: type=gha
|
57
|
+
cache-to: type=gha,mode=max
|
data/.gitignore
CHANGED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.2.1
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 7.0.1 - 2023-03-24
|
4
|
+
|
5
|
+
- Reconnect on retryable connection errors.
|
6
|
+
|
7
|
+
## 7.0.0 - 2022-06-02
|
8
|
+
|
9
|
+
- Add support for Active Record 7.0.3
|
10
|
+
|
3
11
|
## 6.1.10 - 2022-05-06
|
4
12
|
|
5
13
|
- Disable supports_expression_index regardless of CockroachDB version until
|
@@ -46,7 +54,7 @@
|
|
46
54
|
|
47
55
|
## 6.1.1 - 2021-05-14
|
48
56
|
|
49
|
-
- Fix a bug where starting the driver can result in a NoDatabaseError.
|
57
|
+
- Fix a bug where starting the driver can result in a NoDatabaseError.
|
50
58
|
|
51
59
|
## 6.1.0 - 2021-04-26
|
52
60
|
|
data/CONTRIBUTING.md
CHANGED
@@ -25,22 +25,39 @@ CREATE DATABASE activerecord_unittest2;
|
|
25
25
|
```
|
26
26
|
|
27
27
|
It is best to have a Ruby environment manager installed, such as
|
28
|
-
[
|
29
|
-
If you are using
|
30
|
-
|
28
|
+
[rbenv](https://github.com/rbenv/rbenv), as Rails has varying Ruby version
|
29
|
+
requirements. If you are using rbenv, you then install and use the required
|
30
|
+
Ruby version.
|
31
31
|
|
32
32
|
(Alternatively, one can use `./docker.sh build/teamcity-test.sh` to run
|
33
|
-
tests
|
33
|
+
tests similarly to TeamCity. The database is destroyed between each
|
34
34
|
test file.)
|
35
35
|
|
36
|
+
Install rbenv with ruby-build on MacOS:
|
36
37
|
|
37
38
|
```bash
|
38
|
-
|
39
|
-
|
40
|
-
rvm use 2.2.5
|
39
|
+
brew install rbenv ruby-build
|
40
|
+
echo 'eval "$(rbenv init - zsh)"' >> ~/.profile
|
41
41
|
```
|
42
42
|
|
43
|
-
|
43
|
+
Install rbenv with ruby-build on Ubuntu:
|
44
|
+
|
45
|
+
```bash
|
46
|
+
sudo apt-get install rbenv
|
47
|
+
echo 'eval "$(rbenv init - zsh)"' >> ~/.profile
|
48
|
+
|
49
|
+
git clone https://github.com/sstephenson/ruby-build.git ~/ruby-build
|
50
|
+
sh ~/ruby-build/install.sh
|
51
|
+
```
|
52
|
+
|
53
|
+
Use rbenv to install version of Ruby specified by `.ruby-version`.
|
54
|
+
|
55
|
+
```bash
|
56
|
+
rbenv install
|
57
|
+
rbenv rehash
|
58
|
+
```
|
59
|
+
|
60
|
+
Using [bundler](http://bundler.io/), install the dependencies of Rails.
|
44
61
|
|
45
62
|
```bash
|
46
63
|
bundle install
|
@@ -52,13 +69,13 @@ Then, to run the full test suite with an active CockroachDB instance:
|
|
52
69
|
bundle exec rake test
|
53
70
|
```
|
54
71
|
|
55
|
-
To run specific ActiveRecord tests, set
|
72
|
+
To run specific ActiveRecord tests, set environment variable `TEST_FILES_AR`. For example, to run ActiveRecord tests `test/cases/associations_test.rb` and `test/cases/ar_schema_test.rb.rb`
|
56
73
|
|
57
74
|
```bash
|
58
75
|
TEST_FILES_AR="test/cases/associations_test.rb,test/cases/ar_schema_test.rb" bundle exec rake test
|
59
76
|
```
|
60
77
|
|
61
|
-
To run specific CockroachDB Adapter tests, set
|
78
|
+
To run specific CockroachDB Adapter tests, set environment variable `TEST_FILES`. For example, to run CockroachDB Adpater tests `test/cases/adapter_test.rb` and `test/cases/associations/left_outer_join_association_test.rb`
|
62
79
|
|
63
80
|
```bash
|
64
81
|
TEST_FILES="test/cases/adapter_test.rb,test/cases/associations/left_outer_join_association_test.rb" bundle exec rake test
|
@@ -104,7 +121,7 @@ And the `activerecord_unittest` database will use the `RESTORE` command to load
|
|
104
121
|
## Support past Rails versions
|
105
122
|
|
106
123
|
Currently, only a beta version of Rails is tested. This means that the
|
107
|
-
adapter has been modified in to
|
124
|
+
adapter has been modified in to accommodate unreleased changes. In order
|
108
125
|
to run the tests for Rails 5.1 or 4.2, the test changes will need to be
|
109
126
|
cherry-picked back. Conflicts are mostly only expected for tests that
|
110
127
|
have not yet been added.
|
@@ -129,7 +146,7 @@ against different versions of CockroachDB.
|
|
129
146
|
|
130
147
|
As CockroachDB improves, so do the features that can be supported in
|
131
148
|
ActiveRecord. Many of them are gated by conditions the
|
132
|
-
CockroachDBAdapter has
|
149
|
+
CockroachDBAdapter has overridden. As these features are completed, these
|
133
150
|
gates should be toggled. Something that would help this process would be
|
134
151
|
linking those issues back to this adapter so that part of the feature
|
135
152
|
completing includes updating the adapter.
|
@@ -231,10 +248,10 @@ need to be cleaned up.
|
|
231
248
|
# Notes for the non-Rubyer
|
232
249
|
|
233
250
|
rvm is an environment manager that lets you manage and swap between
|
234
|
-
multiple
|
251
|
+
multiple versions of Ruby and their dependencies.
|
235
252
|
|
236
|
-
bundle is
|
237
|
-
`<project>.gemspec`) to manage and load
|
253
|
+
bundle is dependency manager that uses a projects `Gemfile` (and often
|
254
|
+
`<project>.gemspec`) to manage and load dependencies and their required
|
238
255
|
versions. When using projects commands are prefixed with
|
239
|
-
`bundle exec ...`. Bundle will ensure that all
|
256
|
+
`bundle exec ...`. Bundle will ensure that all dependencies are fetched
|
240
257
|
and used.
|
data/Gemfile
CHANGED
@@ -47,17 +47,19 @@ else
|
|
47
47
|
)
|
48
48
|
end
|
49
49
|
|
50
|
-
# Get Rails from source
|
50
|
+
# Get Rails from source because the gem doesn't include tests
|
51
51
|
version = ENV['RAILS_VERSION'] || get_version_from_gemspec
|
52
52
|
gem 'rails', git: "https://github.com/rails/rails.git", tag: "v#{version}"
|
53
53
|
end
|
54
54
|
|
55
55
|
group :development do
|
56
56
|
gem "byebug"
|
57
|
-
gem "minitest-excludes"
|
57
|
+
gem "minitest-excludes", "~> 2.0.1"
|
58
58
|
|
59
59
|
# Gems used by the ActiveRecord test suite
|
60
|
-
gem "bcrypt"
|
61
|
-
gem "mocha"
|
62
|
-
gem "sqlite3"
|
60
|
+
gem "bcrypt", "~> 3.1.18"
|
61
|
+
gem "mocha", "~> 1.14.0"
|
62
|
+
gem "sqlite3", "~> 1.4.4"
|
63
|
+
|
64
|
+
gem "minitest", "~> 5.15.0"
|
63
65
|
end
|
data/README.md
CHANGED
@@ -1,19 +1,21 @@
|
|
1
1
|
# ActiveRecord CockroachDB Adapter
|
2
2
|
|
3
|
-
CockroachDB adapter for ActiveRecord 5 and
|
3
|
+
CockroachDB adapter for ActiveRecord 5, 6, and 7. This is a lightweight extension of the PostgreSQL adapter that establishes compatibility with [CockroachDB](https://github.com/cockroachdb/cockroach).
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
Add this line to your project's Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem 'activerecord-cockroachdb-adapter', '~>
|
10
|
+
gem 'activerecord-cockroachdb-adapter', '~> 7.0.0'
|
11
11
|
```
|
12
12
|
|
13
13
|
If you're using Rails 5.2, use the `5.2.x` versions of this gem.
|
14
14
|
|
15
15
|
If you're using Rails 6.0, use the `6.0.x` versions of this gem.
|
16
16
|
|
17
|
+
If you're using Rails 7.0, use the `7.0.x` versions of this gem.
|
18
|
+
|
17
19
|
In `database.yml`, use the following adapter setting:
|
18
20
|
|
19
21
|
```
|
@@ -17,10 +17,12 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.description = "Allows the use of CockroachDB as a backend for ActiveRecord and Rails apps."
|
18
18
|
spec.homepage = "https://github.com/cockroachdb/activerecord-cockroachdb-adapter"
|
19
19
|
|
20
|
-
spec.add_dependency "activerecord", "~>
|
20
|
+
spec.add_dependency "activerecord", "~> 7.0.3"
|
21
21
|
spec.add_dependency "pg", "~> 1.2"
|
22
22
|
spec.add_dependency "rgeo-activerecord", "~> 7.0.0"
|
23
23
|
|
24
|
+
spec.add_development_dependency "benchmark-ips", "~> 2.9.1"
|
25
|
+
|
24
26
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
25
27
|
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
26
28
|
if spec.respond_to?(:metadata)
|
data/build/Dockerfile
CHANGED
@@ -8,10 +8,7 @@ RUN apt-get --allow-releaseinfo-change update -y && apt-get install -y \
|
|
8
8
|
libxslt-dev \
|
9
9
|
libxml2-dev \
|
10
10
|
libsqlite3-dev \
|
11
|
+
rbenv \
|
11
12
|
&& rm -rf /var/lib/apt/lists/*
|
12
13
|
|
13
|
-
|
14
|
-
RUN gem install bundle rake
|
15
|
-
|
16
|
-
# Add global Gem binaries to the path.
|
17
|
-
ENV PATH /usr/local/lib/ruby/gems/2.4.0::$PATH
|
14
|
+
RUN rm -rf ~/ruby-build; git clone https://github.com/sstephenson/ruby-build.git ~/ruby-build; sh ~/ruby-build/install.sh
|
data/build/local-test.sh
CHANGED
@@ -11,7 +11,6 @@ rm -f "$urlfile"
|
|
11
11
|
# canceled on an agent.
|
12
12
|
rm -rf $HOME/tmp/rails &
|
13
13
|
# Start CockroachDB.
|
14
|
-
cockroach quit --insecure || true
|
15
14
|
cockroach start --insecure --host=localhost --listening-url-file="$urlfile" --store=path=$HOME/tmp/rails &
|
16
15
|
trap "echo 'Exit routine: Killing CockroachDB.' && kill -9 $! &> /dev/null" EXIT
|
17
16
|
for i in {0..3}
|
@@ -31,8 +30,3 @@ echo "Rebuilding database"
|
|
31
30
|
(cd rails/activerecord && bundle exec rake db:cockroachdb:rebuild)
|
32
31
|
echo "Starting tests"
|
33
32
|
(cd rails/activerecord && bundle exec rake test:cockroachdb TESTFILES=$1)
|
34
|
-
|
35
|
-
# Attempt a clean shutdown for good measure. We'll force-kill in the atexit
|
36
|
-
# handler if this fails.
|
37
|
-
cockroach quit --insecure
|
38
|
-
trap - EXIT
|
data/build/teamcity-test.sh
CHANGED
@@ -2,9 +2,17 @@
|
|
2
2
|
|
3
3
|
set -euox pipefail
|
4
4
|
|
5
|
+
eval "$(rbenv init -)"
|
6
|
+
|
7
|
+
rbenv install --skip-existing --verbose
|
8
|
+
|
9
|
+
rbenv rehash
|
10
|
+
|
11
|
+
ruby -v
|
12
|
+
|
5
13
|
# Download CockroachDB
|
6
|
-
VERSION=
|
7
|
-
wget -qO- https://binaries.cockroachdb.com/cockroach-$VERSION.linux-amd64.tgz | tar
|
14
|
+
VERSION=v22.2.6
|
15
|
+
wget -qO- https://binaries.cockroachdb.com/cockroach-$VERSION.linux-amd64.tgz | tar xvz
|
8
16
|
readonly COCKROACH=./cockroach-$VERSION.linux-amd64/cockroach
|
9
17
|
|
10
18
|
# Make sure cockroach can be found on the path. This is required for the
|
@@ -16,9 +24,6 @@ run_cockroach() {
|
|
16
24
|
# Start a CockroachDB server, wait for it to become ready, and arrange
|
17
25
|
# for it to be force-killed when the script exits.
|
18
26
|
rm -f "$urlfile"
|
19
|
-
# Clean out a past CockroachDB instance. This will clean out leftovers
|
20
|
-
# from the build agent, and also between CockroachDB runs.
|
21
|
-
cockroach quit --insecure || true
|
22
27
|
rm -rf cockroach-data
|
23
28
|
# Start CockroachDB.
|
24
29
|
cockroach start-single-node --max-sql-memory=25% --cache=25% --insecure --host=localhost --spatial-libs=./cockroach-$VERSION.linux-amd64/lib --listening-url-file="$urlfile" >/dev/null 2>&1 &
|
@@ -35,6 +40,8 @@ run_cockroach() {
|
|
35
40
|
cockroach sql --insecure -e 'CREATE DATABASE activerecord_unittest2;'
|
36
41
|
cockroach sql --insecure -e 'SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false;'
|
37
42
|
cockroach sql --insecure -e 'SET CLUSTER SETTING sql.stats.histogram_collection.enabled = false;'
|
43
|
+
cockroach sql --insecure -e "SET CLUSTER SETTING jobs.retention_time = '180s';"
|
44
|
+
cockroach sql --insecure -e "SET CLUSTER SETTING sql.defaults.experimental_alter_column_type.enabled = 'true'"
|
38
45
|
|
39
46
|
cockroach sql --insecure -e "ALTER RANGE default CONFIGURE ZONE USING num_replicas = 1, gc.ttlseconds = 30;"
|
40
47
|
cockroach sql --insecure -e "ALTER TABLE system.public.jobs CONFIGURE ZONE USING num_replicas = 1, gc.ttlseconds = 30;"
|
@@ -46,21 +53,17 @@ run_cockroach() {
|
|
46
53
|
cockroach sql --insecure -e "SET CLUSTER SETTING kv.raft_log.disable_synchronization_unsafe = 'true'"
|
47
54
|
cockroach sql --insecure -e "SET CLUSTER SETTING jobs.registry.interval.cancel = '180s';"
|
48
55
|
cockroach sql --insecure -e "SET CLUSTER SETTING jobs.registry.interval.gc = '30s';"
|
49
|
-
cockroach sql --insecure -e "SET CLUSTER SETTING jobs.retention_time = '15s';"
|
50
|
-
cockroach sql --insecure -e "SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false;"
|
51
56
|
cockroach sql --insecure -e "SET CLUSTER SETTING kv.range_split.by_load_merge_delay = '5s';"
|
52
57
|
|
53
|
-
# Enable when we test with v22.1.
|
54
|
-
# cockroach sql --insecure -e "SET CLUSTER SETTING sql.catalog.unsafe_skip_system_config_trigger.enabled = true;"
|
55
|
-
|
56
58
|
# Enable experimental features.
|
57
59
|
cockroach sql --insecure -e "SET CLUSTER SETTING sql.defaults.experimental_temporary_tables.enabled = 'true';"
|
58
|
-
cockroach sql --insecure -e "SET CLUSTER SETTING sql.defaults.datestyle.enabled = true"
|
59
|
-
cockroach sql --insecure -e "SET CLUSTER SETTING sql.defaults.intervalstyle.enabled = true;"
|
60
60
|
}
|
61
61
|
|
62
|
+
gem env
|
63
|
+
|
62
64
|
# Install ruby dependencies.
|
63
|
-
gem install bundler:2.
|
65
|
+
gem install bundler:2.4.9 rake:13.0.6
|
66
|
+
|
64
67
|
bundle install
|
65
68
|
|
66
69
|
run_cockroach
|
@@ -73,11 +76,6 @@ else
|
|
73
76
|
HAS_FAILED=0
|
74
77
|
fi
|
75
78
|
|
76
|
-
# Attempt a clean shutdown for good measure. We'll force-kill in the
|
77
|
-
# exit trap if this script fails.
|
78
|
-
cockroach quit --insecure
|
79
|
-
trap - EXIT
|
80
|
-
|
81
79
|
if [ $HAS_FAILED -eq 1 ]; then
|
82
80
|
exit 1
|
83
81
|
fi
|
data/docker.sh
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module CockroachDB
|
6
|
+
module OID
|
7
|
+
module DateTime
|
8
|
+
protected
|
9
|
+
|
10
|
+
# override
|
11
|
+
# Uses CockroachDBAdapter instead of PostgreSQLAdapter
|
12
|
+
def real_type_unless_aliased(real_type)
|
13
|
+
ActiveRecord::ConnectionAdapters::CockroachDBAdapter.datetime_type == real_type ? :datetime : real_type
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
PostgreSQL::OID::DateTime.prepend(DateTime)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -2,8 +2,6 @@ module ActiveRecord
|
|
2
2
|
module ConnectionAdapters
|
3
3
|
module CockroachDB
|
4
4
|
module Quoting
|
5
|
-
private
|
6
|
-
|
7
5
|
# CockroachDB does not allow inserting integer values into string
|
8
6
|
# columns, but ActiveRecord expects this to work. CockroachDB will
|
9
7
|
# however allow inserting string values into integer columns. It will
|
@@ -19,7 +17,7 @@ module ActiveRecord
|
|
19
17
|
# (https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary)
|
20
18
|
# but when creating objects, using RGeo features is more convenient than
|
21
19
|
# converting to WKB, so this does it automatically.
|
22
|
-
def
|
20
|
+
def quote(value)
|
23
21
|
if value.is_a?(Numeric)
|
24
22
|
"'#{quote_string(value.to_s)}'"
|
25
23
|
elsif RGeo::Feature::Geometry.check_type(value)
|
@@ -20,17 +20,31 @@ module ActiveRecord
|
|
20
20
|
|
21
21
|
yield
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
23
|
+
# Prefixes and suffixes are added in add_foreign_key
|
24
|
+
# in AR7+ so we need to temporarily disable them here,
|
25
|
+
# otherwise prefixes/suffixes will be erroneously added.
|
26
|
+
old_prefix = ActiveRecord::Base.table_name_prefix
|
27
|
+
old_suffix = ActiveRecord::Base.table_name_suffix
|
28
|
+
|
29
|
+
ActiveRecord::Base.table_name_prefix = ""
|
30
|
+
ActiveRecord::Base.table_name_suffix = ""
|
31
|
+
|
32
|
+
begin
|
33
|
+
foreign_keys.each do |foreign_key|
|
34
|
+
begin
|
35
|
+
add_foreign_key(foreign_key.from_table, foreign_key.to_table, **foreign_key.options)
|
36
|
+
rescue ActiveRecord::StatementInvalid => error
|
37
|
+
if error.cause.class == PG::DuplicateObject
|
38
|
+
# This error is safe to ignore because the yielded caller
|
39
|
+
# already re-added the foreign key constraint.
|
40
|
+
else
|
41
|
+
raise error
|
42
|
+
end
|
32
43
|
end
|
33
44
|
end
|
45
|
+
ensure
|
46
|
+
ActiveRecord::Base.table_name_prefix = old_prefix
|
47
|
+
ActiveRecord::Base.table_name_suffix = old_suffix
|
34
48
|
end
|
35
49
|
end
|
36
50
|
end
|
@@ -10,21 +10,44 @@ module ActiveRecord
|
|
10
10
|
# exceeded.
|
11
11
|
def within_new_transaction(isolation: nil, joinable: true, attempts: 0)
|
12
12
|
super(isolation: isolation, joinable: joinable)
|
13
|
+
rescue ActiveRecord::ConnectionNotEstablished => error
|
14
|
+
raise unless retryable? error
|
15
|
+
raise if attempts >= @connection.max_transaction_retries
|
16
|
+
|
17
|
+
sleep_rand_seconds(attempts)
|
18
|
+
|
19
|
+
unless @connection.active?
|
20
|
+
warn "connection isn't active, reconnecting"
|
21
|
+
@connection.reconnect!
|
22
|
+
end
|
23
|
+
|
24
|
+
within_new_transaction(isolation: isolation, joinable: joinable, attempts: attempts + 1) { yield }
|
13
25
|
rescue ActiveRecord::StatementInvalid => error
|
14
26
|
raise unless retryable? error
|
15
27
|
raise if attempts >= @connection.max_transaction_retries
|
16
28
|
|
17
|
-
attempts
|
18
|
-
|
19
|
-
|
20
|
-
within_new_transaction(isolation: isolation, joinable: joinable, attempts: attempts) { yield }
|
29
|
+
sleep_rand_seconds(attempts)
|
30
|
+
|
31
|
+
within_new_transaction(isolation: isolation, joinable: joinable, attempts: attempts + 1) { yield }
|
21
32
|
end
|
22
33
|
|
23
34
|
def retryable?(error)
|
35
|
+
return true if serialization_error?(error)
|
24
36
|
return true if error.is_a? ActiveRecord::SerializationFailure
|
25
37
|
return retryable? error.cause if error.cause
|
26
38
|
false
|
27
39
|
end
|
40
|
+
|
41
|
+
def serialization_error?(error)
|
42
|
+
errors = [error]
|
43
|
+
errors << error.cause if error.cause
|
44
|
+
errors.any? {|e| e.is_a? PG::TRSerializationFailure }
|
45
|
+
end
|
46
|
+
|
47
|
+
def sleep_rand_seconds(attempts)
|
48
|
+
sleep_seconds = (2 ** attempts + rand) / 10
|
49
|
+
sleep(sleep_seconds)
|
50
|
+
end
|
28
51
|
end
|
29
52
|
end
|
30
53
|
|
@@ -18,7 +18,9 @@ require "active_record/connection_adapters/cockroachdb/setup"
|
|
18
18
|
require "active_record/connection_adapters/cockroachdb/oid/type_map_initializer"
|
19
19
|
require "active_record/connection_adapters/cockroachdb/oid/spatial"
|
20
20
|
require "active_record/connection_adapters/cockroachdb/oid/interval"
|
21
|
+
require "active_record/connection_adapters/cockroachdb/oid/date_time"
|
21
22
|
require "active_record/connection_adapters/cockroachdb/arel_tosql"
|
23
|
+
require_relative "../migration/cockroachdb/compatibility"
|
22
24
|
require_relative "../../version"
|
23
25
|
|
24
26
|
# Run to ignore spatial tables that will break schemna dumper.
|
@@ -215,6 +217,10 @@ module ActiveRecord
|
|
215
217
|
false
|
216
218
|
end
|
217
219
|
|
220
|
+
def supports_deferrable_constraints?
|
221
|
+
false
|
222
|
+
end
|
223
|
+
|
218
224
|
# This is hardcoded to 63 (as previously was in ActiveRecord 5.0) to aid in
|
219
225
|
# migration from PostgreSQL to CockroachDB. In practice, this limitation
|
220
226
|
# is arbitrary since CockroachDB supports index name lengths and table alias
|
@@ -293,8 +299,20 @@ module ActiveRecord
|
|
293
299
|
false
|
294
300
|
end
|
295
301
|
|
296
|
-
|
302
|
+
# override
|
303
|
+
# The PostgreSQLAdapter uses syntax for an anonymous function
|
304
|
+
# (DO $$) that CockroachDB does not support.
|
305
|
+
#
|
306
|
+
# Given a name and an array of values, creates an enum type.
|
307
|
+
def create_enum(name, values)
|
308
|
+
sql_values = values.map { |s| "'#{s}'" }.join(", ")
|
309
|
+
query = <<~SQL
|
310
|
+
CREATE TYPE IF NOT EXISTS \"#{name}\" AS ENUM (#{sql_values});
|
311
|
+
SQL
|
312
|
+
exec_query(query)
|
313
|
+
end
|
297
314
|
|
315
|
+
class << self
|
298
316
|
def initialize_type_map(m = type_map)
|
299
317
|
%w(
|
300
318
|
geography
|
@@ -341,10 +359,13 @@ module ActiveRecord
|
|
341
359
|
# lookups on PG
|
342
360
|
Type::DecimalWithoutScale.new(precision: precision)
|
343
361
|
else
|
344
|
-
OID::Decimal.new(precision: precision, scale: scale)
|
362
|
+
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Decimal.new(precision: precision, scale: scale)
|
345
363
|
end
|
346
364
|
end
|
347
365
|
end
|
366
|
+
end
|
367
|
+
|
368
|
+
private
|
348
369
|
|
349
370
|
# Configures the encoding, verbosity, schema search path, and time zone of the connection.
|
350
371
|
# This is called by #connect and should not be called manually.
|
@@ -366,7 +387,7 @@ module ActiveRecord
|
|
366
387
|
# If using Active Record's time zone support configure the connection to return
|
367
388
|
# TIMESTAMP WITH ZONE types in UTC.
|
368
389
|
unless variables["timezone"]
|
369
|
-
if ActiveRecord
|
390
|
+
if ActiveRecord.default_timezone == :utc
|
370
391
|
variables["timezone"] = "UTC"
|
371
392
|
elsif @local_tz
|
372
393
|
variables["timezone"] = @local_tz
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "active_record/migration"
|
2
|
+
require "active_record/migration/compatibility"
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
class Migration
|
6
|
+
module CockroachDB
|
7
|
+
module Compatibility
|
8
|
+
module V7_0Patch
|
9
|
+
# Override. Use "CockroachDB" instead of "PostgreSQL"
|
10
|
+
def compatible_timestamp_type(type, connection)
|
11
|
+
if connection.adapter_name == "CockroachDB"
|
12
|
+
# For Rails <= 6.1, :datetime was aliased to :timestamp
|
13
|
+
# See: https://github.com/rails/rails/blob/v6.1.3.2/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L108
|
14
|
+
# From Rails 7 onwards, you can define what :datetime resolves to (the default is still :timestamp)
|
15
|
+
# See `ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.datetime_type`
|
16
|
+
type.to_sym == :datetime ? :timestamp : type
|
17
|
+
else
|
18
|
+
type
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
prepend_mod = ActiveRecord::Migration::CockroachDB::Compatibility::V7_0Patch
|
28
|
+
ActiveRecord::Migration::Compatibility::V6_1::PostgreSQLCompat.singleton_class.prepend(prepend_mod)
|
data/lib/version.rb
CHANGED
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:
|
4
|
+
version: 7.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cockroach Labs
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 7.0.3
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 7.0.3
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: pg
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 7.0.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: benchmark-ips
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.9.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.9.1
|
55
69
|
description: Allows the use of CockroachDB as a backend for ActiveRecord and Rails
|
56
70
|
apps.
|
57
71
|
email:
|
@@ -60,8 +74,10 @@ executables: []
|
|
60
74
|
extensions: []
|
61
75
|
extra_rdoc_files: []
|
62
76
|
files:
|
77
|
+
- ".github/workflows/docker.yml"
|
63
78
|
- ".gitignore"
|
64
79
|
- ".gitmodules"
|
80
|
+
- ".ruby-version"
|
65
81
|
- CHANGELOG.md
|
66
82
|
- CONTRIBUTING.md
|
67
83
|
- Gemfile
|
@@ -82,6 +98,7 @@ files:
|
|
82
98
|
- lib/active_record/connection_adapters/cockroachdb/column_methods.rb
|
83
99
|
- lib/active_record/connection_adapters/cockroachdb/database_statements.rb
|
84
100
|
- lib/active_record/connection_adapters/cockroachdb/database_tasks.rb
|
101
|
+
- lib/active_record/connection_adapters/cockroachdb/oid/date_time.rb
|
85
102
|
- lib/active_record/connection_adapters/cockroachdb/oid/interval.rb
|
86
103
|
- lib/active_record/connection_adapters/cockroachdb/oid/spatial.rb
|
87
104
|
- lib/active_record/connection_adapters/cockroachdb/oid/type_map_initializer.rb
|
@@ -96,6 +113,7 @@ files:
|
|
96
113
|
- lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb
|
97
114
|
- lib/active_record/connection_adapters/cockroachdb/type.rb
|
98
115
|
- lib/active_record/connection_adapters/cockroachdb_adapter.rb
|
116
|
+
- lib/active_record/migration/cockroachdb/compatibility.rb
|
99
117
|
- lib/activerecord-cockroachdb-adapter.rb
|
100
118
|
- lib/version.rb
|
101
119
|
homepage: https://github.com/cockroachdb/activerecord-cockroachdb-adapter
|
@@ -118,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
136
|
- !ruby/object:Gem::Version
|
119
137
|
version: '0'
|
120
138
|
requirements: []
|
121
|
-
rubygems_version: 3.
|
139
|
+
rubygems_version: 3.4.9
|
122
140
|
signing_key:
|
123
141
|
specification_version: 4
|
124
142
|
summary: CockroachDB adapter for ActiveRecord.
|