最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ruby - Writes Going to the Wrong Shard After Upgrading to Rails 7.1.5 - Stack Overflow

programmeradmin1浏览0评论

Background After upgrading from Rails 6.1.7 to Rails 7.1.5, we encountered an issue where writes are being executed on the wrong shard, despite explicitly specifying the target shard.

Expected Behavior Executing queries within a MyModel::connected_to(role: :writing, shard: :shard_name) block should direct all operations to the specified shard.

Writes should always occur on the correct shard as defined in database.yml.

Observed Behavior Writes are going to the incorrect shard, even when explicitly switching connections.

In Rails 6.1.7, shard switching worked correctly.

Reversing the order of shards in connects_to causes writes to go to the other shard instead.

Reproduction Steps Rails Version: 7.1.5

Database Adapter: PostgreSQL

Configuration (database.yml)

production:
primary:
adapter: postgresql
database: dashboard
username: user
password: password

shard_one:
writing:
adapter: postgresql
database: serve
username: user
password: password

shard_two:
writing:
adapter: postgresql
database: serve
username: user
password: password

Model Configuration (ServeRecord)

class ServeRecord < ActiveRecord::Base
self.abstract_class = true
connects_to shards: {
shard_one: { writing: :shard_one, reading: :shard_one },
shard_two: { writing: :shard_two, reading: :shard_two }
}
end

Unexpected Behavior in Shard Switching

MyModel.connected_to(role: :writing, shard: :shard_one) do
puts "Current DB: #{ServeRecord.connection.current_database}" # Outputs correct DB name
User.create!(name: "Test User") # Unexpectedly writes to shard_two
end

Expected: The User record should be created in shard_one.

Observed: The record is incorrectly written to shard_two.

Questions / Request for Help Is this a bug in Rails 7.1.5’? Are there any additional configurations required in Rails 7+ to ensure shard switching behaves correctly?

Would appreciate any guidance or insights.

发布评论

评论列表(0)

  1. 暂无评论