-
Notifications
You must be signed in to change notification settings - Fork 28
/
application.rb
53 lines (47 loc) · 2.16 KB
/
application.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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
format = "%Y-%m-%dT%k:%M:%S.%N"
singer = nil
album = nil
ActiveRecord::Base.transaction do
# Create a singer and album with a commit timestamp that is set by Cloud Spanner.
# Use the `:commit_timestamp` symbol to instruct the Spanner ActiveRecord adapter to update the attribute to the
# commit timestamp of the transaction. Note that the value is not readable before the transaction has been
# committed. The commit timestamp of the following two records will be equal, as they are created in the same
# transaction.
singer = Singer.create first_name: "Pete", last_name: "Allison", last_updated: :commit_timestamp
album = singer.albums.create title: "Dear Repayment", last_updated: :commit_timestamp
end
# Reload the records to get the actual commit timestamp values.
singer.reload
album.reload
puts ""
puts "Singer and album created:"
puts "#{singer.first_name} #{singer.last_name} (Last updated: #{singer.last_updated.strftime format})"
puts " #{album.title} (Last updated: #{album.last_updated.strftime format})"
# The commit timestamp can also be set in an implicit transaction. Note that the following two statements will
# create two separate (implicit) transactions, and the commit timestamps of the two records will now be slightly
# different.
singer.update last_updated: :commit_timestamp
album.update last_updated: :commit_timestamp
singer.reload
album.reload
puts ""
puts "Singer and album updated:"
puts "#{singer.first_name} #{singer.last_name} (Last updated: #{singer.last_updated.strftime format})"
puts " #{album.title} (Last updated: #{album.last_updated.strftime format})"
puts ""
puts "Press any key to end the application"
STDIN.getch
end
end
Application.run