Skip to content

Commit 0fe2427

Browse files
authored
fix: remove max_identifier_length override (#317)
This is no longer necessary as CRDB now supports `SHOW max_identifier_length`, which is the same as PostgreSQL's adapter implementation. Fixes #260
1 parent 3b9ce51 commit 0fe2427

File tree

6 files changed

+96
-17
lines changed

6 files changed

+96
-17
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
## Ongoing
44

5-
- Add support for [AOST](cockroachlabs.com/docs/stable/as-of-system-time) queries ([#284](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/284))
6-
- Dump schema name in foreign keys ([#289](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/289))
5+
- Support arbitrary max identifier length ([#317](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/317)).
76

87
## 7.1.0 - 2024-01-30
98

109
- Add support for Rails 7.1 ([#300](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/300)).
10+
- Add support for [AOST](cockroachlabs.com/docs/stable/as-of-system-time) queries ([#284](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/284))
11+
- Dump schema name in foreign keys ([#289](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/289))
1112

1213
## 7.0.3 - 2023-08-23
1314

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ group :development, :test do
5656
gem "minitest-excludes", "~> 2.0.1"
5757
gem "minitest-github_action_reporter", github: "BuonOmo/minitest-github_action_reporter", require: "minitest/github_action_reporter_plugin"
5858

59+
# Gems used for tests meta-programming.
60+
gem "parser"
61+
5962
# Gems used by the ActiveRecord test suite
6063
gem "bcrypt", "~> 3.1.18"
6164
gem "mocha", "~> 1.14.0"

lib/active_record/connection_adapters/cockroachdb_adapter.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,21 +232,6 @@ def supports_deferrable_constraints?
232232
false
233233
end
234234

235-
# This is hardcoded to 63 (as previously was in ActiveRecord 5.0) to aid in
236-
# migration from PostgreSQL to CockroachDB. In practice, this limitation
237-
# is arbitrary since CockroachDB supports index name lengths and table alias
238-
# lengths far greater than this value. For the time being though, we match
239-
# the original behavior for PostgreSQL to simplify migrations.
240-
#
241-
# Note that in the migration to ActiveRecord 5.1, this was changed in
242-
# PostgreSQLAdapter to use `SHOW max_identifier_length` (which does not
243-
# exist in CockroachDB). Therefore, we have to redefine this here.
244-
def max_identifier_length
245-
63
246-
end
247-
alias index_name_length max_identifier_length
248-
alias table_alias_length max_identifier_length
249-
250235
# NOTE: This commented bit of code allows to have access to crdb version,
251236
# which can be useful for feature detection. However, we currently don't
252237
# need, hence we avoid the extra queries.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
require "cases/helper_cockroachdb"
4+
require "support/copy_cat"
5+
6+
module ActiveRecord
7+
module CockroachDB
8+
class Migration
9+
class CompatibilityTest < ActiveRecord::Migration::CompatibilityTest
10+
CopyCat.copy_methods(self, ActiveRecord::Migration::CompatibilityTest,
11+
:test_add_index_errors_on_too_long_name_7_0,
12+
:test_create_table_add_index_errors_on_too_long_name_7_0
13+
) do
14+
def on_sym(node)
15+
return unless node.children[0] == :very_long_column_name_to_test_with
16+
17+
insert_after(node.loc.expression, "_and_actually_way_longer_because_cockroach_is_in_the_128_game")
18+
end
19+
end
20+
end
21+
end
22+
end
23+
end

test/excludes/ActiveRecord/Migration/CompatibilityTest.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@
66
::DefaultPrecisionImplicitTestCases.undef_method(:test_datetime_doesnt_set_precision_on_change_column)
77
::DefaultPrecisionSixTestCases.undef_method(:test_datetime_sets_precision_6_on_change_column)
88
BaseCompatibilityTest.descendants.each { _1.use_transactional_tests = false }
9+
10+
exclude :test_add_index_errors_on_too_long_name_7_0, "The max length in CRDB is 128, not 64."
11+
exclude :test_create_table_add_index_errors_on_too_long_name_7_0, "The max length in CRDB is 128, not 64."

test/support/copy_cat.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
require "parser/current"
4+
5+
module CopyCat
6+
extend self
7+
8+
# Copy methods from The original rails class to our adapter.
9+
# While copying, we can rewrite the source code of the method using
10+
# ast. Use `debug: true` to lead you true that process.
11+
def copy_methods(new_klass, old_klass, *methods, debug: false, &block)
12+
methods.each do |met|
13+
file, _ = old_klass.instance_method(met).source_location
14+
ast = find_method(Parser::CurrentRuby.parse_file(file), met)
15+
code =
16+
if block_given?
17+
source = ast.location.expression.source
18+
buffer = Parser::Source::Buffer.new(met, source: source)
19+
# We need to recompute the ast to have correct locations.
20+
ast = Parser::CurrentRuby.parse(source)
21+
22+
if debug
23+
puts "=" * 80
24+
puts "Rewriter doc: https://www.rubydoc.info/gems/parser/3.3.0.5/Parser/TreeRewriter"
25+
puts "Pattern matching doc: https://docs.ruby-lang.org/en/master/syntax/pattern_matching_rdoc.html"
26+
puts
27+
puts "Method: #{met}"
28+
puts
29+
puts "Source:"
30+
puts buffer.source
31+
puts
32+
puts "AST:"
33+
pp ast
34+
puts
35+
end
36+
rewriter_class = Class.new(Parser::TreeRewriter, &block)
37+
rewriter_class.new.rewrite(buffer, ast)
38+
else
39+
ast.location.expression.source
40+
end
41+
if debug and block_given?
42+
puts "Rewritten source:"
43+
puts code
44+
puts "=" * 80
45+
end
46+
location = caller_locations(3, 1).first
47+
new_klass.class_eval(code, location.absolute_path, location.lineno)
48+
end
49+
end
50+
51+
def find_method(ast, method_name)
52+
method_name = method_name.to_sym
53+
to_search = [ast]
54+
while !to_search.empty?
55+
node = to_search.shift
56+
next unless node.is_a?(Parser::AST::Node)
57+
if node in [:def, ^method_name, *]
58+
return node
59+
end
60+
to_search += node.children
61+
end
62+
return nil
63+
end
64+
end

0 commit comments

Comments
 (0)