-
Notifications
You must be signed in to change notification settings - Fork 21
/
application.rb
112 lines (96 loc) · 3.45 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# frozen_string_literal: true
# Copyright 2023 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 'active_record'
require 'io/console'
require_relative 'config/environment'
require_relative 'models/singer'
require_relative 'models/album'
require_relative 'models/concert'
require_relative 'models/ticket_sale'
# This sample application shows the basic features of
# ActiveRecord with Google Cloud Spanner PostgreSQL-dialect.
class Application
def self.run
# Query the current singers in the database.
query_singers
# Update a random singer.
update_singer
# Execute a query on the singers table.
query_singers_by_name
# List all singers and related venue information.
query_concerts
# Create a ticket_sale record that will be assigned a auto-generated primary key value.
create_ticket_sale
end
def self.query_singers
# Fetch all singers and albums from the database.
# The database has been pre-filled by the `db/seeds.rb` script.
puts ''
puts 'Known singers and their albums:'
puts ''
Singer.all.each do |singer|
puts "#{singer.first_name} #{singer.last_name} has albums:"
singer.albums.each do |album|
puts " #{album.title} has tracks:"
album.tracks.each do |track|
puts " #{track.track_number} #{track.title}"
end
end
end
end
def self.update_singer
# Select a random singer and update the name of this singer.
puts ''
singer = Singer.all.sample
puts ''
puts "Current name of singer #{singer.id} is '#{singer.first_name} #{singer.last_name}'"
puts "Updating name to 'Dave Anderson'"
singer.first_name = 'Dave'
singer.last_name = 'Anderson'
singer.save!
singer.reload
puts "New name of singer #{singer.id}: #{singer.first_name} #{singer.last_name}"
end
def self.query_singers_by_name
# Select all singers whose last name start with 'A'.
# This should include at least the singer that was updated
# in the previous step, but probably also a number of other singers.
puts ''
puts "Getting all singers with a last name that starts with 'A'"
last_name = Singer.arel_table['last_name']
# The 'escape=nil, case_sensitive=true' is necessary to work around the lack of support
# for ILIKE in Cloud Spanner.
Singer.where(last_name.matches("A%", escape=nil, case_sensitive=true)).each do |s|
puts "#{s.first_name} #{s.last_name}"
end
end
# Lists all concerts and related venue information.
def self.query_concerts
puts ''
puts 'Known concerts:'
puts ''
Concert.all.each do |concert|
puts "#{concert.name} at #{concert.venue.name}"
puts " Start time: #{concert.start_time}"
puts " Venue description: #{concert.venue.description}"
end
end
def self.create_ticket_sale
concert = Concert.first
ticket_sale = TicketSale.create({
concert: concert,
customer_name: "Alice",
price: 99.90,
seats: %w[A19 A20 A21],
})
puts ""
puts "Created ticket sale for concert '#{concert.name}' for customer #{ticket_sale.customer_name}"
puts "The ticket sale was assigned auto-generated key #{ticket_sale.id}"
puts ""
end
end
Application.run