Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#
# frozen_string_literal: true

require_relative "../activerecord_spanner_mock_server/base_spanner_mock_server_test"
require_relative "./model_helper"
require_relative "../mock_server/spanner_mock_server"
require_relative "../test_helper"
Expand Down Expand Up @@ -408,6 +409,32 @@ def test_create_nested_associated_records
end
end

def test_create_child_records
select_singer_sql = "SELECT `singers`.* FROM `singers` WHERE `singers`.`singerid` = @p1 LIMIT @p2"
insert_album_sql = "INSERT INTO `albums` (`singerid`, `title`, `albumid`) VALUES (@p1, @p2, @p3)"
insert_track_sql = "INSERT INTO `tracks` (`trackid`, `singerid`, `albumid`) VALUES (@p1, @p2, @p3)"
@mock.put_statement_result select_singer_sql, TestInterleavedTables::create_random_singers_result(1)
@mock.put_statement_result insert_album_sql, StatementResult.new(1)
@mock.put_statement_result insert_track_sql, StatementResult.new(1)

singer = Singer.find 1

album = Album.new title: "Album 1", singer: singer
album.save!

# Create a new track for an existing album.
track = album.tracks.create trackid: 3

commit_request = @mock.requests.select { |req| req.is_a?(Google::Cloud::Spanner::V1::CommitRequest) }.first
assert_empty commit_request.mutations

insert_track_requests = @mock.requests.select { |req| req.is_a?(Google::Cloud::Spanner::V1::ExecuteSqlRequest) && req.sql == insert_track_sql }
assert_equal 1, insert_track_requests.length
assert_equal track.trackid, insert_track_requests[0].params["p1"].to_i
assert_equal singer.singerid, insert_track_requests[0].params["p2"].to_i
assert_equal album.albumid, insert_track_requests[0].params["p3"].to_i
end

def test_delete_all
# composite_primary_keys v12 generates a query with a WHERE EXISTS clause.
# This disables the use of mutations for delete_all.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class Album < ActiveRecord::Base
belongs_to :singer, foreign_key: :singerid

# `tracks` is defined as INTERLEAVE IN PARENT `albums`. The primary key of `albums` is (`singerid`, `albumid`).
has_many :tracks, foreign_key: [:singerid, :albumid]
has_many :tracks, :class_name => "Track", foreign_key: [:singerid, :albumid]
end
end
4 changes: 2 additions & 2 deletions test/activerecord_spanner_interleaved_table/models/track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module TestInterleavedTables
class Track < ActiveRecord::Base
self.primary_keys = :singerid, :albumid, :trackid

belongs_to :album, foreign_key: [:singerid, :albumid]
belongs_to :singer, foreign_key: :singerid
belongs_to :album, :class_name => "Album", foreign_key: [:singerid, :albumid]
belongs_to :singer, :class_name => "Singer", foreign_key: :singerid

def album=value
super
Expand Down
Loading