Skip to content

Commit 0c2678d

Browse files
BuonOmorafiss
authored andcommitted
feat: support datetime precision
This is possible since v23.1.13. We also removed deadcode in the process, and made CopyCat capable of overriding method from the same class without raising. Fixes #307
1 parent 56a89ab commit 0c2678d

File tree

8 files changed

+38
-86
lines changed

8 files changed

+38
-86
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Ongoing
44

5+
- Enable precision support ([#318](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/318))
56
- Support arbitrary max identifier length ([#317](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/317)).
67
- Fix `#schema_names` not to return `crdb_internal` ([#316](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/316))
78

bin/console

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require "active_record"
1212
# structure_load(Post.connection_db_config, "awesome-file.sql")
1313
require "active_record/connection_adapters/cockroachdb/database_tasks"
1414

15-
schema_kind = ENV.fetch("SCHEMA_KIND", "default")
15+
schema_kind = ENV.fetch("SCHEMA_KIND", ENV.fetch("SCHEMA", "default"))
1616

1717
system("cockroach sql --insecure --host=localhost:26257 --execute='drop database if exists ar_crdb_console'",
1818
exception: true)

lib/active_record/connection_adapters/cockroachdb/oid/type_map_initializer.rb

Lines changed: 0 additions & 26 deletions
This file was deleted.

lib/active_record/connection_adapters/cockroachdb_adapter.rb

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
require "active_record/connection_adapters/cockroachdb/column"
1919
require "active_record/connection_adapters/cockroachdb/spatial_column_info"
2020
require "active_record/connection_adapters/cockroachdb/setup"
21-
require "active_record/connection_adapters/cockroachdb/oid/type_map_initializer"
2221
require "active_record/connection_adapters/cockroachdb/oid/spatial"
2322
require "active_record/connection_adapters/cockroachdb/oid/interval"
2423
require "active_record/connection_adapters/cockroachdb/oid/date_time"
@@ -157,18 +156,21 @@ def max_transaction_retries
157156
@max_transaction_retries ||= @config.fetch(:max_transaction_retries, 3)
158157
end
159158

160-
# CockroachDB 20.1 can run queries that work against PostgreSQL 10+.
161-
def postgresql_version
162-
100000
159+
def get_database_version
160+
major, minor, patch = query_value("SHOW crdb_version").match(/v(\d+).(\d+).(\d+)/)[1..].map(&:to_i)
161+
major * 100 * 100 + minor * 100 + patch
163162
end
163+
undef :postgresql_version
164+
alias :cockroachdb_version :database_version
164165

165-
def supports_bulk_alter?
166-
true
166+
def supports_datetime_with_precision?
167+
# https://github.com/cockroachdb/cockroach/pull/111400
168+
database_version >= 23_01_13
167169
end
168170

169-
def supports_json?
170-
# FIXME(joey): Add a version check.
171-
true
171+
def supports_nulls_not_distinct?
172+
# https://github.com/cockroachdb/cockroach/issues/115836
173+
false
172174
end
173175

174176
def supports_ddl_transactions?
@@ -183,10 +185,6 @@ def supports_materialized_views?
183185
false
184186
end
185187

186-
def supports_partial_index?
187-
true
188-
end
189-
190188
def supports_index_include?
191189
false
192190
end
@@ -202,14 +200,6 @@ def supports_expression_index?
202200
false
203201
end
204202

205-
def supports_datetime_with_precision?
206-
false
207-
end
208-
209-
def supports_comments?
210-
true
211-
end
212-
213203
def supports_comments_in_create?
214204
false
215205
end

test/excludes/ActiveRecord/Migration/CompatibilityTest.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
exclude :test_legacy_change_column_with_null_executes_update, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
22
exclude :test_legacy_add_foreign_key_with_deferrable_true, "CRDB does not support DEFERRABLE constraints"
33
exclude :test_disable_extension_on_7_0, "CRDB does not support enabling/disabling extensions."
4-
exclude :test_timestamps_sets_default_precision_on_create_table, "See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/307"
54
# CRDB does not support ALTER COLUMN TYPE inside a transaction
6-
::DefaultPrecisionImplicitTestCases.undef_method(:test_datetime_doesnt_set_precision_on_change_column)
7-
::DefaultPrecisionSixTestCases.undef_method(:test_datetime_sets_precision_6_on_change_column)
85
BaseCompatibilityTest.descendants.each { _1.use_transactional_tests = false }
96

107
exclude :test_add_index_errors_on_too_long_name_7_0, "The max length in CRDB is 128, not 64."
Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,21 @@
1-
exclude :test_precision_is_respected_on_timestamp_columns, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
2-
exclude :test_quoting_non_standard_delimiters, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
3-
exclude :test_attribute_for_inspect_for_array_field, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
4-
exclude :test_strings_with_null_strings, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
5-
exclude :test_attribute_for_inspect_for_array_field_for_large_array, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
6-
exclude :test_column, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
7-
exclude :test_default, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
8-
exclude :test_string_quoting_rules_match_pg_behavior, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
1+
require "support/copy_cat"
2+
3+
# Remove hstore from setup (both the extension and the column).
4+
CopyCat.copy_methods(self, self, :setup) do
5+
def on_send(node)
6+
if node in [:send, nil, :enable_extension!, [:str, "hstore"], *] | # enable_extension!("hstore", ...)
7+
[:send, [:lvar, :t], :hstore, *] # t.hstore(...)
8+
remove(node.location.expression)
9+
end
10+
end
11+
end
12+
913
exclude :test_change_column_default_with_array, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
10-
exclude :test_encoding_arrays_of_utf8_strings, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
11-
exclude :test_array_with_serialized_attributes, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
12-
exclude :test_change_column_cant_make_non_array_column_to_array, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
13-
exclude :test_not_compatible_with_serialize_array, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
14-
exclude :test_strings_with_array_delimiters, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
15-
exclude :test_assigning_non_array_value, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
16-
exclude :test_insert_fixture, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
17-
exclude :test_strings_with_quotes, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
18-
exclude :test_contains_nils, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
19-
exclude :test_datetime_with_timezone_awareness, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
20-
exclude :test_select_with_integers, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
21-
exclude :test_strings_with_commas, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
2214
exclude :test_schema_dump_with_shorthand, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
23-
exclude :test_type_cast_integers, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
24-
exclude :test_rewrite_with_strings, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
25-
exclude :test_select_with_strings, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
2615
exclude :test_change_column_from_non_array_to_array, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
2716
exclude :test_with_multi_dimensional_empty_strings, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
2817
exclude :test_mutate_value_in_array, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
29-
exclude :test_default_strings, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
30-
exclude :test_where_by_attribute_with_array, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
31-
exclude :test_with_empty_strings, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
32-
exclude :test_uniqueness_validation, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
3318
exclude :test_change_column_with_array, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
34-
exclude :test_assigning_valid_pg_array_literal, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
3519
exclude :test_multi_dimensional_with_integers, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
3620
exclude :test_with_arbitrary_whitespace, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
37-
exclude :test_type_cast_array, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
38-
exclude :test_rewrite_with_integers, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
39-
exclude :test_escaping, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
40-
exclude :test_assigning_empty_string, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
4121
exclude :test_multi_dimensional_with_strings, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
42-
exclude :test_mutate_array, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"

test/excludes/SchemaDumperTest.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
exclude :test_schema_dump_interval_type, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
44
exclude :test_do_not_dump_foreign_keys_for_ignored_tables, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
55
exclude :test_schema_dump_includes_bigint_default, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
6-
exclude :test_schema_dump_keeps_large_precision_integer_columns_as_decimal, "Skipping until we can triage further. See https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/48"
76
exclude :test_schema_dump_with_timestamptz_datetime_format, "Re-implementing ourselves because we need CockroachDB specific methods."
87
exclude :test_schema_dump_with_correct_timestamp_types_via_add_column_with_type_as_string, "Re-implementing ourselves because we need CockroachDB specific methods."
98
exclude :test_timestamps_schema_dump_before_rails_7_with_timestamptz_setting, "Re-implementing ourselves because we need CockroachDB specific methods."

test/support/copy_cat.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
require "parser/current"
44

55
module CopyCat
6+
module NoWarnRewrite
7+
def warn(message, category: nil, **kwargs)
8+
if /method redefined; discarding old|previous definition of/.match?(message) &&
9+
caller_locations.any? { _1.path["test/support/copy_cat.rb"] && _1.label["copy_methods"] }
10+
# ignore
11+
else
12+
super
13+
end
14+
end
15+
end
16+
::Warning.singleton_class.prepend NoWarnRewrite
17+
618
extend self
719

820
# Copy methods from The original rails class to our adapter.
@@ -44,7 +56,7 @@ def copy_methods(new_klass, old_klass, *methods, debug: false, &block)
4456
puts "=" * 80
4557
end
4658
location = caller_locations(3, 1).first
47-
new_klass.class_eval(code, location.absolute_path, location.lineno)
59+
new_klass.class_eval(code, location.absolute_path || location.path, location.lineno)
4860
end
4961
end
5062

0 commit comments

Comments
 (0)