rails/activerecord/lib
eileencodes 0d41bfd3cc
Set default_shard from connects_to hash
If an application is using sharding, they may not want to use `default`
as the `default_shard`. Unfortunately Rails expects there to be a shard
named `default` for certain actions internally. This leads to some
errors on boot and the application is left manually setting
`default_shard=` in their model or updating their shards in
`connects_to` to name `shard_one` to `default`. Neither are a great
solution, especially if Rails can do this for you. Changes to Active
Record are:

* Simplify `connects_to` by merging `database` into `shards` kwarg so we
can do a single loop through provided options.
* Set the `self.default_shard` to the first keys in the shards kwarg.
* Add a test for this behavior
* Update existing test that wasn't testing this to use `default`. I
could have left this test but it really messes with connections in the
other tests and since this isn't testing shard behavior specifically, I
updated it to use `default` as the default shard name.

This is a slight change in behavior from existing applications but
arguably this is fixing a bug because without this an application won't
boot. I originally thought that this would require a huge refactoring to
fix but realized that it makes a lot of sense to take the first shard as
they default. They should all have the same schema so we can assume it's
fine to take the first one.

Fixes: #45390
2023-05-31 12:52:12 -04:00
..
active_record Set default_shard from connects_to hash 2023-05-31 12:52:12 -04:00
arel Adds Arel::Nodes::Cte for use in WITH expressions 2023-05-24 19:19:21 -04:00
rails/generators Simplify the implementation to register the correct source_path for the migration template 2023-05-24 22:54:12 +00:00
active_record.rb Implement ActiveRecord.disconnect_all! to close all connections 2023-05-19 13:19:31 +02:00
arel.rb Fix a code block in the Arel documentation 2023-05-06 18:57:12 -07:00