Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 8 additions & 10 deletions peewee_async/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
from peewee_async.aio_model import AioModel, aio_prefetch
from peewee_async.connection import connection_context
from peewee_async.databases import (
PooledMySQLDatabase,
PooledPostgresqlDatabase,
PooledPostgresqlExtDatabase,
PsycopgDatabase,
MySQLDatabase,
PostgresqlDatabase,
Psycopg3Database,
)
from peewee_async.pool import MysqlPoolBackend, PostgresqlPoolBackend
from peewee_async.transactions import Transaction
Expand All @@ -34,8 +33,8 @@

__all__ = [
"PooledPostgresqlDatabase",
"PooledPostgresqlExtDatabase",
"PooledMySQLDatabase",
"PostgresqlDatabase",
"MySQLDatabase",
"Transaction",
"AioModel",
"aio_prefetch",
Expand All @@ -44,7 +43,6 @@
"MysqlPoolBackend",
]

register_database(PooledPostgresqlDatabase, "postgres+pool+async", "postgresql+pool+async")
register_database(PooledPostgresqlExtDatabase, "postgresext+pool+async", "postgresqlext+pool+async")
register_database(PsycopgDatabase, "psycopg+pool+async", "psycopg+pool+async")
register_database(PooledMySQLDatabase, "mysql+pool+async")
register_database(PostgresqlDatabase, "postgres+pool+async", "postgresql+pool+async")
register_database(Psycopg3Database, "psycopg+pool+async", "psycopg+pool+async")
register_database(MySQLDatabase, "mysql+pool+async")
58 changes: 14 additions & 44 deletions peewee_async/databases.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import contextlib
import warnings
from collections.abc import AsyncIterator, Iterator
from contextlib import AbstractAsyncContextManager
from typing import Any

import peewee
from playhouse import postgres_ext as ext
from playhouse.psycopg3_ext import Psycopg3Database

from .connection import ConnectionContextManager, connection_context
from .pool import MysqlPoolBackend, PoolBackend, PostgresqlPoolBackend, PsycopgPoolBackend
Expand All @@ -22,17 +20,16 @@ class AioDatabase(peewee.Database):

Example::

database = PooledPostgresqlExtDatabase(
database = Psycopg3Database(
'database': 'postgres',
'host': '127.0.0.1',
'port':5432,
'port': 5432,
'password': 'postgres',
'user': 'postgres',
'pool_params': {
"minsize": 0,
"maxsize": 5,
"timeout": 30,
'pool_recycle': 1.5
"min_size": 0,
"max_size": 5,
'max_lifetime': 15
}
)

Expand All @@ -54,18 +51,6 @@ def init_pool_params_defaults(self) -> None:

def init_pool_params(self) -> None:
self.init_pool_params_defaults()
if "min_connections" in self.connect_params or "max_connections" in self.connect_params:
warnings.warn(
"`min_connections` and `max_connections` are deprecated, use `pool_params` instead.",
DeprecationWarning,
stacklevel=2,
)
self.pool_params.update(
{
"minsize": self.connect_params.pop("min_connections", 1),
"maxsize": self.connect_params.pop("max_connections", 20),
}
)
pool_params = self.connect_params.pop("pool_params", {})
self.pool_params.update(pool_params)
self.pool_params.update(self.connect_params)
Expand Down Expand Up @@ -194,13 +179,13 @@ async def aio_execute(self, query: Any, fetch_results: FetchResults | None = Non
return await self.aio_execute_sql(sql, params, fetch_results=fetch_results)


class PsycopgDatabase(AioDatabase, Psycopg3Database):
"""Extension for `peewee.PostgresqlDatabase` providing extra methods
class Psycopg3Database(AioDatabase, ext.Psycopg3Database):
"""Extension for `playhouse.Psycopg3Database` providing extra methods
for managing async connection based on psycopg3 pool backend.

Example::

database = PsycopgDatabase(
database = Psycopg3Database(
'database': 'postgres',
'host': '127.0.0.1',
'port': 5432,
Expand All @@ -225,14 +210,14 @@ def init(self, database: str | None, **kwargs: Any) -> None:
super().init(database, **kwargs)


class PooledPostgresqlDatabase(AioDatabase, peewee.PostgresqlDatabase):
"""Extension for `peewee.PostgresqlDatabase` providing extra methods
class PostgresqlDatabase(AioDatabase, ext.PostgresqlExtDatabase):
"""Extension for `playhouse.PostgresqlDatabase` providing extra methods
for managing async connection based on aiopg pool backend.


Example::

database = PooledPostgresqlExtDatabase(
database = PostgresqlDatabase(
'database': 'postgres',
'host': '127.0.0.1',
'port':5432,
Expand All @@ -253,36 +238,21 @@ class PooledPostgresqlDatabase(AioDatabase, peewee.PostgresqlDatabase):
pool_backend_cls = PostgresqlPoolBackend

def init_pool_params_defaults(self) -> None:
self.pool_params.update({"enable_json": False, "enable_hstore": False})
self.pool_params.update({"enable_json": True, "enable_hstore": self._register_hstore})

def init(self, database: str | None, **kwargs: Any) -> None:
if not aiopg:
raise Exception("Error, aiopg is not installed!")
super().init(database, **kwargs)


class PooledPostgresqlExtDatabase(PooledPostgresqlDatabase, ext.PostgresqlExtDatabase):
"""PosgtreSQL database extended driver providing **single drop-in sync**
connection and **async connections pool** interface based on aiopg pool backend.

JSON fields support is enabled by default, HStore supports is disabled by
default, but can be enabled through pool_params or with ``register_hstore=False`` argument.

See also:
https://peewee.readthedocs.io/en/latest/peewee/playhouse.html#PostgresqlExtDatabase
"""

def init_pool_params_defaults(self) -> None:
self.pool_params.update({"enable_json": True, "enable_hstore": self._register_hstore})


class PooledMySQLDatabase(AioDatabase, peewee.MySQLDatabase):
class MySQLDatabase(AioDatabase, peewee.MySQLDatabase):
"""MySQL database driver providing **single drop-in sync**
connection and **async connections pool** interface.

Example::

database = PooledMySQLDatabase(
database = MySQLDatabase(
'database': 'mysql',
'host': '127.0.0.1',
'port': 3306,
Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ authors = [
requires-python = ">=3.10"
readme = "README.md"
dependencies = [
"peewee>=3.15.4,<4",
"peewee>=4,<5",
"typing-extensions>=4.12.2"
]

Expand All @@ -19,12 +19,12 @@ postgresql = [
"aiopg>=1.4.0",
]
mysql = [
"aiomysql>=0.2.0",
"aiomysql>=0.3.2",
"cryptography>=46.0.5"
]
psycopg = [
"psycopg>=3.2.0",
"psycopg-pool>=3.2.0"
"psycopg>=3.3.0",
"psycopg-pool>=3.3.0"
]
docs = [
"Sphinx>=8.1.3",
Expand Down
3 changes: 1 addition & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ async def db(request: pytest.FixtureRequest) -> AsyncGenerator[AioDatabase, None


PG_DBS = [
"postgres-pool",
"postgres-pool-ext",
"aiopg-pool",
"psycopg-pool",
]

Expand Down
10 changes: 4 additions & 6 deletions tests/db_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@
}

DB_DEFAULTS = {
"postgres-pool": PG_DEFAULTS,
"postgres-pool-ext": PG_DEFAULTS,
"aiopg-pool": PG_DEFAULTS,
"psycopg-pool": PSYCOPG_DEFAULTS,
"mysql-pool": MYSQL_DEFAULTS,
}

DB_CLASSES = {
"postgres-pool": peewee_async.PooledPostgresqlDatabase,
"postgres-pool-ext": peewee_async.PooledPostgresqlExtDatabase,
"psycopg-pool": peewee_async.PsycopgDatabase,
"mysql-pool": peewee_async.PooledMySQLDatabase,
"aiopg-pool": peewee_async.PostgresqlDatabase,
"psycopg-pool": peewee_async.Psycopg3Database,
"mysql-pool": peewee_async.MySQLDatabase,
}
29 changes: 1 addition & 28 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,6 @@ async def test_deferred_init(db_name: str) -> None:
await database.aio_close()


@pytest.mark.parametrize("db_name", ["postgres-pool", "postgres-pool-ext", "mysql-pool"])
async def test_deprecated_min_max_connections_param(db_name: str) -> None:
default_params = DB_DEFAULTS[db_name].copy()
del default_params["pool_params"]
default_params["min_connections"] = 1
default_params["max_connections"] = 3
db_cls = DB_CLASSES[db_name]
database = db_cls(**default_params)
await database.aio_connect()

assert database.pool_backend.pool.minsize == 1 # type: ignore
assert database.pool_backend.pool.maxsize == 3 # type: ignore

await database.aio_close()


@dbs_mysql
async def test_mysql_params(db: AioDatabase) -> None:
async with db.aio_connection() as connection_1:
Expand All @@ -101,18 +85,7 @@ async def test_mysql_params(db: AioDatabase) -> None:
assert db.pool_backend.pool.maxsize == 5 # type: ignore


@pytest.mark.parametrize("db", ["postgres-pool"], indirect=["db"])
async def test_pg_json_hstore__params(db: AioDatabase) -> None:
await db.aio_connect()
assert db.pool_backend.pool._enable_json is False # type: ignore
assert db.pool_backend.pool._enable_hstore is False # type: ignore
assert db.pool_backend.pool._timeout == 30 # type: ignore
assert db.pool_backend.pool._recycle == 1.5 # type: ignore
assert db.pool_backend.pool.minsize == 0 # type: ignore
assert db.pool_backend.pool.maxsize == 5 # type: ignore


@pytest.mark.parametrize("db", ["postgres-pool-ext"], indirect=["db"])
@pytest.mark.parametrize("db", ["aiopg-pool"], indirect=["db"])
async def test_pg_ext_json_hstore__params(db: AioDatabase) -> None:
await db.aio_connect()
assert db.pool_backend.pool._enable_json is True # type: ignore
Expand Down