Skip to content

Commit 6115bba

Browse files
author
Sergio García Prado
committed
ISSUE #346
* Add retro-compatibility.
1 parent d956d1b commit 6115bba

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

packages/core/minos-microservice-common/minos/common/database/abc.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
)
3434
from .pools import (
3535
DatabaseClientPool,
36+
PostgreSqlPool,
3637
)
3738

3839

@@ -42,26 +43,32 @@ class DatabaseMixin(SetupMixin):
4243
@Inject()
4344
def __init__(
4445
self,
45-
pool: Optional[DatabaseClientPool] = None,
46+
database_pool: Optional[DatabaseClientPool] = None,
4647
pool_factory: Optional[PoolFactory] = None,
48+
postgresql_pool: Optional[PostgreSqlPool] = None,
4749
*args,
4850
**kwargs,
4951
):
5052
super().__init__(*args, **kwargs, pool_factory=pool_factory)
51-
if pool is None and pool_factory is not None:
52-
pool = pool_factory.get_pool("database")
53+
if database_pool is None and pool_factory is not None:
54+
database_pool = pool_factory.get_pool("database")
5355

54-
if not isinstance(pool, DatabaseClientPool):
55-
raise NotProvidedException(f"A {DatabaseClientPool!r} instance is required. Obtained: {pool}")
56+
if database_pool is None and postgresql_pool is not None:
57+
warnings.warn("'postgresql_pool' argument has been deprecated", DeprecationWarning)
58+
database_pool = postgresql_pool
5659

57-
self._pool = pool
60+
if not isinstance(database_pool, DatabaseClientPool):
61+
raise NotProvidedException(f"A {DatabaseClientPool!r} instance is required. Obtained: {database_pool}")
62+
63+
self._pool = database_pool
5864

5965
@property
6066
def database(self) -> str:
6167
"""Get the database's database.
6268
6369
:return: A ``str`` value.
6470
"""
71+
warnings.warn("'database' has been deprecated. Use 'pool.database' instead.", DeprecationWarning)
6572
return self.pool.database
6673

6774
@property
@@ -70,6 +77,7 @@ def host(self) -> str:
7077
7178
:return: A ``str`` value.
7279
"""
80+
warnings.warn("'host' has been deprecated. Use 'pool.host' instead.", DeprecationWarning)
7381
return self.pool.host
7482

7583
@property
@@ -78,6 +86,7 @@ def port(self) -> int:
7886
7987
:return: An ``int`` value.
8088
"""
89+
warnings.warn("'port' has been deprecated. Use 'pool.port' instead.", DeprecationWarning)
8190
return self.pool.port
8291

8392
@property
@@ -86,6 +95,7 @@ def user(self) -> str:
8695
8796
:return: A ``str`` value.
8897
"""
98+
warnings.warn("'user' has been deprecated. Use 'pool.user' instead.", DeprecationWarning)
8999
return self.pool.user
90100

91101
@property
@@ -94,6 +104,7 @@ def password(self) -> str:
94104
95105
:return: A ``str`` value.
96106
"""
107+
warnings.warn("'password' has been deprecated. Use 'pool.password' instead.", DeprecationWarning)
97108
return self.pool.password
98109

99110
async def submit_query_and_fetchone(self, *args, **kwargs) -> tuple:

packages/core/minos-microservice-common/minos/common/pools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def _get_pool_cls(self, type_: str) -> type[Pool]:
8787
pool_cls = self._default_classes.get(type_)
8888

8989
if pool_cls is None:
90-
pool_cls = self._config.get_pools().get(type_)
90+
pool_cls = self._config.get_pools().get("types", dict()).get(type_)
9191

9292
if pool_cls is None:
9393
raise ValueError(

packages/core/minos-microservice-common/tests/test_common/test_database/test_abc.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
NotProvidedException,
1010
PoolFactory,
1111
PostgreSqlMinosDatabase,
12+
PostgreSqlPool,
1213
)
1314
from minos.common.testing import (
1415
PostgresAsyncTestCase,
@@ -35,32 +36,56 @@ async def test_constructor_with_pool_factory(self):
3536
async def test_constructor_raises(self):
3637
with self.assertRaises(NotProvidedException):
3738
# noinspection PyArgumentEqualDefault
38-
DatabaseMixin(pool=None, pool_factory=None)
39+
DatabaseMixin(database_pool=None, pool_factory=None)
40+
41+
async def test_constructor_with_postgresql_pool(self):
42+
with warnings.catch_warnings():
43+
warnings.simplefilter("ignore", DeprecationWarning)
44+
pool = PostgreSqlPool.from_config(self.config)
45+
# noinspection PyTypeChecker,PyArgumentEqualDefault
46+
database = DatabaseMixin(database_pool=None, pool_factory=None, postgresql_pool=pool)
47+
48+
self.assertEqual(pool, database.pool)
3949

4050
def test_database(self):
4151
pool = DatabaseClientPool.from_config(self.config)
4252
database = DatabaseMixin(pool)
43-
self.assertEqual(pool.database, database.database)
53+
with warnings.catch_warnings():
54+
warnings.simplefilter("ignore", DeprecationWarning)
55+
# noinspection PyDeprecation
56+
self.assertEqual(pool.database, database.database)
4457

4558
def test_user(self):
4659
pool = DatabaseClientPool.from_config(self.config)
4760
database = DatabaseMixin(pool)
48-
self.assertEqual(pool.user, database.user)
61+
with warnings.catch_warnings():
62+
warnings.simplefilter("ignore", DeprecationWarning)
63+
# noinspection PyDeprecation
64+
self.assertEqual(pool.user, database.user)
4965

5066
def test_password(self):
5167
pool = DatabaseClientPool.from_config(self.config)
5268
database = DatabaseMixin(pool)
53-
self.assertEqual(pool.password, database.password)
69+
with warnings.catch_warnings():
70+
warnings.simplefilter("ignore", DeprecationWarning)
71+
# noinspection PyDeprecation
72+
self.assertEqual(pool.password, database.password)
5473

5574
def test_host(self):
5675
pool = DatabaseClientPool.from_config(self.config)
5776
database = DatabaseMixin(pool)
58-
self.assertEqual(pool.host, database.host)
77+
with warnings.catch_warnings():
78+
warnings.simplefilter("ignore", DeprecationWarning)
79+
# noinspection PyDeprecation
80+
self.assertEqual(pool.host, database.host)
5981

6082
def test_port(self):
6183
pool = DatabaseClientPool.from_config(self.config)
6284
database = DatabaseMixin(pool)
63-
self.assertEqual(pool.port, database.port)
85+
with warnings.catch_warnings():
86+
warnings.simplefilter("ignore", DeprecationWarning)
87+
# noinspection PyDeprecation
88+
self.assertEqual(pool.port, database.port)
6489

6590
async def test_pool(self):
6691
async with DatabaseMixin() as database:

0 commit comments

Comments
 (0)