If I have a horizontally sharded database table using a file_id
column as shard key, e.g. file_id from 1 to 1000 go to shard A, 1001 to 2000 go to shard B, …
Is there a ways for me to make rails automatically query from the right database shard when I write something like MyModel.find_by(file_id: 123)
? Is manually selecting the shard all the time with ActiveRecord::Base.connected_to
the only option?
I have looked into the shard selector middleware but it looks like it’s based on the request, is there a way for me to write an active record middleware to intercept, parse the query, and then connect to the right shard for each query? Or my best bet is to have an external proxy/gateway?