from mydb.connection import Connection
db = Connection("mysql://youngking@localhost:13306/mydatabase")
for article in db.query("SELECT * FROM articles"):
print article.title
from mydb.router import ConnectionRouter
from mydb.proxy import DBProxy
router = ConnectionRouter(["myapp.somewhere.MasterSlaveRouter"])
db = DBProxy(router)
for article in db.query("SELECT * FROM articles"):
print article.title
A database Router is a class that provides up to two methods:
db_for_read(statement, **hints)
Suggest the database that should be used for read operations for ``SELECT`` statement.
If a database operation is able to provide any additional information that might assist in selecting a database, it will be provided in the hints dictionary. Details on valid hints are provided below.
Returns None if there is no suggestion.
db_for_write(model, **hints)
Suggest the database that should be used for writes of statements except ``SELECT``.
If a database operation is able to provide any additional information that might assist in selecting a database, it will be provided in the hints dictionary. Details on valid hints are provided below.
There is an example in tests/test_router.py.
A router doesn’t have to provide all these methods – it may omit one or more of them. If one of the methods is omitted, mydb will skip that router when performing the relevant check.
In mydb.router there are some default routers, where you can inherit from and overwriten them.
Router that sends all reads to a slave, all writes to default.
Send reads to slaves in round-robin.
Send all writes to the master.
Router that sends reads to master iff a certain flag is set. Writes always go to master.
Typically, we set a cookie in middleware when the request is a POST and give it a max age that’s certain to be longer than the replication lag. The flag comes from that cookie.
Send reads to slaves in round-robin unless this thread is “stuck” to the master.