Support for read slaves
See original GitHub issueHi @coleifer,
I’m using a master-slave(read-only) setup for our MySQL RDS. Currently, there’s no straightforward way to specify the database connection on the basis of SELECT
vs other query types. I stumbled upon your read_slave
module from playhouse extension v2.10.0 release of peewee.
Is it possible that I can use the same code to do it myself?
from peewee import *
class ReadSlaveModel(Model):
@classmethod
def _get_read_database(cls):
if not getattr(cls._meta, 'read_slaves', None):
return cls._meta.database
current_idx = getattr(cls, '_read_slave_idx', -1)
cls._read_slave_idx = (current_idx + 1) % len(cls._meta.read_slaves)
return cls._meta.read_slaves[cls._read_slave_idx]
@classmethod
def select(cls, *args, **kwargs):
query = super(ReadSlaveModel, cls).select(*args, **kwargs)
query.database = cls._get_read_database()
return query
@classmethod
def raw(cls, *args, **kwargs):
query = super(ReadSlaveModel, cls).raw(*args, **kwargs)
if query._sql.lower().startswith('select'):
query.database = cls._get_read_database()
return query
taken from https://github.com/coleifer/peewee/blob/2.10.2/playhouse/read_slave.py
Also, I’d like to know if there are any future plans to bring back the support for read slaves.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Literacy as Freedom
The issue of literacy among blacks during the Civil War was a complicated one. Before the 1830s there were few restrictions on teaching...
Read more >How Literacy Became a Powerful Weapon in the Fight to End ...
Despite the consequences, many enslaved people continued to learn to read. And numerous enslavers may have supported this. Many enslaved people ...
Read more >Reparations for Slavery - Constitutional Rights Foundation
Reparations for Slavery Reading · The claim for reparations is not against white Americans or even individual Americans. · The problems faced by...
Read more >About this Collection | Born in Slavery: Slave Narratives from ...
Born in Slavery: Slave Narratives from the Federal Writers' Project, 1936-1938 contains more than 2300 first-person accounts of slavery and 500 ...
Read more >Writing about "Slavery"? This Might Help
Senior slavery scholars of color community-sourced this short guide to share with and be used by editors, presses, museums, journalists, and curricular projects ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You basically create two connections, one for read and other for write. For read connection, you can specify the models to bind to the database.
Got it. Thanks