Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple databases are incompatible (since Rails/ActiveRecord 6) #643

Open
simoleone opened this issue Mar 23, 2021 · 0 comments
Open

Multiple databases are incompatible (since Rails/ActiveRecord 6) #643

simoleone opened this issue Mar 23, 2021 · 0 comments

Comments

@simoleone
Copy link

Using the new built-in multiple database support seems to be incompatible with Spring.

A minimal example that can reproduce the issue is below. For the sake of example, assume there is some really boring model called Account which can be created with just an email address.

In the below minimal RSpec test...

  • Both examples FAIL when run through Spring. The failure is ActiveRecord::ReadOnlyError due to Account.create!
  • Both examples PASS when not run through Spring.
RSpec.describe 'something' do
  let!(:stuff) { Account.create!(email: 'foo@example.com') }

  it 'should do things' do
    ActiveRecord::Base.connected_to(role: :reading) do
      expect(Account.count).to eq(1)
    end
  end

  it 'should do other things' do
    expect(Account.count).to eq(1)
  end
end

The relevant part of ApplicationRecord

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  connects_to database: { writing: :primary, reading: :primary_replica }
end

Our hypothetical Account is just an empty class.

class Account < ApplicationRecord
end

And the relevant section of database.yml, which is about as default as it comes.

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

test:
  primary: &primary
    <<: *default
    url: <%= ENV['TEST_DATABASE_URL'] %>
  primary_replica:
    replica: true
    <<: *primary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant