question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for read slaves

See original GitHub issue

Hi @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:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
aniljaiswalcommented, May 14, 2021

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.

0reactions
JishnuRameshcommented, May 21, 2021

Got it. Thanks

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found