Skip to content

Commit

Permalink
chore: deprecate unusable databases
Browse files Browse the repository at this point in the history
  • Loading branch information
kalombos committed Jun 23, 2024
1 parent 541fa9d commit 661f474
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 119 deletions.
173 changes: 127 additions & 46 deletions peewee_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from typing import Optional, Type

import peewee
from playhouse import postgres_ext as ext
from playhouse.db_url import register_database
from peewee_async_compat import Manager, count, execute, prefetch, scalar, savepoint, atomic, transaction
from peewee_async_compat import _patch_query_with_compat_methods
Expand Down Expand Up @@ -515,40 +516,57 @@ async def last_insert_id_async(self, cursor):
return cursor.lastrowid


class PostgresqlDatabase(AioPostgresqlMixin, peewee.PostgresqlDatabase):
"""PosgreSQL database driver providing **single drop-in sync** connection
and **single async connection** interface.
class PooledPostgresqlDatabase(AioPostgresqlMixin, peewee.PostgresqlDatabase):
"""PosgreSQL database driver providing **single drop-in sync**
connection and **async connections pool** interface.
:param max_connections: connections pool size
Example::
database = PostgresqlDatabase('test')
database = PooledPostgresqlDatabase('test', max_connections=20)
See also:
http://peewee.readthedocs.io/en/latest/peewee/api.html#PostgresqlDatabase
"""
def init(self, database, **kwargs):
self.min_connections = 1
self.max_connections = 1
self.min_connections = kwargs.pop('min_connections', 1)
self.max_connections = kwargs.pop('max_connections', 20)
connection_timeout = kwargs.pop('connection_timeout', None)
if connection_timeout is not None:
warnings.warn(
"`connection_timeout` is deprecated, use `connect_timeout` instead.",
DeprecationWarning
)
kwargs['connect_timeout'] = connection_timeout
super().init(database, **kwargs)
self.init_async()


register_database(PostgresqlDatabase, 'postgres+async', 'postgresql+async')
register_database(PooledPostgresqlDatabase, 'postgres+pool+async', 'postgresql+pool+async')


class PooledPostgresqlDatabase(AioPostgresqlMixin, peewee.PostgresqlDatabase):
"""PosgreSQL database driver providing **single drop-in sync**
class PooledPostgresqlExtDatabase(
AioPostgresqlMixin,
ext.PostgresqlExtDatabase
):
"""PosgreSQL database extended driver providing **single drop-in sync**
connection and **async connections pool** interface.
JSON fields support is always enabled, HStore supports is enabled by
default, but can be disabled with ``register_hstore=False`` argument.
:param max_connections: connections pool size
Example::
database = PooledPostgresqlDatabase('test', max_connections=20)
database = PooledPostgresqlExtDatabase('test', register_hstore=False,
max_connections=20)
See also:
http://peewee.readthedocs.io/en/latest/peewee/api.html#PostgresqlDatabase
https://peewee.readthedocs.io/en/latest/peewee/playhouse.html#PostgresqlExtDatabase
"""

def init(self, database, **kwargs):
self.min_connections = kwargs.pop('min_connections', 1)
self.max_connections = kwargs.pop('max_connections', 20)
Expand All @@ -560,10 +578,11 @@ def init(self, database, **kwargs):
)
kwargs['connect_timeout'] = connection_timeout
super().init(database, **kwargs)
self.init_async()


register_database(PooledPostgresqlDatabase, 'postgres+pool+async', 'postgresql+pool+async')
self.init_async(
enable_json=True,
enable_hstore=self._register_hstore
)
register_database(PooledPostgresqlExtDatabase, 'postgresext+pool+async', 'postgresqlext+pool+async')


#########
Expand All @@ -583,13 +602,15 @@ async def create(self):
)


class MySQLDatabase(AioDatabase, peewee.MySQLDatabase):
"""MySQL database driver providing **single drop-in sync** connection
and **single async connection** interface.
class PooledMySQLDatabase(AioDatabase, peewee.MySQLDatabase):
"""MySQL database driver providing **single drop-in sync**
connection and **async connections pool** interface.
:param max_connections: connections pool size
Example::
database = MySQLDatabase('test')
database = PooledMySQLDatabase('test', max_connections=10)
See also:
http://peewee.readthedocs.io/en/latest/peewee/api.html#MySQLDatabase
Expand All @@ -602,8 +623,8 @@ class MySQLDatabase(AioDatabase, peewee.MySQLDatabase):
def init(self, database, **kwargs):
if not aiomysql:
raise Exception("Error, aiomysql is not installed!")
self.min_connections = 1
self.max_connections = 1
self.min_connections = kwargs.pop('min_connections', 1)
self.max_connections = kwargs.pop('max_connections', 20)
super().init(database, **kwargs)

@property
Expand All @@ -623,34 +644,10 @@ async def last_insert_id_async(self, cursor):
"""
return cursor.lastrowid


register_database(MySQLDatabase, 'mysql+async')


class PooledMySQLDatabase(MySQLDatabase):
"""MySQL database driver providing **single drop-in sync**
connection and **async connections pool** interface.
:param max_connections: connections pool size
Example::
database = MySQLDatabase('test', max_connections=10)
See also:
http://peewee.readthedocs.io/en/latest/peewee/api.html#MySQLDatabase
"""
def init(self, database, **kwargs):
min_connections = kwargs.pop('min_connections', 1)
max_connections = kwargs.pop('max_connections', 10)
super().init(database, **kwargs)
self.min_connections = min_connections
self.max_connections = max_connections


register_database(PooledMySQLDatabase, 'mysql+pool+async')



async def aio_prefetch(sq, *subqueries, prefetch_type):
"""Asynchronous version of the `prefetch()` from peewee."""
if not subqueries:
Expand Down Expand Up @@ -940,3 +937,87 @@ async def aio_get_or_create(cls, **kwargs):
return await query.aio_get(), False
except cls.DoesNotExist:
raise exc


# DEPRECATED Databases


class PostgresqlDatabase(AioPostgresqlMixin, peewee.PostgresqlDatabase):
"""PosgreSQL database driver providing **single drop-in sync** connection
and **single async connection** interface.
Example::
database = PostgresqlDatabase('test')
See also:
http://peewee.readthedocs.io/en/latest/peewee/api.html#PostgresqlDatabase
"""
def init(self, database, **kwargs):
warnings.warn(
"`PostgresqlDatabase` is deprecated, use `PooledPostgresqlDatabase` instead.",
DeprecationWarning
)
self.min_connections = 1
self.max_connections = 1
super().init(database, **kwargs)
self.init_async()


register_database(PostgresqlDatabase, 'postgres+async', 'postgresql+async')


class MySQLDatabase(PooledMySQLDatabase):
"""MySQL database driver providing **single drop-in sync** connection
and **single async connection** interface.
Example::
database = MySQLDatabase('test')
See also:
http://peewee.readthedocs.io/en/latest/peewee/api.html#MySQLDatabase
"""
def init(self, database, **kwargs):
warnings.warn(
"`MySQLDatabase` is deprecated, use `PooledMySQLDatabase` instead.",
DeprecationWarning
)
super().init(database, **kwargs)
self.min_connections = 1
self.max_connections = 1


register_database(MySQLDatabase, 'mysql+async')


class PostgresqlExtDatabase(AioPostgresqlMixin, ext.PostgresqlExtDatabase):
"""PosgreSQL database extended driver providing **single drop-in sync**
connection and **single async connection** interface.
JSON fields support is always enabled, HStore supports is enabled by
default, but can be disabled with ``register_hstore=False`` argument.
Example::
database = PostgresqlExtDatabase('test', register_hstore=False)
See also:
https://peewee.readthedocs.io/en/latest/peewee/playhouse.html#PostgresqlExtDatabase
"""

def init(self, database, **kwargs):
warnings.warn(
"`PostgresqlExtDatabase` is deprecated, use `PooledPostgresqlExtDatabase` instead.",
DeprecationWarning
)
self.min_connections = 1
self.max_connections = 1
super().init(database, **kwargs)
self.init_async(
enable_json=True,
enable_hstore=self._register_hstore
)


register_database(PostgresqlExtDatabase, 'postgresext+async', 'postgresqlext+async')
78 changes: 5 additions & 73 deletions peewee_asyncext.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,77 +14,9 @@
"""
import warnings
from peewee_async import AioPostgresqlMixin, PooledPostgresqlExtDatabase, PostgresqlExtDatabase

from playhouse import postgres_ext as ext
from playhouse.db_url import register_database

from peewee_async import AioPostgresqlMixin


class PostgresqlExtDatabase(AioPostgresqlMixin, ext.PostgresqlExtDatabase):
"""PosgreSQL database extended driver providing **single drop-in sync**
connection and **single async connection** interface.
JSON fields support is always enabled, HStore supports is enabled by
default, but can be disabled with ``register_hstore=False`` argument.
Example::
database = PostgresqlExtDatabase('test', register_hstore=False)
See also:
https://peewee.readthedocs.io/en/latest/peewee/playhouse.html#PostgresqlExtDatabase
"""

def init(self, database, **kwargs):
self.min_connections = 1
self.max_connections = 1
super().init(database, **kwargs)
self.init_async(
enable_json=True,
enable_hstore=self._register_hstore
)


register_database(PostgresqlExtDatabase, 'postgresext+async', 'postgresqlext+async')


class PooledPostgresqlExtDatabase(
AioPostgresqlMixin,
ext.PostgresqlExtDatabase
):
"""PosgreSQL database extended driver providing **single drop-in sync**
connection and **async connections pool** interface.
JSON fields support is always enabled, HStore supports is enabled by
default, but can be disabled with ``register_hstore=False`` argument.
:param max_connections: connections pool size
Example::
database = PooledPostgresqlExtDatabase('test', register_hstore=False,
max_connections=20)
See also:
https://peewee.readthedocs.io/en/latest/peewee/playhouse.html#PostgresqlExtDatabase
"""

def init(self, database, **kwargs):
self.min_connections = kwargs.pop('min_connections', 1)
self.max_connections = kwargs.pop('max_connections', 20)
connection_timeout = kwargs.pop('connection_timeout', None)
if connection_timeout is not None:
warnings.warn(
"`connection_timeout` is deprecated, use `connect_timeout` instead.",
DeprecationWarning
)
kwargs['connect_timeout'] = connection_timeout
super().init(database, **kwargs)
self.init_async(
enable_json=True,
enable_hstore=self._register_hstore
)


register_database(PooledPostgresqlExtDatabase, 'postgresext+pool+async', 'postgresqlext+pool+async')
warnings.warn(
"import from `peewee_asyncext is deprecated the module will be removed",
DeprecationWarning
)

0 comments on commit 661f474

Please sign in to comment.