Skip to content

Commit 2b8e2e6

Browse files
committed
More verbose connection logging, minor refactor
1 parent b9372f5 commit 2b8e2e6

File tree

5 files changed

+50
-49
lines changed

5 files changed

+50
-49
lines changed

tortoise/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ async def _init_connections(cls, connections_config, create_db):
163163
connection = client_class(**db_params)
164164
if create_db:
165165
await connection.db_create()
166-
await connection.create_connection()
166+
await connection.create_connection(True)
167167
cls._connections[name] = connection
168168
current_transaction_map[name] = ContextVar(name, default=None)
169169

tortoise/backends/asyncpg/client.py

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,26 @@ def __init__(self, user: str, password: str, database: str, host: str, port: Sup
4242
self.host = host
4343
self.port = int(port) # make sure port is int type
4444

45-
self.dsn = self.DSN_TEMPLATE.format(
46-
user=self.user,
47-
password=self.password,
48-
host=self.host,
49-
port=self.port,
50-
database=self.database
51-
)
5245
# self._db_pool = None # Type: Optional[asyncpg.pool.Pool]
5346
self._connection = None # Type: Optional[asyncpg.Connection]
5447

5548
self._transaction_class = type(
5649
'TransactionWrapper', (TransactionWrapper, self.__class__), {}
5750
)
5851

59-
async def create_connection(self) -> None:
52+
async def create_connection(self, with_db: bool) -> None:
53+
dsn = self.DSN_TEMPLATE.format(
54+
user=self.user,
55+
password=self.password,
56+
host=self.host,
57+
port=self.port,
58+
database=self.database if with_db else ''
59+
)
6060
try:
61-
self._connection = await asyncpg.connect(self.dsn)
61+
self._connection = await asyncpg.connect(dsn)
6262
self.log.debug(
63-
'Created connection with params: user=%s database=%s host=%s port=%s',
64-
self.user, self.database, self.host, self.port
63+
'Created connection %s with params: user=%s database=%s host=%s port=%s',
64+
self._connection, self.user, self.database, self.host, self.port
6565
)
6666
except asyncpg.InvalidCatalogNameError:
6767
raise DBConnectionError("Can't establish connection to database {}".format(
@@ -71,34 +71,26 @@ async def create_connection(self) -> None:
7171
async def close(self) -> None:
7272
if self._connection:
7373
await self._connection.close()
74+
self.log.debug(
75+
'Closed connection %s with params: user=%s database=%s host=%s port=%s',
76+
self._connection, self.user, self.database, self.host, self.port
77+
)
7478
self._connection = None
7579

7680
async def db_create(self) -> None:
77-
self._connection = await asyncpg.connect(self.DSN_TEMPLATE.format(
78-
user=self.user,
79-
password=self.password,
80-
host=self.host,
81-
port=self.port,
82-
database=''
83-
))
81+
await self.create_connection(False)
8482
await self.execute_script(
8583
'CREATE DATABASE "{}" OWNER "{}"'.format(self.database, self.user)
8684
)
87-
await self._connection.close() # type: ignore
85+
await self.close()
8886

8987
async def db_delete(self) -> None:
90-
self._connection = await asyncpg.connect(self.DSN_TEMPLATE.format(
91-
user=self.user,
92-
password=self.password,
93-
host=self.host,
94-
port=self.port,
95-
database=''
96-
))
88+
await self.create_connection(False)
9789
try:
9890
await self.execute_script('DROP DATABASE "{}"'.format(self.database))
9991
except asyncpg.InvalidCatalogNameError: # pragma: nocoverage
10092
pass
101-
await self._connection.close() # type: ignore
93+
await self.close()
10294

10395
def acquire_connection(self) -> ConnectionWrapper:
10496
return ConnectionWrapper(self._connection)

tortoise/backends/base/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self, connection_name: str, **kwargs) -> None:
1616
self.log = logging.getLogger('db_client')
1717
self.connection_name = connection_name
1818

19-
async def create_connection(self) -> None:
19+
async def create_connection(self, with_db: bool) -> None:
2020
raise NotImplementedError() # pragma: nocoverage
2121

2222
async def close(self) -> None:

tortoise/backends/mysql/client.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,27 @@ def __init__(self, user: str, password: str, database: str, host: str, port: Sup
4444
self.host = host
4545
self.port = int(port) # make sure port is int type
4646

47-
self.template = {
48-
'host': self.host,
49-
'port': self.port,
50-
'user': self.user,
51-
'password': self.password,
52-
}
53-
5447
# self._db_pool = None # Type: Optional[aiomysql.Pool]
5548
self._connection = None # Type: Optional[aiomysql.Connection]
5649

5750
self._transaction_class = type(
5851
'TransactionWrapper', (TransactionWrapper, self.__class__), {}
5952
)
6053

61-
async def create_connection(self) -> None:
54+
async def create_connection(self, with_db: bool) -> None:
55+
template = {
56+
'host': self.host,
57+
'port': self.port,
58+
'user': self.user,
59+
'password': self.password,
60+
'db': self.database if with_db else None
61+
}
62+
6263
try:
63-
self._connection = await aiomysql.connect(db=self.database, **self.template)
64+
self._connection = await aiomysql.connect(**template)
6465
self.log.debug(
65-
'Created connection with params: user=%s database=%s host=%s port=%s',
66-
self.user, self.database, self.host, self.port
66+
'Created connection %s with params: user=%s database=%s host=%s port=%s',
67+
self._connection, self.user, self.database, self.host, self.port
6768
)
6869
except pymysql.err.OperationalError:
6970
raise DBConnectionError(
@@ -76,26 +77,26 @@ async def create_connection(self) -> None:
7677
async def close(self) -> None:
7778
if self._connection:
7879
self._connection.close()
80+
self.log.debug(
81+
'Closed connection %s with params: user=%s database=%s host=%s port=%s',
82+
self._connection, self.user, self.database, self.host, self.port
83+
)
7984
self._connection = None
8085

8186
async def db_create(self) -> None:
82-
self._connection = await aiomysql.connect(
83-
**self.template
84-
)
87+
await self.create_connection(False)
8588
await self.execute_script(
8689
'CREATE DATABASE {}'.format(self.database)
8790
)
88-
self._connection.close() # type: ignore
91+
await self.close()
8992

9093
async def db_delete(self) -> None:
91-
self._connection = await aiomysql.connect(
92-
**self.template
93-
)
94+
await self.create_connection(False)
9495
try:
9596
await self.execute_script('DROP DATABASE {}'.format(self.database))
9697
except pymysql.err.DatabaseError: # pragma: nocoverage
9798
pass
98-
self._connection.close() # type: ignore
99+
await self.close()
99100

100101
def acquire_connection(self) -> ConnectionWrapper:
101102
return ConnectionWrapper(self._connection)

tortoise/backends/sqlite/client.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,24 @@ def __init__(self, file_path: str, **kwargs) -> None:
3838
)
3939
self._connection = None # type: Optional[aiosqlite.Connection]
4040

41-
async def create_connection(self) -> None:
41+
async def create_connection(self, with_db: bool) -> None:
4242
if not self._connection: # pragma: no branch
4343
self._connection = aiosqlite.connect(self.filename, isolation_level=None)
4444
self._connection.start()
4545
await self._connection._connect()
4646
self._connection._conn.row_factory = sqlite3.Row
47+
self.log.debug(
48+
'Created connection %s with params: filename=%s',
49+
self._connection, self.filename
50+
)
4751

4852
async def close(self) -> None:
4953
if self._connection:
5054
await self._connection.close()
55+
self.log.debug(
56+
'Closed connection %s with params: filename=%s',
57+
self._connection, self.filename
58+
)
5159
self._connection = None
5260

5361
async def db_create(self) -> None:

0 commit comments

Comments
 (0)