googleapis / googleapis/ruby-spanner-activerecord

BufferedMutation breaks after_xxx hooks.

Offen
#177 2 Kommentare 0 Reaktionen 2 zugewiesene Personen Auf GitHub ansehen

@olavloite arbeitet bereits daran.

Seit 02.6.2022.

api: spanner priority: p2 type: bug
Vorherrschende Sprache
Ruby
Sterne
105
Forks
37
Ø Merge
10 Std. 2 Min.
Gemergte PRs (30 T.)
5

Beschreibung

Problem

activerecord-spanner-adapter breaks ActiveRecord's after_xxx hooks.
Perhaps this is a problem caused by BufferedMutation.

Like codes shows in bellow, I am unable to retrieve the record in the after_save block immediately after insertion.

I doubt that this similar problem is also happening in after_update and after_create.

Solution(?)

I do not have a idea to monkey-patch for this problem.
My only solution was to remove require "activerecord_spanner_adapter/base" from lib.
https://github.com/googleapis/ruby-spanner-activerecord/blob/07f5b6cbfc701848e3aebf4cb195d267d9e52fb8/lib/active_record/connection_adapters/spanner_adapter.rb#L23

Reproduce

sample code:

require "bundler/inline"

gemfile do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem 'activerecord', '6.1.6'
  gem 'activerecord-spanner-adapter'
  # gem 'activerecord-spanner-adapter',
  #   git: 'https://github.com/googleapis/ruby-spanner-activerecord',
  #   ref: '2d30298fd0dedb447ce9ff089df43c65131dc9fe' # composite-pk branch
end

require "active_record"
require "activerecord-spanner-adapter"
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new project: "test-project", emulator_host: "localhost:9010"
job = spanner.create_instance "test-instance",
                              name: "Test Instance",
                              config: "emulator-config",
                              nodes: 1
job.wait_until_done!

instance = spanner.instance "test-instance"
job = instance.create_database "testdb", statements: []
job.wait_until_done!

ActiveRecord::Base.establish_connection(project: "test-project", adapter: "spanner", instance: "test-instance", database: "testdb", emulator_host: "localhost:9010")

ActiveRecord::Schema.define do
  create_table :singers, force: true, id: false do |t|
    t.integer :singer_id, null: false, primary_key: true
    t.column :first_name, :string, limit: 200
    t.string :last_name
  end
end

class Singer < ActiveRecord::Base
  after_save do
    Singer.print_name
  end

  def self.print_name
    puts "first name => #{Singer.first&.first_name}"
  end
end

Singer.create(first_name: "John", last_name: "Doe")
Singer.print_name
singer = Singer.first
singer.first_name = "Jane"
singer.save!

output:

-- create_table(:singers, {:force=>true, :id=>false})
   -> 0.0135s
first name =>           <=== can't see created record in after_save
first name => John
first name => Jane

This is expected output by using MySQL or remove require "activerecord_spanner_adapter/base" from lib.

-- create_table(:singers, {:force=>true, :id=>false})
   -> 0.0172s
first name => John
first name => John
first name => Jane

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.