Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests using PooledPostgresqlExtDatabase #237

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions python/sdssdb/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from sqlalchemy.orm import scoped_session, sessionmaker

import peewee
from peewee import OperationalError, PostgresqlDatabase
from peewee import OperationalError
from playhouse.pool import PooledPostgresqlExtDatabase
from playhouse.postgres_ext import ArrayField
from playhouse.reflection import Introspector, UnknownField

Expand Down Expand Up @@ -357,7 +358,7 @@ def post_connect(self):
pass


class PeeweeDatabaseConnection(DatabaseConnection, PostgresqlDatabase):
class PeeweeDatabaseConnection(DatabaseConnection, PooledPostgresqlExtDatabase):
"""Peewee database connection implementation.

Attributes
Expand All @@ -376,8 +377,12 @@ def __init__(self, *args, **kwargs):
self._metadata = {}

autorollback = kwargs.pop('autorollback', True)
max_connections = kwargs.pop('max_connections', 10)
stale_timeout = kwargs.pop('stale_timeout', None)

PostgresqlDatabase.__init__(self, None, autorollback=autorollback)
PooledPostgresqlExtDatabase.__init__(self, None, autorollback=autorollback,
stale_timeout=stale_timeout,
max_connections=max_connections)
DatabaseConnection.__init__(self, *args, **kwargs)

@property
Expand Down Expand Up @@ -408,16 +413,16 @@ def _conn(self, dbname, silent_on_fail=False, **params):
except pgpasslib.FileNotFound:
params['password'] = None

PostgresqlDatabase.init(self, dbname, **params)
PooledPostgresqlExtDatabase.init(self, dbname, **params)
self._metadata = {}

try:
PostgresqlDatabase.connect(self)
PooledPostgresqlExtDatabase.connect(self)
self.dbname = dbname
except OperationalError as ee:
if not silent_on_fail:
log.warning(f'failed to connect to database {self.database!r}: {ee}')
PostgresqlDatabase.init(self, None)
PooledPostgresqlExtDatabase.init(self, None)

if self.is_connection_usable() and self.auto_reflect:
with self.atomic():
Expand Down
Loading