On Wednesday, February 6, 2019 at 7:41:20 AM UTC-8, Jason Landry wrote: > > Suppose we have a production database with a smaller sandboxed version of > that database (that may existing on another server). This is easy enough > to use with shards (after setting up the database option :servers hash with > the :sandbox connection info). So now we can query each shard: > > prod_users = User.select(:id) > sandbox_users = User.server(:sandbox).select(:id) > > This works fine; we end up with a dataset as expected. Associations also > follow the shard as expected. > > But there is one case where I'm trying to figure out the best way to solve > a particular problem when using `dataset_module` to create methods on the > dataset. Let me give a tortured but simple example (this could be done > easier with associations, but it illustrates the point). > > class User < Sequel::Model > dataset_module do > def team_member(team_names) > teams = Team.where(name: team_names).select_map(:id) > where(team_id: teams) > end > end > end > > This would work fine if I am using the default server, but if I hit the > :sandbox shard, the Team.where query hits the :default shard > > My thought for dealing with this is getting the current server within the > team_member method (it's available in the opts method), so everything > would look like this: > > class User < Sequel::Model > dataset_module do > def team_member(team_names) > shard = opts.fetch(:server, :default) > teams = Team.server(shard).where(name: team_names).select_map(:id) > > where(team_id: teams) > end > end > end > > Is this the best approach to ensuring models used within dataset modules > are on the same server as the dataset itself? > That is the approach I would recommend. Another possible approach is to use the server_block extension, as that allows all database access inside a block to default to a given server if a server hasn't been specified.
Thanks, Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/sequel-talk. For more options, visit https://groups.google.com/d/optout.
