Commit 58c27c9
Alexei Șerșun
Add support for horizontal sharding for Rails 6.1+
Since Rails 6.1, it is possible for a model to connect to multiple
databases. A minimal example of such application is:
```ruby
class ApplicationRecord < ActiveRecord::Base
connects_to shard: {
defaul: { writing: :primary_db },
shard_one: { writing: :secondary_db }
}
end
class User < ApplicationRecord; end
ApplicationRecord.connected_to(shard: :shard_one, role: :writing) do
User.create!(...) # creates users in secondary_db DB
end
ApplicationRecord.connection_handler.connection_pools.map { |pool|
pool.db_config.configuration_hash[:database] } # [:primary_db,
:secondary_db]
```
With support for multiple databases for a model, one would have something like this in tests:
```ruby
DatabaseCleaner[:active_record, db: ApplicationRecord]
DatabaseCleaner.start
DatabaseCleaner.clean
```
In `.clean`, however, the bug occurs: it doesn't actually
delete or truncate data from the :secondary_db.
To fix the bug, DatabaseCleaner should iterate through _all_ connection pools the model is connected to.1 parent 0b0bfc4 commit 58c27c9
2 files changed
Lines changed: 25 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
38 | 41 | | |
39 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
40 | 46 | | |
41 | 47 | | |
42 | 48 | | |
| |||
0 commit comments