Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord_spanner_adapter/base.rb

Constant Summary collapse

VERSION_7_1 =
Gem::Version.create "7.1.0"
VERSION_7_2 =
Gem::Version.create "7.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._buffer_record(values, method, returning) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/activerecord_spanner_adapter/base.rb', line 192

def self._buffer_record values, method, returning
  primary_key_value =
    if primary_key.is_a? Array
      _set_composite_primary_key_values primary_key, values
    else
      _set_single_primary_key_value primary_key, values
    end

   = .new self, arel_table
  columns, grpc_values = _create_grpc_values_for_insert , values

  write = Google::Cloud::Spanner::V1::Mutation::Write.new(
    table: arel_table.name,
    columns: columns,
    values: [grpc_values.list_value]
  )
  mutation = Google::Cloud::Spanner::V1::Mutation.new(
    "#{method}": write
  )
  connection.current_spanner_transaction.buffer mutation

  _convert_primary_key primary_key_value, returning
end

._convert_primary_key(primary_key_value, returning) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/activerecord_spanner_adapter/base.rb', line 108

def self._convert_primary_key primary_key_value, returning
  # 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. The order of the values should be equal to the order of the returning
  # columns (or the primary key if no returning columns were specified).
  return primary_key_value if ActiveRecord.gem_version < VERSION_7_1
  return primary_key_value if primary_key_value.is_a?(Array) && primary_key_value.length == 1
  return [primary_key_value] unless primary_key_value.is_a? Array

  # Re-order the returned values according to the returning columns if it is not equal to the primary key of the
  # table.
  keys = returning || primary_key
  return primary_key_value if keys == primary_key

  primary_key_values_hash = primary_key.zip(primary_key_value).to_h
  keys.map do |column|
    primary_key_values_hash[column]
  end
end

._insert_record(*args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/activerecord_spanner_adapter/base.rb', line 64

def self._insert_record *args
  if ActiveRecord.gem_version < VERSION_7_2
    values, returning = args
  else
    _connection, values, returning = args
  end

  if _should_use_standard_insert_record? values
    return super values if ActiveRecord.gem_version < VERSION_7_1
    return super
  end

  # Mutations cannot be used in combination with a sequence, as mutations do not support a THEN RETURN clause.
  if buffered_mutations? && sequence_name
    raise StatementInvalid, "Mutations cannot be used to create records that use a sequence " \
                            "to generate the primary key. #{self} uses #{sequence_name}."
  end

  return _buffer_record values, :insert, returning if buffered_mutations?

  _insert_record_dml values, returning
end

._insert_record_dml(values, returning) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/activerecord_spanner_adapter/base.rb', line 87

def self._insert_record_dml values, returning
  primary_key_value = _set_primary_key_value values
  if ActiveRecord::VERSION::MAJOR >= 7
    im = Arel::InsertManager.new arel_table
    im.insert(values.transform_keys { |name| arel_table[name] })
  else
    im = arel_table.compile_insert _substitute_values(values)
  end
  result = connection.insert(im, "#{self} Create", primary_key || false, primary_key_value)

  _convert_primary_key result, returning
end

._internal_insert_record(values) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/activerecord_spanner_adapter/base.rb', line 56

def self._internal_insert_record values
  if ActiveRecord.gem_version < VERSION_7_2
    _insert_record values
  else
    _insert_record nil, values
  end
end

._set_composite_primary_key_value(primary_key, values) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/activerecord_spanner_adapter/base.rb', line 222

def self._set_composite_primary_key_value primary_key, values
  value = values[primary_key]
  type = ActiveModel::Type::BigInteger.new

  if value.is_a? ActiveModel::Attribute
    type = value.type
    value = value.value
  end

  return value unless prefetch_primary_key?

  if value.nil?
    value = next_sequence_value
  end

  values[primary_key] =
    if ActiveRecord::VERSION::MAJOR >= 7
      ActiveModel::Attribute.from_database primary_key, value, type
    else
      value
    end

  value
end

._set_composite_primary_key_values(primary_key, values) ⇒ Object



216
217
218
219
220
# File 'lib/activerecord_spanner_adapter/base.rb', line 216

def self._set_composite_primary_key_values primary_key, values
  primary_key.map do |col|
    _set_composite_primary_key_value col, values
  end
end

._set_primary_key_value(values) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/activerecord_spanner_adapter/base.rb', line 100

def self._set_primary_key_value values
  if primary_key.is_a? Array
    _set_composite_primary_key_values primary_key, values
  else
    _set_single_primary_key_value primary_key, values
  end
end

._set_single_primary_key_value(primary_key, values) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/activerecord_spanner_adapter/base.rb', line 247

def self._set_single_primary_key_value primary_key, values
  primary_key_value = values[primary_key] || values[primary_key.to_sym]

  return primary_key_value if sequence_name
  return primary_key_value unless prefetch_primary_key?

  if primary_key_value.nil?
    primary_key_value = next_sequence_value
    if ActiveRecord::VERSION::MAJOR >= 7
      values[primary_key] = ActiveModel::Attribute.from_database primary_key, primary_key_value,
                                                                 ActiveModel::Type::BigInteger.new
    else
      values[primary_key] = primary_key_value
    end
  end

  primary_key_value
end

._should_use_standard_insert_record?(values) ⇒ Boolean



52
53
54
# File 'lib/activerecord_spanner_adapter/base.rb', line 52

def self._should_use_standard_insert_record? values
  !(buffered_mutations? || (primary_key && values.is_a?(Hash))) || !spanner_adapter?
end

._upsert_record(values, returning) ⇒ Object



127
128
129
# File 'lib/activerecord_spanner_adapter/base.rb', line 127

def self._upsert_record values, returning
  _buffer_record values, :insert_or_update, returning
end

.active_transaction?Boolean



277
278
279
280
# File 'lib/activerecord_spanner_adapter/base.rb', line 277

def self.active_transaction?
  current_transaction = connection.current_transaction
  !(current_transaction.nil? || current_transaction.is_a?(ConnectionAdapters::NullTransaction))
end

.buffered_mutations?Boolean



48
49
50
# File 'lib/activerecord_spanner_adapter/base.rb', line 48

def self.buffered_mutations?
  spanner_adapter? && connection&.current_spanner_transaction&.isolation == :buffered_mutations
end

.create(attributes = nil, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/activerecord_spanner_adapter/base.rb', line 33

def self.create attributes = nil, &block
  return super unless spanner_adapter?
  return super if active_transaction?

  # Only use mutations to create new records if the primary key is generated client-side.
  isolation = sequence_name ? nil : :buffered_mutations
  transaction isolation: isolation do
    return super
  end
end

.create!(attributes = nil, &block) ⇒ Object

Creates an object (or multiple objects) and saves it to the database. This method will use mutations instead of DML if there is no active transaction, or if the active transaction has been created with the option isolation: :buffered_mutations.



24
25
26
27
28
29
30
31
# File 'lib/activerecord_spanner_adapter/base.rb', line 24

def self.create! attributes = nil, &block
  return super unless spanner_adapter?
  return super if active_transaction?

  transaction isolation: :buffered_mutations do
    return super
  end
end

.delete_allObject

Deletes all records of this class. This method will use mutations instead of DML if there is no active transaction, or if the active transaction has been created with the option isolation: :buffered_mutations.



268
269
270
271
272
273
274
275
# File 'lib/activerecord_spanner_adapter/base.rb', line 268

def self.delete_all
  return super unless spanner_adapter?
  return super if active_transaction?

  transaction isolation: :buffered_mutations do
    return super
  end
end

.insert!(attributes, returning: nil, **kwargs) ⇒ Object



140
141
142
143
144
145
# File 'lib/activerecord_spanner_adapter/base.rb', line 140

def self.insert! attributes, returning: nil, **kwargs
  return super unless spanner_adapter?
  return super if active_transaction? && !buffered_mutations?

  insert_all! [attributes], returning: returning, **kwargs
end

.insert_all(attributes, returning: nil, **_kwargs) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/activerecord_spanner_adapter/base.rb', line 131

def self.insert_all attributes, returning: nil, **_kwargs
  if active_transaction? && buffered_mutations?
    raise NotImplementedError,
          "Spanner does not support skip_duplicates for mutations. " \
          "Use a transaction that uses DML, or use insert! or upsert instead."
  end
  super
end

.insert_all!(attributes, returning: nil, **_kwargs) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/activerecord_spanner_adapter/base.rb', line 147

def self.insert_all! attributes, returning: nil, **_kwargs
  return super unless spanner_adapter?
  return super if active_transaction? && !buffered_mutations?

  # This might seem inefficient, but is actually not, as it is only buffering a mutation locally.
  # The mutations will be sent as one batch when the transaction is committed.
  if active_transaction?
    attributes.each do |record|
      _internal_insert_record record
    end
  else
    transaction isolation: :buffered_mutations do
      attributes.each do |record|
        _internal_insert_record record
      end
    end
  end
end

.spanner_adapter?Boolean



44
45
46
# File 'lib/activerecord_spanner_adapter/base.rb', line 44

def self.spanner_adapter?
  connection.adapter_name == "spanner"
end

.unwrap_attribute(attr_or_value) ⇒ Object



282
283
284
285
286
287
288
# File 'lib/activerecord_spanner_adapter/base.rb', line 282

def self.unwrap_attribute attr_or_value
  if attr_or_value.is_a? ActiveModel::Attribute
    attr_or_value.value
  else
    attr_or_value
  end
end

.upsert(attributes, returning: nil, **kwargs) ⇒ Object



166
167
168
169
170
171
# File 'lib/activerecord_spanner_adapter/base.rb', line 166

def self.upsert attributes, returning: nil, **kwargs
  return super unless spanner_adapter?
  return super if active_transaction? && !buffered_mutations?

  upsert_all [attributes], returning: returning, **kwargs
end

.upsert_all(attributes, returning: nil, unique_by: nil, **kwargs) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/activerecord_spanner_adapter/base.rb', line 173

def self.upsert_all attributes, returning: nil, unique_by: nil, **kwargs
  return super unless spanner_adapter?
  return super if active_transaction? && !buffered_mutations?

  # This might seem inefficient, but is actually not, as it is only buffering a mutation locally.
  # The mutations will be sent as one batch when the transaction is committed.
  if active_transaction?
    attributes.each do |record|
      _upsert_record record, returning
    end
  else
    transaction isolation: :buffered_mutations do
      attributes.each do |record|
        _upsert_record record, returning
      end
    end
  end
end

Instance Method Details

#destroyObject

Deletes the object in the database. This method will use mutations instead of DML if there is no active transaction, or if the active transaction has been created with the option isolation: :buffered_mutations.



305
306
307
308
309
310
311
312
# File 'lib/activerecord_spanner_adapter/base.rb', line 305

def destroy
  return super unless self.class.spanner_adapter?
  return super if self.class.active_transaction?

  transaction isolation: :buffered_mutations do
    return super
  end
end

#update(attributes) ⇒ Object

Updates the given attributes of the object in the database. This method will use mutations instead of DML if there is no active transaction, or if the active transaction has been created with the option isolation: :buffered_mutations.



293
294
295
296
297
298
299
300
# File 'lib/activerecord_spanner_adapter/base.rb', line 293

def update attributes
  return super unless self.class.spanner_adapter?
  return super if self.class.active_transaction?

  transaction isolation: :buffered_mutations do
    return super
  end
end