{"push_id":2944664279,"size":1,"distinct_size":1,"ref":"refs/heads/connection-switching","head":"291b5589681e15a57d585b7217befad3e5993124","before":"86b876f527d5711b5df5aeb41365e50032f9d86b","commits":[{"sha":"291b5589681e15a57d585b7217befad3e5993124","author":{"name":"Eileen Uchitelle","email":"7a0559213618bb59f33e95efd260811728549345@gmail.com"},"message":"Basic API for connection switching\n\nThis PR adds the ability to 1) connect to multiple databases in a model,\nand 2) switch between those connections using a block.\n\nTo connect a model to a set of databases for writing and reading use\nthe following API. This API supercedes `establish_connection`. The\n`writing` and `reading` keys represent handler / mode names and\n`animals` and `animals_replica` represents the database key to look up\nthe configuration hash from.\n\n```\nclass AnimalsBase < ApplicationRecord\n  connects_to database: { writing: :animals, reading: :animals_replica }\nend\n```\n\nInside the application - outside the model declaration - we can switch\nconnections with a block call to `connected_to`.\n\nIf we want to connect to a db that isn't default (ie readonly_slow) we\ncan connect like this:\n\nOutside the model we may want to connect to a new database (one that is\nnot in the default writing/reading set) - for example a slow replica for\nmaking slow queries. To do this we have the `connected_to` method that\ntakes a `database` hash that matches the signature of `connects_to`. The\n`connected_to` method also takes a block.\n\n```\nAcitveRecord::Base.connected_to(database: { slow_readonly: :primary_replica_slow }) do\n  ModelInPrimary.do_something_thats_slow\nend\n```\n\nFor models that are already loaded and connections that are already\nconnected, `connected_to` doesn't need to pass in a `database` because\nyou may want to run queries against multiple databases using a specific\nmode/handler.\n\nIn this case `connected_to` can take a `handler` and use that to swap on\nthe connection passed. This simplies queries - and matches how we do it\nin GitHub. Once you're connected to the database you don't need to\nre-connect, we assume the connection is in the pool and simply pass the\nhandler we'd like to swap on.\n\n```\nActiveRecord::Base.connected_to(hander: :reading) do\n  Dog.read_something_from_dog\n  ModelInPrimary.do_something_from_model_in_primary\nend\n```","distinct":true,"url":"https://api.github.com/repos/eileencodes/rails/commits/291b5589681e15a57d585b7217befad3e5993124"}]}
{"actor":{"display_login":"eileencodes"}}