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

BackgroundAfter upgrading from Rails 6.1.7 to Rails 7.1.5, we encountered an issue where writes are be

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743982840a4538800.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信