From 4a4c1d87150ae787ad024021c088c53691ae4a08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Fri, 1 Dec 2023 16:15:13 +0100 Subject: [PATCH] chore: cleanup --- .../has_many_associations_using_interleaved_test.rb | 4 ++++ acceptance/cases/models/interleave_test.rb | 4 ++++ .../cases/transactions/optimistic_locking_test.rb | 4 ++++ .../spanner/database_statements.rb | 13 ++++++++++--- .../connection_adapters/spanner_adapter.rb | 5 +---- lib/activerecord_spanner_adapter/base.rb | 2 ++ .../information_schema.rb | 2 +- .../model_helper.rb | 2 +- 8 files changed, 27 insertions(+), 9 deletions(-) diff --git a/acceptance/cases/interleaved_associations/has_many_associations_using_interleaved_test.rb b/acceptance/cases/interleaved_associations/has_many_associations_using_interleaved_test.rb index e4181fc4..d3fde224 100644 --- a/acceptance/cases/interleaved_associations/has_many_associations_using_interleaved_test.rb +++ b/acceptance/cases/interleaved_associations/has_many_associations_using_interleaved_test.rb @@ -6,6 +6,10 @@ # frozen_string_literal: true +# ActiveRecord 7.1 introduced native support for composite primary keys. +# This deprecates the https://github.com/composite-primary-keys/composite_primary_keys gem that was previously used in +# this library to support composite primary keys, which again are needed for interleaved tables. These tests use the +# third-party composite primary key gem and are therefore not executed for Rails 7.1 and higher. return if ActiveRecord::gem_version >= Gem::Version.create('7.1.0') require "test_helper" diff --git a/acceptance/cases/models/interleave_test.rb b/acceptance/cases/models/interleave_test.rb index a1cdd095..506501b7 100644 --- a/acceptance/cases/models/interleave_test.rb +++ b/acceptance/cases/models/interleave_test.rb @@ -6,6 +6,10 @@ # frozen_string_literal: true +# ActiveRecord 7.1 introduced native support for composite primary keys. +# This deprecates the https://github.com/composite-primary-keys/composite_primary_keys gem that was previously used in +# this library to support composite primary keys, which again are needed for interleaved tables. These tests use the +# third-party composite primary key gem and are therefore not executed for Rails 7.1 and higher. return if ActiveRecord::gem_version >= Gem::Version.create('7.1.0') require "test_helper" diff --git a/acceptance/cases/transactions/optimistic_locking_test.rb b/acceptance/cases/transactions/optimistic_locking_test.rb index 7b3689e6..98317280 100644 --- a/acceptance/cases/transactions/optimistic_locking_test.rb +++ b/acceptance/cases/transactions/optimistic_locking_test.rb @@ -6,6 +6,10 @@ # frozen_string_literal: true +# ActiveRecord 7.1 introduced native support for composite primary keys. +# This deprecates the https://github.com/composite-primary-keys/composite_primary_keys gem that was previously used in +# this library to support composite primary keys, which again are needed for interleaved tables. These tests use the +# third-party composite primary key gem and are therefore not executed for Rails 7.1 and higher. return if ActiveRecord::gem_version >= Gem::Version.create('7.1.0') require "test_helper" diff --git a/lib/active_record/connection_adapters/spanner/database_statements.rb b/lib/active_record/connection_adapters/spanner/database_statements.rb index 5fcc08ec..9872fbd4 100644 --- a/lib/active_record/connection_adapters/spanner/database_statements.rb +++ b/lib/active_record/connection_adapters/spanner/database_statements.rb @@ -6,6 +6,8 @@ # frozen_string_literal: true +require "active_record/gem_version" + module ActiveRecord module ConnectionAdapters module Spanner @@ -72,10 +74,12 @@ def internal_execute sql, name = "SQL", binds = [], end end - if ActiveRecord::gem_version >= VERSION_7_1_0 # rubocop:disable Style/ColonMethodCall + # The method signatures for executing queries and DML statements changed between Rails 7.0 and 7.1. + + if ActiveRecord.gem_version >= VERSION_7_1_0 def sql_for_insert sql, _pk, binds, returning if supports_insert_returning? - # TODO: Add primary key to returning columns when supporting sequences. + # TODO: Add primary key to returning columns when support for bit-reversed sequences has been added. returning_columns = returning if returning_columns&.any? @@ -92,7 +96,7 @@ def sql_for_insert sql, _pk, binds, returning def query sql, name = nil exec_query sql, name end - else + else # ActiveRecord.gem_version < VERSION_7_1_0 def query sql, name = nil exec_query sql, name end @@ -256,6 +260,7 @@ def to_types_and_params binds type = ActiveRecord::Type::Spanner::SpannerActiveRecordConverter .convert_active_model_type_to_spanner(bind.type) elsif bind.class == Symbol + # This ensures that for example :environment is sent as the string 'environment' to Cloud Spanner. type = :STRING end [ @@ -267,8 +272,10 @@ def to_types_and_params binds type = if bind.respond_to? :type bind.type elsif bind.class == Symbol + # This ensures that for example :environment is sent as the string 'environment' to Cloud Spanner. :STRING else + # The Cloud Spanner default type is INT64 if no other type is known. ActiveModel::Type::Integer end bind_value = bind.respond_to?(:value) ? bind.value : bind diff --git a/lib/active_record/connection_adapters/spanner_adapter.rb b/lib/active_record/connection_adapters/spanner_adapter.rb index 8782892d..b8e9b140 100644 --- a/lib/active_record/connection_adapters/spanner_adapter.rb +++ b/lib/active_record/connection_adapters/spanner_adapter.rb @@ -111,10 +111,6 @@ def reset! end alias reconnect! reset! - def schema_cache - super - end - def spanner_schema_cache @spanner_schema_cache ||= SpannerSchemaCache.new self end @@ -265,6 +261,7 @@ def type_map include TypeMapBuilder end + # Overwrite the standard log method to be able to translate exceptions. def log sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil, *args super rescue ActiveRecord::StatementInvalid diff --git a/lib/activerecord_spanner_adapter/base.rb b/lib/activerecord_spanner_adapter/base.rb index b341edf8..663a5b03 100644 --- a/lib/activerecord_spanner_adapter/base.rb +++ b/lib/activerecord_spanner_adapter/base.rb @@ -69,6 +69,8 @@ def self._insert_record values, returning = [] end def self._convert_primary_key primary_key_value + # Rails 7.1 and higher supports composite primary keys, and therefore require the provider to return an array + # instead of a single value in all cases. return primary_key_value if ActiveRecord.gem_version < VERSION_7_1 [primary_key_value] end diff --git a/lib/activerecord_spanner_adapter/information_schema.rb b/lib/activerecord_spanner_adapter/information_schema.rb index 1946b4eb..dc78b9fe 100644 --- a/lib/activerecord_spanner_adapter/information_schema.rb +++ b/lib/activerecord_spanner_adapter/information_schema.rb @@ -89,7 +89,7 @@ def _create_column table_name, row, primary_keys, column_options default = row["COLUMN_DEFAULT"] default_function = row["GENERATION_EXPRESSION"] - if /\w+\(.*\)/.match?(default) + if default && default.length < 200 && /\w+\(.*\)/.match?(default) default_function ||= default default = nil end diff --git a/test/activerecord_spanner_interleaved_table/model_helper.rb b/test/activerecord_spanner_interleaved_table/model_helper.rb index 30e7bac6..320edc5c 100644 --- a/test/activerecord_spanner_interleaved_table/model_helper.rb +++ b/test/activerecord_spanner_interleaved_table/model_helper.rb @@ -206,7 +206,7 @@ def self.register_commit_timestamps_result spanner_mock_server, table_name, colu Google::Protobuf::Value.new(string_value: "allow_commit_timestamp"), Google::Protobuf::Value.new(string_value: "BOOL"), Google::Protobuf::Value.new(string_value: "TRUE"), - ) + ) end result_set.rows.push row spanner_mock_server.put_statement_result option_sql, StatementResult.new(result_set)