Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple databases with peewee

I'm writing a "multi tenant" application. It's going to be hosted on different subdomains, and based on which subdomain it's hosted, it should use a different database.

Is it possible to define, in execution time, which database peewee should be using? If I were using django, I'd just write a router that takes care of it, but I haven't found anything similar on peewee.

Am I missing something?

Thanks!

PS: A hack like this How to query several similar databases using Peewee?, where you need to know beforehand which class to invoke wouldn't work fine in my scenario

like image 373
g3rv4 Avatar asked Dec 22 '14 20:12

g3rv4


2 Answers

You can also take a look at the ReadSlave module for an example of changing databases at run-time.

Code:

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
like image 183
coleifer Avatar answered Sep 16 '22 22:09

coleifer


Peewee provides also the possibility to use DB Proxy, where you can switch the DB easily. Peewee Documentation

database_proxy = Proxy()  # Create a proxy for our db.

class BaseModel(Model):
    class Meta:
        database = database_proxy  # Use proxy for our DB.

class User(BaseModel):
    username = CharField()

# Based on configuration, use a different database.
if app.config['DEBUG']:
    database = SqliteDatabase('local.db')
elif app.config['TESTING']:
    database = SqliteDatabase(':memory:')
else:
    database = PostgresqlDatabase('mega_production_db')

# Configure our proxy to use the db we specified in config.
database_proxy.initialize(database)
like image 32
Roman S. Avatar answered Sep 20 '22 22:09

Roman S.