Skip to content

Commit 63e8e86

Browse files
authored
bump activerecord to 8.0.0 (#356)
Cleanup useless code, update overriden methods. Add a `#supports_native_partitioning? => false` method Add timezone to quoted dates to comply with postgresql way of parsing `timestamp with timezone`. Add `#show_create` test. Add test tool for excluding tests by name from transactional tests. Update CI. Fixes #354
1 parent 7064053 commit 63e8e86

25 files changed

+172
-57
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ jobs:
4040
fail-fast: false
4141
matrix:
4242
# https://www.cockroachlabs.com/docs/releases/release-support-policy
43-
crdb: [v23.2, v24.1, v24.2]
44-
ruby: ["3.3"]
43+
crdb: [v23.2, v24.1, v24.3]
44+
ruby: ["3.4"]
4545
name: Test (crdb=${{ matrix.crdb }} ruby=${{ matrix.ruby }})
4646
steps:
4747
- name: Set Up Actions

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ group :development, :test do
6363

6464
# Gems used by the ActiveRecord test suite
6565
gem "bcrypt", "~> 3.1"
66-
gem "sqlite3", "~> 1.4"
66+
gem "sqlite3", "~> 2.1"
6767

6868
gem "minitest", "~> 5.15"
6969
end

activerecord-cockroachdb-adapter.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Gem::Specification.new do |spec|
1414
spec.description = "Allows the use of CockroachDB as a backend for ActiveRecord and Rails apps."
1515
spec.homepage = "https://github.com/cockroachdb/activerecord-cockroachdb-adapter"
1616

17-
spec.add_dependency "activerecord", "~> 7.2.0"
17+
spec.add_dependency "activerecord", "~> 8.0.0"
1818
spec.add_dependency "pg", "~> 1.5"
19-
spec.add_dependency "rgeo-activerecord", "~> 7.0.0"
19+
spec.add_dependency "rgeo-activerecord", "~> 8.0.0"
2020

2121
spec.add_development_dependency "benchmark-ips", "~> 2.9.1"
2222

lib/active_record/connection_adapters/cockroachdb/database_statements.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ def insert_fixtures_set(fixture_set, tables_to_delete = [])
2424
table_deletes = tables_to_delete.map { |table| "DELETE FROM #{quote_table_name(table)}" }
2525
statements = table_deletes + fixture_inserts
2626

27-
with_multi_statements do
28-
disable_referential_integrity do
29-
execute_batch(statements, "Fixtures Load")
30-
end
27+
disable_referential_integrity do
28+
execute_batch(statements, "Fixtures Load")
3129
end
3230
end
3331
end

lib/active_record/connection_adapters/cockroachdb/quoting.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ def quote(value)
5050
super
5151
end
5252
end
53+
54+
def quoted_date(value)
55+
# CockroachDB differs from PostgreSQL in its representation of
56+
# a `timestamp with timezone`, it does not always include the
57+
# timezone offset (e.g. `+00`), so we need to add it here.
58+
# This is tested by `BasicsTest#test_default_in_local_time`.
59+
super + value.strftime("%z")
60+
end
5361
end
5462
end
5563
end

lib/active_record/connection_adapters/cockroachdb_adapter.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ def supports_nulls_not_distinct?
161161
false
162162
end
163163

164+
# Partitioning is quite different from PostgreSQL, so we don't support it.
165+
# If you need partitioning, you should default to using raw SQL queries.
166+
#
167+
# See https://www.postgresql.org/docs/current/ddl-partitioning.html
168+
# See https://www.cockroachlabs.com/docs/stable/partitioning
169+
def supports_native_partitioning?
170+
false
171+
end
172+
164173
def supports_ddl_transactions?
165174
false
166175
end
@@ -466,9 +475,7 @@ def crdb_column_definitions(table_name)
466475
# That method will not work for CockroachDB because the error
467476
# originates from the "runExecBuilder" function, so we need
468477
# to modify the original to match the CockroachDB behavior.
469-
def is_cached_plan_failure?(e)
470-
pgerror = e.cause
471-
478+
def is_cached_plan_failure?(pgerror)
472479
pgerror.result.result_error_field(PG::PG_DIAG_SQLSTATE) == FEATURE_NOT_SUPPORTED &&
473480
pgerror.result.result_error_field(PG::PG_DIAG_SOURCE_FUNCTION) == "runExecBuilder"
474481
rescue

lib/active_record/relation/query_methods_ext.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def force_index(index_name, direction: nil)
5757
def force_index!(index_name, direction: nil)
5858
return self unless from_clause_is_a_table_name?
5959

60-
index_name = sanitize_sql(index_name.to_s)
60+
index_name = model.sanitize_sql(index_name.to_s)
6161
direction = direction.to_s.upcase
6262
direction = %w[ASC DESC].include?(direction) ? ",#{direction}" : ""
6363

@@ -84,7 +84,7 @@ def index_hint(hint)
8484
def index_hint!(hint)
8585
return self unless from_clause_is_a_table_name?
8686

87-
hint = sanitize_sql(hint.to_s)
87+
hint = model.sanitize_sql(hint.to_s)
8888
@index_hint = hint.to_s
8989
self.from_clause = build_from_clause_with_hints
9090
self
@@ -120,7 +120,7 @@ def build_from_clause_with_hints
120120

121121
table_name =
122122
if from_clause.empty?
123-
quoted_table_name
123+
model.quoted_table_name
124124
else
125125
# Remove previous table hints if any. And spaces.
126126
from_clause.value.partition("@").first.strip

test/cases/helper_cockroachdb.rb

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
require 'bundler'
22
Bundler.setup
33

4-
CRDB_VALIDATE_BUG = "CockcroachDB bug, see https://github.com/cockroachdb/cockroach/blob/dd1e0e0164cb3d5859ea4bb23498863d1eebc0af/pkg/sql/alter_table.go#L458-L467"
4+
module ExcludeMessage
5+
VALIDATE_BUG = "CockcroachDB bug, see https://github.com/cockroachdb/cockroach/blob/dd1e0e0164cb3d5859ea4bb23498863d1eebc0af/pkg/sql/alter_table.go#L458-L467"
6+
NO_HSTORE = "Extension \"hstore\" is not yet supported by CRDB. See https://github.com/cockroachdb/cockroach/issues/41284"
7+
end
8+
59
require "minitest/excludes"
610
require "minitest/github_action_reporter"
711

@@ -17,24 +21,13 @@
1721
# and COCKROACH_SKIP_LOAD_SCHEMA that can
1822
# skip this step
1923
require "support/load_schema_helper"
20-
class NoPGSchemaTestCase < SimpleDelegator
21-
def current_adapter?(...)
22-
false
23-
end
24-
end
2524

2625
module LoadSchemaHelperExt
2726
def load_schema
28-
# TODO: Remove this const_set mess once https://github.com/rails/rails/commit/d5c2ff8345c9d23b7326edb2bbe72b6e86a63140
29-
# is part of a rails release.
30-
old_helper = ActiveRecord::TestCase
31-
ActiveRecord.const_set(:TestCase, NoPGSchemaTestCase.new(ActiveRecord::TestCase))
3227
return if ENV['COCKROACH_LOAD_FROM_TEMPLATE']
3328
return if ENV['COCKROACH_SKIP_LOAD_SCHEMA']
3429

3530
super
36-
ensure
37-
ActiveRecord.const_set(:TestCase, old_helper)
3831
end
3932
end
4033
LoadSchemaHelper.prepend(LoadSchemaHelperExt)
@@ -44,6 +37,11 @@ def load_schema
4437
# Load ActiveRecord test helper
4538
require "cases/helper"
4639

40+
require "support/exclude_from_transactional_tests"
41+
42+
# Allow exclusion of tests by name using #exclude_from_transactional_tests(test_name)
43+
ActiveRecord::TestCase.prepend(ExcludeFromTransactionalTests)
44+
4745
# Load the CockroachDB specific schema. It replaces ActiveRecord's PostgreSQL
4846
# specific schema.
4947
def load_cockroachdb_specific_schema

test/cases/migration/check_constraint_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_validate_check_constraint_by_name
3737
end
3838
end
3939

40-
# CRDB_VALIDATE_BUG
40+
# ExcludeMessage::VALIDATE_BUG
4141
# def test_schema_dumping_with_validate_false
4242
# @connection.add_check_constraint :trades, "quantity > 0", name: "quantity_check", validate: false
4343

test/cases/show_create_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
require "cases/helper_cockroachdb"
4+
require "models/post"
5+
6+
module CockroachDB
7+
class ShowCreateTest < ActiveRecord::TestCase
8+
fixtures :posts
9+
10+
def test_show_create
11+
assert_match(/CREATE TABLE public\.posts/, Post.show_create)
12+
end
13+
end
14+
end

0 commit comments

Comments
 (0)