Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed Dec 1, 2023
1 parent f8fa852 commit 4a4c1d8
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions acceptance/cases/models/interleave_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions acceptance/cases/transactions/optimistic_locking_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

# frozen_string_literal: true

require "active_record/gem_version"

module ActiveRecord
module ConnectionAdapters
module Spanner
Expand Down Expand Up @@ -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?
Expand All @@ -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
Expand Down Expand Up @@ -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
[
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions lib/active_record/connection_adapters/spanner_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/activerecord_spanner_adapter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/activerecord_spanner_adapter/information_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4a4c1d8

Please sign in to comment.