Class: ActiveRecord::Base

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._insert_record(values) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/activerecord_spanner_adapter/base.rb', line 44

def self._insert_record values
  return super unless buffered_mutations?

  primary_key = self.primary_key
  primary_key_value = nil

  if primary_key && values.is_a?(Hash)
    primary_key_value = values[primary_key]

    if !primary_key_value && prefetch_primary_key?
      primary_key_value = next_sequence_value
      values[primary_key] = primary_key_value
    end
  end

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

  mutation = Google::Cloud::Spanner::V1::Mutation.new(
    insert: Google::Cloud::Spanner::V1::Mutation::Write.new(
      table: arel_table.name,
      columns: columns,
      values: [grpc_values.list_value]
    )
  )
  connection.current_spanner_transaction.buffer mutation

  primary_key_value
end

.active_transaction?Boolean

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/activerecord_spanner_adapter/base.rb', line 85

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

.buffered_mutations?Boolean

Returns:

  • (Boolean)


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

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

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



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

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

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



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

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.



76
77
78
79
80
81
82
83
# File 'lib/activerecord_spanner_adapter/base.rb', line 76

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

  transaction isolation: :buffered_mutations do
    return super
  end
end

.spanner_adapter?Boolean

Returns:

  • (Boolean)


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

def self.spanner_adapter?
  connection.adapter_name == "spanner"
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.



105
106
107
108
109
110
111
112
# File 'lib/activerecord_spanner_adapter/base.rb', line 105

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.



93
94
95
96
97
98
99
100
# File 'lib/activerecord_spanner_adapter/base.rb', line 93

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