Search code examples
pythonmysqlpeewee

How can I use peewee db_url.connect() to generate a connection with RetryOperationalError?


I default generate db connection using the PooledMySQLDatabase with RetryOperationalError like this:

from playhouse.shortcuts import RetryOperationalError
from playhouse.pool import PooledMySQLDatabase


class MySQLRetryDB(RetryOperationalError, PooledMySQLDatabase):
    pass


mysql_db = MySQLRetryDB('test_db')

Now I want to use db_url.connect to generate the db connection like this:

from playhouse.db_url import connect
mysql_db = connect('mysql+pool://root:root@localhost:3306/test_db')

but I can't find a way to use the RetryOperationalError, How can I use connect() to generate a connection with RetryOperationalError?


Solution

  • My solution is hacking the db_url.schemes

    class MySQLRetryDB(RetryOperationalError, PooledMySQLDatabase):
        pass
    
    
    schemes['mysql+pool+retry'] = MySQLRetryDB
    
    
    mysql_db = connect('mysql+pool+retry://root:root@localhost:3306/test_db')