activerecord-spanner-adapter 0.7.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/blunderbuss.yml +2 -0
- data/.github/sync-repo-settings.yaml +1 -1
- data/.github/workflows/acceptance-tests-on-emulator.yaml +8 -6
- data/.github/workflows/acceptance-tests-on-production.yaml +3 -3
- data/.github/workflows/ci.yaml +8 -6
- data/.github/workflows/nightly-acceptance-tests-on-emulator.yaml +9 -5
- data/.github/workflows/nightly-acceptance-tests-on-production.yaml +2 -2
- data/.github/workflows/nightly-unit-tests.yaml +9 -5
- data/.github/workflows/release-please-label.yml +4 -4
- data/.github/workflows/release-please.yml +12 -11
- data/.github/workflows/rubocop.yaml +4 -4
- data/.release-please-manifest.json +3 -0
- data/CHANGELOG.md +48 -26
- data/CONTRIBUTING.md +1 -1
- data/Gemfile +2 -1
- data/README.md +3 -3
- data/acceptance/cases/models/insert_all_test.rb +150 -0
- data/acceptance/cases/type/all_types_test.rb +24 -25
- data/acceptance/cases/type/json_test.rb +0 -2
- data/acceptance/schema/schema.rb +2 -4
- data/acceptance/test_helper.rb +1 -1
- data/activerecord-spanner-adapter.gemspec +1 -1
- data/examples/rails/README.md +8 -8
- data/lib/active_record/connection_adapters/spanner/schema_creation.rb +10 -4
- data/lib/active_record/connection_adapters/spanner_adapter.rb +64 -31
- data/lib/active_record/type/spanner/array.rb +19 -5
- data/lib/activerecord_spanner_adapter/base.rb +72 -5
- data/lib/activerecord_spanner_adapter/connection.rb +46 -20
- data/lib/activerecord_spanner_adapter/information_schema.rb +2 -1
- data/lib/activerecord_spanner_adapter/transaction.rb +52 -21
- data/lib/activerecord_spanner_adapter/version.rb +1 -1
- data/lib/arel/visitors/spanner.rb +10 -0
- data/lib/spanner_client_ext.rb +4 -0
- data/release-please-config.json +19 -0
- metadata +17 -17
- data/examples/snippets/interleaved-tables/README.md +0 -152
- data/examples/snippets/interleaved-tables/Rakefile +0 -13
- data/examples/snippets/interleaved-tables/application.rb +0 -109
- data/examples/snippets/interleaved-tables/config/database.yml +0 -8
- data/examples/snippets/interleaved-tables/db/migrate/01_create_tables.rb +0 -44
- data/examples/snippets/interleaved-tables/db/schema.rb +0 -32
- data/examples/snippets/interleaved-tables/db/seeds.rb +0 -40
- data/examples/snippets/interleaved-tables/models/album.rb +0 -15
- data/examples/snippets/interleaved-tables/models/singer.rb +0 -20
- data/examples/snippets/interleaved-tables/models/track.rb +0 -25
@@ -30,28 +30,37 @@ module ActiveRecordSpannerAdapter
|
|
30
30
|
@mutations << mutation
|
31
31
|
end
|
32
32
|
|
33
|
+
# Begins the transaction.
|
34
|
+
#
|
35
|
+
# Read-only and PDML transactions are started by executing a BeginTransaction RPC.
|
36
|
+
# Read/write transactions are not really started by this method, and instead a
|
37
|
+
# transaction selector is prepared that will be included with the first statement
|
38
|
+
# on the transaction.
|
33
39
|
def begin
|
34
40
|
raise "Nested transactions are not allowed" if @state != :INITIALIZED
|
35
41
|
begin
|
36
|
-
@
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
@connection.session.create_snapshot strong: true
|
45
|
-
else
|
46
|
-
raise "Invalid snapshot argument: #{@isolation}"
|
47
|
-
end
|
48
|
-
when :read_only
|
49
|
-
@connection.session.create_snapshot strong: true
|
50
|
-
when :pdml
|
51
|
-
@connection.session.create_pdml
|
42
|
+
case @isolation
|
43
|
+
when Hash
|
44
|
+
if @isolation[:timestamp]
|
45
|
+
@grpc_transaction = @connection.session.create_snapshot timestamp: @isolation[:timestamp]
|
46
|
+
elsif @isolation[:staleness]
|
47
|
+
@grpc_transaction = @connection.session.create_snapshot staleness: @isolation[:staleness]
|
48
|
+
elsif @isolation[:strong]
|
49
|
+
@grpc_transaction = @connection.session.create_snapshot strong: true
|
52
50
|
else
|
53
|
-
@
|
51
|
+
raise "Invalid snapshot argument: #{@isolation}"
|
54
52
|
end
|
53
|
+
when :read_only
|
54
|
+
@grpc_transaction = @connection.session.create_snapshot strong: true
|
55
|
+
when :pdml
|
56
|
+
@grpc_transaction = @connection.session.create_pdml
|
57
|
+
else
|
58
|
+
@begin_transaction_selector = Google::Spanner::V1::TransactionSelector.new \
|
59
|
+
begin: Google::Spanner::V1::TransactionOptions.new(
|
60
|
+
read_write: Google::Spanner::V1::TransactionOptions::ReadWrite.new
|
61
|
+
)
|
62
|
+
|
63
|
+
end
|
55
64
|
@state = :STARTED
|
56
65
|
rescue Google::Cloud::NotFoundError => e
|
57
66
|
if @connection.session_not_found? e
|
@@ -66,6 +75,12 @@ module ActiveRecordSpannerAdapter
|
|
66
75
|
end
|
67
76
|
end
|
68
77
|
|
78
|
+
# Forces a BeginTransaction RPC for a read/write transaction. This is used by a
|
79
|
+
# connection if the first statement of a transaction failed.
|
80
|
+
def force_begin_read_write
|
81
|
+
@grpc_transaction = @connection.session.create_transaction
|
82
|
+
end
|
83
|
+
|
69
84
|
def next_sequence_number
|
70
85
|
@sequence_number += 1 if @committable
|
71
86
|
end
|
@@ -74,7 +89,10 @@ module ActiveRecordSpannerAdapter
|
|
74
89
|
raise "This transaction is not active" unless active?
|
75
90
|
|
76
91
|
begin
|
77
|
-
|
92
|
+
# Start a transaction with an explicit BeginTransaction RPC if the transaction only contains mutations.
|
93
|
+
force_begin_read_write if @committable && !@mutations.empty? && !@grpc_transaction
|
94
|
+
|
95
|
+
@connection.session.commit_transaction @grpc_transaction, @mutations if @committable && @grpc_transaction
|
78
96
|
@state = :COMMITTED
|
79
97
|
rescue Google::Cloud::NotFoundError => e
|
80
98
|
if @connection.session_not_found? e
|
@@ -93,7 +111,7 @@ module ActiveRecordSpannerAdapter
|
|
93
111
|
def rollback
|
94
112
|
# Allow rollback after abort and/or a failed commit.
|
95
113
|
raise "This transaction is not active" unless active? || @state == :FAILED || @state == :ABORTED
|
96
|
-
if active?
|
114
|
+
if active? && @grpc_transaction
|
97
115
|
# We do a shoot-and-forget rollback here, as the error that caused the transaction to be rolled back could
|
98
116
|
# also have invalidated the transaction (e.g. `Session not found`). If the rollback fails for any other
|
99
117
|
# reason, we also do not need to retry it or propagate the error to the application, as the transaction will
|
@@ -113,11 +131,24 @@ module ActiveRecordSpannerAdapter
|
|
113
131
|
@state = :ABORTED
|
114
132
|
end
|
115
133
|
|
134
|
+
# Sets the underlying gRPC transaction to use for this Transaction.
|
135
|
+
# This is used for queries/DML statements that inlined the BeginTransaction option and returned
|
136
|
+
# a transaction in the metadata.
|
137
|
+
def grpc_transaction= grpc
|
138
|
+
@grpc_transaction = Google::Cloud::Spanner::Transaction.from_grpc grpc, @connection.session
|
139
|
+
end
|
140
|
+
|
116
141
|
def transaction_selector
|
117
142
|
return unless active?
|
118
143
|
|
119
|
-
|
120
|
-
|
144
|
+
# Use the transaction that has been started by a BeginTransaction RPC or returned by a
|
145
|
+
# statement, if present.
|
146
|
+
return Google::Spanner::V1::TransactionSelector.new id: @grpc_transaction.transaction_id \
|
147
|
+
if @grpc_transaction
|
148
|
+
|
149
|
+
# Return a transaction selector that will instruct the statement to also start a transaction
|
150
|
+
# and return its id as a side effect.
|
151
|
+
@begin_transaction_selector
|
121
152
|
end
|
122
153
|
end
|
123
154
|
end
|
@@ -98,6 +98,16 @@ module Arel # :nodoc: all
|
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
+
# For ActiveRecord 7.0
|
102
|
+
def visit_ActiveModel_Attribute o, collector
|
103
|
+
# Do not generate a query parameter if the value should be set to the PENDING_COMMIT_TIMESTAMP(), as that is
|
104
|
+
# not supported as a parameter value by Cloud Spanner.
|
105
|
+
return collector << "PENDING_COMMIT_TIMESTAMP()" \
|
106
|
+
if o.type.is_a?(ActiveRecord::Type::Spanner::Time) && o.value == :commit_timestamp
|
107
|
+
collector.add_bind(o, &bind_block)
|
108
|
+
end
|
109
|
+
|
110
|
+
# For ActiveRecord 6.x
|
101
111
|
def visit_Arel_Nodes_BindParam o, collector
|
102
112
|
# Do not generate a query parameter if the value should be set to the PENDING_COMMIT_TIMESTAMP(), as that is
|
103
113
|
# not supported as a parameter value by Cloud Spanner.
|
data/lib/spanner_client_ext.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
{
|
2
|
+
"bump-minor-pre-major": true,
|
3
|
+
"bump-patch-for-minor-pre-major": false,
|
4
|
+
"draft": false,
|
5
|
+
"include-component-in-tag": true,
|
6
|
+
"include-v-in-tag": true,
|
7
|
+
"prerelease": false,
|
8
|
+
"release-type": "ruby-yoshi",
|
9
|
+
"skip-github-release": false,
|
10
|
+
"separate-pull-requests": true,
|
11
|
+
"tag-separator": "/",
|
12
|
+
"sequential-calls": true,
|
13
|
+
"packages": {
|
14
|
+
".": {
|
15
|
+
"component": "activerecord-spanner-adapter",
|
16
|
+
"version-file": "lib/activerecord_spanner_adapter/version.rb"
|
17
|
+
}
|
18
|
+
}
|
19
|
+
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-spanner-adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Google LLC
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: google-cloud-spanner
|
@@ -28,16 +28,22 @@ dependencies:
|
|
28
28
|
name: activerecord
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 6.0.0
|
34
|
+
- - "<"
|
32
35
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
36
|
+
version: '7.1'
|
34
37
|
type: :runtime
|
35
38
|
prerelease: false
|
36
39
|
version_requirements: !ruby/object:Gem::Requirement
|
37
40
|
requirements:
|
38
|
-
- - "
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 6.0.0
|
44
|
+
- - "<"
|
39
45
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
46
|
+
version: '7.1'
|
41
47
|
- !ruby/object:Gem::Dependency
|
42
48
|
name: autotest-suffix
|
43
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -214,6 +220,7 @@ extensions: []
|
|
214
220
|
extra_rdoc_files: []
|
215
221
|
files:
|
216
222
|
- ".github/CODEOWNERS"
|
223
|
+
- ".github/blunderbuss.yml"
|
217
224
|
- ".github/sync-repo-settings.yaml"
|
218
225
|
- ".github/workflows/acceptance-tests-on-emulator.yaml"
|
219
226
|
- ".github/workflows/acceptance-tests-on-production.yaml"
|
@@ -229,6 +236,7 @@ files:
|
|
229
236
|
- ".kokoro/release.cfg"
|
230
237
|
- ".kokoro/release.sh"
|
231
238
|
- ".kokoro/trampoline_v2.sh"
|
239
|
+
- ".release-please-manifest.json"
|
232
240
|
- ".rubocop.yml"
|
233
241
|
- ".toys/release.rb"
|
234
242
|
- ".trampolinerc"
|
@@ -262,6 +270,7 @@ files:
|
|
262
270
|
- acceptance/cases/migration/rename_column_test.rb
|
263
271
|
- acceptance/cases/models/calculation_query_test.rb
|
264
272
|
- acceptance/cases/models/generated_column_test.rb
|
273
|
+
- acceptance/cases/models/insert_all_test.rb
|
265
274
|
- acceptance/cases/models/mutation_test.rb
|
266
275
|
- acceptance/cases/models/query_test.rb
|
267
276
|
- acceptance/cases/sessions/session_not_found_test.rb
|
@@ -378,16 +387,6 @@ files:
|
|
378
387
|
- examples/snippets/hints/db/seeds.rb
|
379
388
|
- examples/snippets/hints/models/album.rb
|
380
389
|
- examples/snippets/hints/models/singer.rb
|
381
|
-
- examples/snippets/interleaved-tables/README.md
|
382
|
-
- examples/snippets/interleaved-tables/Rakefile
|
383
|
-
- examples/snippets/interleaved-tables/application.rb
|
384
|
-
- examples/snippets/interleaved-tables/config/database.yml
|
385
|
-
- examples/snippets/interleaved-tables/db/migrate/01_create_tables.rb
|
386
|
-
- examples/snippets/interleaved-tables/db/schema.rb
|
387
|
-
- examples/snippets/interleaved-tables/db/seeds.rb
|
388
|
-
- examples/snippets/interleaved-tables/models/album.rb
|
389
|
-
- examples/snippets/interleaved-tables/models/singer.rb
|
390
|
-
- examples/snippets/interleaved-tables/models/track.rb
|
391
390
|
- examples/snippets/migrations/README.md
|
392
391
|
- examples/snippets/migrations/Rakefile
|
393
392
|
- examples/snippets/migrations/application.rb
|
@@ -499,6 +498,7 @@ files:
|
|
499
498
|
- lib/activerecord_spanner_adapter/version.rb
|
500
499
|
- lib/arel/visitors/spanner.rb
|
501
500
|
- lib/spanner_client_ext.rb
|
501
|
+
- release-please-config.json
|
502
502
|
- renovate.json
|
503
503
|
homepage: https://github.com/googleapis/ruby-spanner-activerecord
|
504
504
|
licenses:
|
@@ -519,7 +519,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
519
519
|
- !ruby/object:Gem::Version
|
520
520
|
version: '0'
|
521
521
|
requirements: []
|
522
|
-
rubygems_version: 3.
|
522
|
+
rubygems_version: 3.3.14
|
523
523
|
signing_key:
|
524
524
|
specification_version: 4
|
525
525
|
summary: Rails ActiveRecord connector for Google Spanner Database
|
@@ -1,152 +0,0 @@
|
|
1
|
-
# Sample - Interleaved Tables
|
2
|
-
|
3
|
-
This example shows how to use interleaved tables with the Spanner ActiveRecord adapter.
|
4
|
-
|
5
|
-
See https://cloud.google.com/spanner/docs/schema-and-data-model#creating-interleaved-tables for more information
|
6
|
-
on interleaved tables if you are not familiar with this concept.
|
7
|
-
|
8
|
-
## Creating Interleaved Tables in ActiveRecord
|
9
|
-
You can create interleaved tables using migrations in ActiveRecord by using the following Spanner ActiveRecord specific
|
10
|
-
methods that are defined on `TableDefinition`:
|
11
|
-
* `interleave_in`: Specifies which parent table a child table should be interleaved in and optionally whether
|
12
|
-
deletes of a parent record should automatically cascade delete all child records.
|
13
|
-
* `parent_key`: Creates a column that is a reference to (a part of) the primary key of the parent table. Each child
|
14
|
-
table must include all the primary key columns of the parent table as a `parent_key`.
|
15
|
-
|
16
|
-
Cloud Spanner requires a child table to include the exact same primary key columns as the parent table in addition to
|
17
|
-
the primary key column(s) of the child table. This means that the default `id` primary key column of ActiveRecord is
|
18
|
-
not usable in combination with interleaved tables. Instead each primary key column should be prefixed with the table
|
19
|
-
name of the table that it references, or use some other unique name.
|
20
|
-
|
21
|
-
This example uses the following table schema:
|
22
|
-
|
23
|
-
```sql
|
24
|
-
CREATE TABLE singers (
|
25
|
-
singerid INT64 NOT NULL,
|
26
|
-
first_name STRING(MAX),
|
27
|
-
last_name STRING(MAX)
|
28
|
-
) PRIMARY KEY (singerid);
|
29
|
-
|
30
|
-
CREATE TABLE albums (
|
31
|
-
albumid INT64 NOT NULL,
|
32
|
-
singerid INT64 NOT NULL,
|
33
|
-
title STRING(MAX)
|
34
|
-
) PRIMARY KEY (singerid, albumid), INTERLEAVE IN PARENT singers;
|
35
|
-
|
36
|
-
CREATE TABLE tracks (
|
37
|
-
trackid INT64 NOT NULL,
|
38
|
-
singerid INT64 NOT NULL,
|
39
|
-
albumid INT64 NOT NULL,
|
40
|
-
title STRING(MAX),
|
41
|
-
duration NUMERIC
|
42
|
-
) PRIMARY KEY (singerid, albumid, trackid), INTERLEAVE IN PARENT albums ON DELETE CASCADE;
|
43
|
-
```
|
44
|
-
|
45
|
-
This schema can be created in ActiveRecord as follows:
|
46
|
-
|
47
|
-
```ruby
|
48
|
-
create_table :singers, id: false do |t|
|
49
|
-
# Explicitly define the primary key with a custom name to prevent all primary key columns from being named `id`.
|
50
|
-
t.primary_key :singerid
|
51
|
-
t.string :first_name
|
52
|
-
t.string :last_name
|
53
|
-
end
|
54
|
-
|
55
|
-
create_table :albums, id: false do |t|
|
56
|
-
# Interleave the `albums` table in the parent table `singers`.
|
57
|
-
t.interleave_in :singers
|
58
|
-
t.primary_key :albumid
|
59
|
-
# `singerid` is defined as a `parent_key` which makes it a part of the primary key in the table definition, but
|
60
|
-
# it is not presented to ActiveRecord as part of the primary key, to prevent ActiveRecord from considering this
|
61
|
-
# to be an entity with a composite primary key (which is not supported by ActiveRecord).
|
62
|
-
t.parent_key :singerid
|
63
|
-
t.string :title
|
64
|
-
end
|
65
|
-
|
66
|
-
create_table :tracks, id: false do |t|
|
67
|
-
# Interleave the `tracks` table in the parent table `albums` and cascade delete all tracks that belong to an
|
68
|
-
# album when an album is deleted.
|
69
|
-
t.interleave_in :albums, :cascade
|
70
|
-
# `trackid` is considered the only primary key column by ActiveRecord.
|
71
|
-
t.primary_key :trackid
|
72
|
-
# `singerid` and `albumid` form the parent key of `tracks`. These are part of the primary key definition in the
|
73
|
-
# database, but are presented as parent keys to ActiveRecord.
|
74
|
-
t.parent_key :singerid
|
75
|
-
t.parent_key :albumid
|
76
|
-
t.string :title
|
77
|
-
t.numeric :duration
|
78
|
-
end
|
79
|
-
```
|
80
|
-
|
81
|
-
## Models for Interleaved Tables
|
82
|
-
An interleaved table parent/child relationship must be modelled as a `belongs_to`/`has_many` association in
|
83
|
-
ActiveRecord. As the columns that are used to reference a parent record use a custom column name, it is required to also
|
84
|
-
include the custom column name in the `belongs_to` and `has_many` definitions.
|
85
|
-
|
86
|
-
Instances of these models can be used in the same way as any other association in ActiveRecord, but with a couple of
|
87
|
-
inherent limitations:
|
88
|
-
* It is not possible to change the parent record of a child record. For instance, changing the singer of an album in the
|
89
|
-
above example is impossible, as Cloud Spanner does not allow such an update.
|
90
|
-
* It is not possible to de-reference a parent record by setting it to null.
|
91
|
-
* It is only possible to delete a parent record with existing child records, if the child records are also deleted. This
|
92
|
-
can be done by enabling ON DELETE CASCADE in Cloud Spanner, or by deleting the child records using ActiveRecord.
|
93
|
-
|
94
|
-
### Example Models
|
95
|
-
|
96
|
-
```ruby
|
97
|
-
class Singer < ActiveRecord::Base
|
98
|
-
# `albums` is defined as INTERLEAVE IN PARENT `singers`. The primary key of `albums` is (`singerid`, `albumid`), but
|
99
|
-
# only `albumid` is used by ActiveRecord as the primary key. The `singerid` column is defined as a `parent_key` of
|
100
|
-
# `albums` (see also the `db/migrate/01_create_tables.rb` file).
|
101
|
-
has_many :albums, foreign_key: "singerid"
|
102
|
-
|
103
|
-
# `tracks` is defined as INTERLEAVE IN PARENT `albums`. The primary key of `tracks` is
|
104
|
-
# (`singerid`, `albumid`, `trackid`), but only `trackid` is used by ActiveRecord as the primary key. The `singerid`
|
105
|
-
# and `albumid` columns are defined as `parent_key` of `tracks` (see also the `db/migrate/01_create_tables.rb` file).
|
106
|
-
# The `singerid` column can therefore be used to associate tracks with a singer without the need to go through albums.
|
107
|
-
# Note also that the inclusion of `singerid` as a column in `tracks` is required in order to make `tracks` a child
|
108
|
-
# table of `albums` which has primary key (`singerid`, `albumid`).
|
109
|
-
has_many :tracks, foreign_key: "singerid"
|
110
|
-
end
|
111
|
-
|
112
|
-
class Album < ActiveRecord::Base
|
113
|
-
# `albums` is defined as INTERLEAVE IN PARENT `singers`. The primary key of `singers` is `singerid`.
|
114
|
-
belongs_to :singer, foreign_key: "singerid"
|
115
|
-
|
116
|
-
# `tracks` is defined as INTERLEAVE IN PARENT `albums`. The primary key of `albums` is (`singerid`, `albumid`), but
|
117
|
-
# only `albumid` is used by ActiveRecord as the primary key. The `singerid` column is defined as a `parent_key` of
|
118
|
-
# `albums` (see also the `db/migrate/01_create_tables.rb` file).
|
119
|
-
has_many :tracks, foreign_key: "albumid"
|
120
|
-
end
|
121
|
-
|
122
|
-
class Track < ActiveRecord::Base
|
123
|
-
# `tracks` is defined as INTERLEAVE IN PARENT `albums`. The primary key of `albums` is ()`singerid`, `albumid`).
|
124
|
-
belongs_to :album, foreign_key: "albumid"
|
125
|
-
|
126
|
-
# `tracks` also has a `singerid` column should be used to associate a Track with a Singer.
|
127
|
-
belongs_to :singer, foreign_key: "singerid"
|
128
|
-
|
129
|
-
# Override the default initialize method to automatically set the singer attribute when an album is given.
|
130
|
-
def initialize attributes = nil
|
131
|
-
super
|
132
|
-
self.singer ||= album&.singer
|
133
|
-
end
|
134
|
-
|
135
|
-
def album=value
|
136
|
-
super
|
137
|
-
# Ensure the singer of this track is equal to the singer of the album that is set.
|
138
|
-
self.singer = value&.singer
|
139
|
-
end
|
140
|
-
end
|
141
|
-
```
|
142
|
-
|
143
|
-
## Running the Sample
|
144
|
-
|
145
|
-
The sample will automatically start a Spanner Emulator in a docker container and execute the sample
|
146
|
-
against that emulator. The emulator will automatically be stopped when the application finishes.
|
147
|
-
|
148
|
-
Run the application with the command
|
149
|
-
|
150
|
-
```bash
|
151
|
-
bundle exec rake run
|
152
|
-
```
|
@@ -1,13 +0,0 @@
|
|
1
|
-
# Copyright 2021 Google LLC
|
2
|
-
#
|
3
|
-
# Use of this source code is governed by an MIT-style
|
4
|
-
# license that can be found in the LICENSE file or at
|
5
|
-
# https://opensource.org/licenses/MIT.
|
6
|
-
|
7
|
-
require_relative "../config/environment"
|
8
|
-
require "sinatra/activerecord/rake"
|
9
|
-
|
10
|
-
desc "Sample showing how to work with interleaved tables in ActiveRecord."
|
11
|
-
task :run do
|
12
|
-
Dir.chdir("..") { sh "bundle exec rake run[interleaved-tables]" }
|
13
|
-
end
|
@@ -1,109 +0,0 @@
|
|
1
|
-
# Copyright 2021 Google LLC
|
2
|
-
#
|
3
|
-
# Use of this source code is governed by an MIT-style
|
4
|
-
# license that can be found in the LICENSE file or at
|
5
|
-
# https://opensource.org/licenses/MIT.
|
6
|
-
|
7
|
-
require "io/console"
|
8
|
-
require_relative "../config/environment"
|
9
|
-
require_relative "models/singer"
|
10
|
-
require_relative "models/album"
|
11
|
-
require_relative "models/track"
|
12
|
-
|
13
|
-
class Application
|
14
|
-
def self.run
|
15
|
-
# List all singers, albums and tracks.
|
16
|
-
list_singers_albums_tracks
|
17
|
-
|
18
|
-
# Create a new album with some tracks.
|
19
|
-
create_new_album
|
20
|
-
|
21
|
-
# Try to update the singer of an album. This is not possible as albums are interleaved in singers.
|
22
|
-
update_singer_of_album
|
23
|
-
|
24
|
-
# Try to delete a singer that has at least one album. This is NOT possible as albums is NOT marked with
|
25
|
-
# ON DELETE CASCADE.
|
26
|
-
delete_singer_with_albums
|
27
|
-
|
28
|
-
# Try to delete an album that has at least one track. This IS possible as tracks IS marked with
|
29
|
-
# ON DELETE CASCADE.
|
30
|
-
delete_album_with_tracks
|
31
|
-
|
32
|
-
puts ""
|
33
|
-
puts "Press any key to end the application"
|
34
|
-
STDIN.getch
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.list_singers_albums_tracks
|
38
|
-
puts ""
|
39
|
-
puts "Listing all singers with corresponding albums and tracks"
|
40
|
-
Singer.all.order("last_name, first_name").each do |singer|
|
41
|
-
puts "#{singer.first_name} #{singer.last_name} has #{singer.albums.count} albums:"
|
42
|
-
singer.albums.order("title").each do |album|
|
43
|
-
puts " #{album.title} has #{album.tracks.count} tracks:"
|
44
|
-
album.tracks.each do |track|
|
45
|
-
puts " #{track.title}"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def self.create_new_album
|
52
|
-
# Create a new album with some tracks.
|
53
|
-
puts ""
|
54
|
-
singer = Singer.all.sample
|
55
|
-
puts "Creating a new album for #{singer.first_name} #{singer.last_name}"
|
56
|
-
album = singer.albums.build title: "New Title"
|
57
|
-
album.tracks.build title: "Track 1", duration: 3.5, singer: singer
|
58
|
-
album.tracks.build title: "Track 2", duration: 3.6, singer: singer
|
59
|
-
# This will save the album and corresponding tracks in one transaction.
|
60
|
-
album.save!
|
61
|
-
|
62
|
-
album.reload
|
63
|
-
puts "Album #{album.title} has #{album.tracks.count} tracks:"
|
64
|
-
album.tracks.order("title").each do |track|
|
65
|
-
puts " #{track.title} with duration #{track.duration}"
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def self.update_singer_of_album
|
70
|
-
# It is not possible to change the singer of an album or the album of a track. This is because the associations
|
71
|
-
# between these are not traditional foreign keys, but an immutable parent-child relationship.
|
72
|
-
album = Album.all.sample
|
73
|
-
new_singer = Singer.all.except(album.singer).sample
|
74
|
-
# This will fail as we cannot assign a new singer to an album as it is an INTERLEAVE IN PARENT relationship.
|
75
|
-
begin
|
76
|
-
album.update! singer: new_singer
|
77
|
-
raise StandardError, "Unexpected error: Updating the singer of an album should not be possible."
|
78
|
-
rescue ActiveRecord::StatementInvalid
|
79
|
-
puts ""
|
80
|
-
puts "Failed to update the singer of an album. This is expected."
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
def self.delete_singer_with_albums
|
85
|
-
# Deleting a singer that has albums is not possible, as the INTERLEAVE IN PARENT of albums is not marked with
|
86
|
-
# ON DELETE CASCADE.
|
87
|
-
singer = Album.all.sample.singer
|
88
|
-
begin
|
89
|
-
singer.delete
|
90
|
-
raise StandardError, "Unexpected error: Updating the singer of an album should not be possible."
|
91
|
-
rescue ActiveRecord::StatementInvalid
|
92
|
-
puts ""
|
93
|
-
puts "Failed to delete a singer that has #{singer.albums.count} albums. This is expected."
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
def self.delete_album_with_tracks
|
98
|
-
# Deleting an album with tracks is supported, as the INTERLEAVE IN PARENT relationship between tracks and albums is
|
99
|
-
# marked with ON DELETE CASCADE.
|
100
|
-
puts ""
|
101
|
-
puts "Total track count: #{Track.count}"
|
102
|
-
album = Track.all.sample.album
|
103
|
-
puts "Deleting album #{album.title} with #{album.tracks.count} tracks"
|
104
|
-
album.delete
|
105
|
-
puts "Total track count after deletion: #{Track.count}"
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
Application.run
|
@@ -1,44 +0,0 @@
|
|
1
|
-
# Copyright 2021 Google LLC
|
2
|
-
#
|
3
|
-
# Use of this source code is governed by an MIT-style
|
4
|
-
# license that can be found in the LICENSE file or at
|
5
|
-
# https://opensource.org/licenses/MIT.
|
6
|
-
|
7
|
-
class CreateTables < ActiveRecord::Migration[6.0]
|
8
|
-
def change
|
9
|
-
# Execute the entire migration as one DDL batch.
|
10
|
-
connection.ddl_batch do
|
11
|
-
create_table :singers, id: false do |t|
|
12
|
-
# Explicitly define the primary key with a custom name to prevent all primary key columns from being named `id`.
|
13
|
-
t.primary_key :singerid
|
14
|
-
t.string :first_name
|
15
|
-
t.string :last_name
|
16
|
-
end
|
17
|
-
|
18
|
-
create_table :albums, id: false do |t|
|
19
|
-
# Interleave the `albums` table in the parent table `singers`.
|
20
|
-
t.interleave_in :singers
|
21
|
-
t.primary_key :albumid
|
22
|
-
# `singerid` is defined as a `parent_key` which makes it a part of the primary key in the table definition, but
|
23
|
-
# it is not presented to ActiveRecord as part of the primary key, to prevent ActiveRecord from considering this
|
24
|
-
# to be an entity with a composite primary key (which is not supported by ActiveRecord).
|
25
|
-
t.parent_key :singerid
|
26
|
-
t.string :title
|
27
|
-
end
|
28
|
-
|
29
|
-
create_table :tracks, id: false do |t|
|
30
|
-
# Interleave the `tracks` table in the parent table `albums` and cascade delete all tracks that belong to an
|
31
|
-
# album when an album is deleted.
|
32
|
-
t.interleave_in :albums, :cascade
|
33
|
-
# `trackid` is considered the only primary key column by ActiveRecord.
|
34
|
-
t.primary_key :trackid
|
35
|
-
# `singerid` and `albumid` form the parent key of `tracks`. These are part of the primary key definition in the
|
36
|
-
# database, but are presented as parent keys to ActiveRecord.
|
37
|
-
t.parent_key :singerid
|
38
|
-
t.parent_key :albumid
|
39
|
-
t.string :title
|
40
|
-
t.numeric :duration
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
# This file is auto-generated from the current state of the database. Instead
|
2
|
-
# of editing this file, please use the migrations feature of Active Record to
|
3
|
-
# incrementally modify your database, and then regenerate this schema definition.
|
4
|
-
#
|
5
|
-
# This file is the source Rails uses to define your schema when running `rails
|
6
|
-
# db:schema:load`. When creating a new database, `rails db:schema:load` tends to
|
7
|
-
# be faster and is potentially less error prone than running all of your
|
8
|
-
# migrations from scratch. Old migrations may fail to apply correctly if those
|
9
|
-
# migrations use external dependencies or application code.
|
10
|
-
#
|
11
|
-
# It's strongly recommended that you check this file into your version control system.
|
12
|
-
|
13
|
-
ActiveRecord::Schema.define(version: 1) do
|
14
|
-
|
15
|
-
create_table "albums", primary_key: "albumid", force: :cascade do |t|
|
16
|
-
t.integer "singerid", limit: 8, null: false
|
17
|
-
t.string "title"
|
18
|
-
end
|
19
|
-
|
20
|
-
create_table "singers", primary_key: "singerid", force: :cascade do |t|
|
21
|
-
t.string "first_name"
|
22
|
-
t.string "last_name"
|
23
|
-
end
|
24
|
-
|
25
|
-
create_table "tracks", primary_key: "trackid", force: :cascade do |t|
|
26
|
-
t.integer "singerid", limit: 8, null: false
|
27
|
-
t.integer "albumid", limit: 8, null: false
|
28
|
-
t.string "title"
|
29
|
-
t.decimal "duration"
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
# Copyright 2021 Google LLC
|
2
|
-
#
|
3
|
-
# Use of this source code is governed by an MIT-style
|
4
|
-
# license that can be found in the LICENSE file or at
|
5
|
-
# https://opensource.org/licenses/MIT.
|
6
|
-
|
7
|
-
require_relative "../../config/environment.rb"
|
8
|
-
require_relative "../models/singer"
|
9
|
-
require_relative "../models/album"
|
10
|
-
require_relative "../models/track"
|
11
|
-
|
12
|
-
first_names = %w[Pete Alice John Ethel Trudy Naomi Wendy Ruben Thomas Elly]
|
13
|
-
last_names = %w[Wendelson Allison Peterson Johnson Henderson Ericsson Aronson Tennet Courtou]
|
14
|
-
|
15
|
-
adjectives = %w[daily happy blue generous cooked bad open]
|
16
|
-
nouns = %w[windows potatoes bank street tree glass bottle]
|
17
|
-
|
18
|
-
verbs = %w[operate waste package chew yield express polish stress slip want cough campaign cultivate report park refer]
|
19
|
-
adverbs = %w[directly right hopefully personally economically privately supposedly consequently fully later urgently]
|
20
|
-
|
21
|
-
durations = [3.14, 5.4, 3.3, 4.1, 5.0, 3.2, 3.0, 3.5, 4.0, 4.5, 5.5, 6.0]
|
22
|
-
|
23
|
-
# This ensures all the records are inserted using one read/write transaction that will use mutations instead of DML.
|
24
|
-
ActiveRecord::Base.transaction isolation: :buffered_mutations do
|
25
|
-
singers = []
|
26
|
-
5.times do
|
27
|
-
singers << Singer.create(first_name: first_names.sample, last_name: last_names.sample)
|
28
|
-
end
|
29
|
-
|
30
|
-
albums = []
|
31
|
-
20.times do
|
32
|
-
singer = singers.sample
|
33
|
-
albums << Album.create(title: "#{adjectives.sample} #{nouns.sample}", singer: singer)
|
34
|
-
end
|
35
|
-
|
36
|
-
200.times do
|
37
|
-
album = albums.sample
|
38
|
-
Track.create title: "#{verbs.sample} #{adverbs.sample}", duration: durations.sample, album: album
|
39
|
-
end
|
40
|
-
end
|