Commit 106d5c5
Support integer shard keys
Currently, `.connects_to(shards: ...)` coerces all of the shard keys
into symbols. Because an integer cannot be coerced into a symbol,
attempting to identify a shard with an integer will raise an exception:
```ruby
1.to_sym
(irb):1:in '<main>': undefined method 'to_sym' for an instance of Integer (NoMethodError)
```
I think it would be useful to support integers as shard keys along with
symbols. This would simplify shard switching when shards are identified
by integer columns in a database.
As an example, if there is an `accounts` table with a `shard` integer
column which identifies which shard that account's data lives on, this
currently would not work:
```ruby
account = Account.first
ActiveRecord::Base.connected_to(shard: account.shard) do
# Raises NoMethodError
end
```
A workaround would be to first coerce or serialize the integer into a
string but that's unideal:
```ruby
account = Account.first
ActiveRecord::Base.connected_to(shard: account.shard.to_s.to_sym) do
# ...
end
```
This makes a `.connects_to` change, coercing the shard key into a
symbol _unless_ the key is an integer. From there, passing a symbol or
integer to `connected_to(shard: ...)` should both work.
In the test, we call:
```ruby
ActiveRecord::Base.default_shard = 0
```
Normally, it would be `:default`. In that case we'd have to mix symbols
and integers in the `.connects_to:(shards:)` hash which is pretty ugly.1 parent cea7cae commit 106d5c5
File tree
3 files changed
+78
-1
lines changed- activerecord
- lib/active_record
- test/cases/connection_adapters
3 files changed
+78
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
1 | 16 | | |
2 | 17 | | |
3 | 18 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
| 103 | + | |
| 104 | + | |
104 | 105 | | |
105 | 106 | | |
106 | 107 | | |
| |||
Lines changed: 61 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
106 | 167 | | |
107 | 168 | | |
108 | 169 | | |
| |||
0 commit comments