Sha256: 2ca54cf21aaae20c43e4b0c1f8e0700dbfdf481f5b283400d6ff26bf063b1462

Contents?: true

Size: 1.85 KB

Versions: 22

Compression:

Stored size: 1.85 KB

Contents

# Copyright 2021 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.

require "io/console"
require_relative "../config/environment"
require_relative "models/singer"
require_relative "models/album"

class Application
  def self.run # rubocop:disable Metrics/AbcSize
    from_album = nil
    to_album = nil
    # This transaction uses Mutations instead of DML. That means that any data changes are not readable during
    # the transaction itself, as the mutations are buffered locally in the client and only sent to Spanner
    # when the transaction is committed.
    ActiveRecord::Base.transaction isolation: :buffered_mutations do
      # Transfer a marketing budget of 10,000 from one album to another.
      from_album = Album.all.sample
      to_album = Album.where.not(id: from_album.id).sample

      puts ""
      puts "Transferring 10,000 marketing budget from #{from_album.title} (#{from_album.marketing_budget}) "\
           "to #{to_album.title} (#{to_album.marketing_budget})"
      from_album.update marketing_budget: from_album.marketing_budget - 10000
      to_album.update marketing_budget: to_album.marketing_budget + 10000

      # The above change is not sent to the database before the transaction is committed.
      puts ""
      puts "Budgets before commit:"
      puts "Marketing budget #{from_album.title}: #{from_album.reload.marketing_budget}"
      puts "Marketing budget #{to_album.title}: #{to_album.reload.marketing_budget}"
    end
    puts ""
    puts "Budgets after commit:"
    puts "Marketing budget #{from_album.title}: #{from_album.reload.marketing_budget}"
    puts "Marketing budget #{to_album.title}: #{to_album.reload.marketing_budget}"

    puts ""
    puts "Press any key to end the application"
    STDIN.getch
  end
end

Application.run

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
activerecord-spanner-adapter-1.8.0 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.6.3 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.6.2 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.6.1 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.6.0 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.5.1 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.5.0 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.4.4 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.4.3 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.4.2 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.4.1 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.4.0 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.3.1 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.2.2 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.2.1 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.2.0 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.1.0 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.0.1 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-1.0.0 examples/snippets/mutations/application.rb
activerecord-spanner-adapter-0.7.0 examples/snippets/mutations/application.rb