Class: ActiveRecord::Base

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

Constant Summary collapse

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



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/activerecord_spanner_adapter/base.rb', line 201

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

   = TableMetadata.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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/activerecord_spanner_adapter/base.rb', line 118

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 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

._has_all_primary_key_values?(primary_key, values) ⇒ Boolean

Returns:

  • (Boolean)


225
226
227
228
229
230
231
232
233
234
235
# File 'lib/activerecord_spanner_adapter/base.rb', line 225

def self._has_all_primary_key_values? primary_key, values
  if primary_key.is_a? Array
    all = TrueClass
    primary_key.each do |key|
      all &&= values.key? key
    end
    all
  else
    values.key? primary_key
  end
end

._insert_record(*args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/activerecord_spanner_adapter/base.rb', line 76

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
  end

  # Mutations cannot be used in combination with an auto-generated primary key,
  # as mutations do not support a THEN RETURN clause.
  if buffered_mutations? \
    && has_auto_generated_primary_key? \
    && !_has_all_primary_key_values?(primary_key, values) \
    && !connection.use_client_side_id_for_mutations
    raise StatementInvalid, "Mutations cannot be used to create records that use an auto-generated primary key."
  end

  return _buffer_record values, :insert, returning if buffered_mutations?

  _insert_record_dml values, returning
end

._insert_record_dml(values, returning) ⇒ Object



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

def self._insert_record_dml values, returning
  primary_key_value = _set_primary_key_value values, false
  im = Arel::InsertManager.new arel_table
  im.insert(values.transform_keys { |name| arel_table[name] })
  result = connection.insert(im, "#{self} Create", primary_key || false, primary_key_value)

  _convert_primary_key result, returning
end

._internal_insert_record(values) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/activerecord_spanner_adapter/base.rb', line 68

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, is_mutation) ⇒ Object



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

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

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

  return value if is_auto_generated?(primary_key) \
    && !(is_mutation && connection.use_client_side_id_for_mutations)
  return value unless prefetch_primary_key?

  if value.nil?
    value = next_sequence_value
  end

  values[primary_key] = ActiveModel::Attribute.from_database primary_key, value, type

  value
end

._set_composite_primary_key_values(primary_key, values, is_mutation) ⇒ Object



237
238
239
240
241
# File 'lib/activerecord_spanner_adapter/base.rb', line 237

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

._set_primary_key_value(values, is_mutation) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/activerecord_spanner_adapter/base.rb', line 110

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

._set_single_primary_key_value(primary_key, values, is_mutation) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/activerecord_spanner_adapter/base.rb', line 265

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

  return primary_key_value if has_auto_generated_primary_key? \
    && !(is_mutation && connection.use_client_side_id_for_mutations)
  return primary_key_value unless prefetch_primary_key?

  if primary_key_value.nil?
    primary_key_value = next_sequence_value
    values[primary_key] = ActiveModel::Attribute.from_database primary_key, primary_key_value,
                                                               ActiveModel::Type::BigInteger.new
  end

  primary_key_value
end

._should_use_standard_insert_record?(values) ⇒ Boolean

Returns:

  • (Boolean)


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

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

._upsert_record(values, returning) ⇒ Object



136
137
138
# File 'lib/activerecord_spanner_adapter/base.rb', line 136

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

.active_transaction?Boolean

Returns:

  • (Boolean)


292
293
294
295
# File 'lib/activerecord_spanner_adapter/base.rb', line 292

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

.buffered_mutations?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/activerecord_spanner_adapter/base.rb', line 41

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

.create(attributes = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/activerecord_spanner_adapter/base.rb', line 26

def self.create(attributes = nil, &)
  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 = has_auto_generated_primary_key? ? nil : :buffered_mutations
  transaction isolation: isolation do
    return super
  end
end

.create!(attributes = nil) ⇒ 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.



17
18
19
20
21
22
23
24
# File 'lib/activerecord_spanner_adapter/base.rb', line 17

def self.create!(attributes = nil, &)
  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.



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

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

  transaction isolation: :buffered_mutations do
    return super
  end
end

.dml_batchObject



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

def self.dml_batch(&)
  connection.dml_batch(&)
end

.has_auto_generated_primary_key?Boolean

Returns:

  • (Boolean)


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

def self.has_auto_generated_primary_key?
  return true if sequence_name
  pk = primary_key
  if pk.is_a? Array
    return pk.any? do |col|
      columns_hash[col].auto_incremented_by_db?
    end
  end
  columns_hash[pk].auto_incremented_by_db?
end

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



149
150
151
152
153
154
# File 'lib/activerecord_spanner_adapter/base.rb', line 149

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



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

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/activerecord_spanner_adapter/base.rb', line 156

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

.is_auto_generated?(col) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/activerecord_spanner_adapter/base.rb', line 64

def self.is_auto_generated? col
  columns_hash[col]&.auto_incremented_by_db?
end

.spanner_adapter?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/activerecord_spanner_adapter/base.rb', line 37

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

.unwrap_attribute(attr_or_value) ⇒ Object



297
298
299
300
301
302
303
# File 'lib/activerecord_spanner_adapter/base.rb', line 297

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



175
176
177
178
179
180
# File 'lib/activerecord_spanner_adapter/base.rb', line 175

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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/activerecord_spanner_adapter/base.rb', line 182

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.



320
321
322
323
324
325
326
327
# File 'lib/activerecord_spanner_adapter/base.rb', line 320

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.



308
309
310
311
312
313
314
315
# File 'lib/activerecord_spanner_adapter/base.rb', line 308

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