@@ -42,26 +42,26 @@ def __init__(self, user: str, password: str, database: str, host: str, port: Sup
42
42
self .host = host
43
43
self .port = int (port ) # make sure port is int type
44
44
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
- )
52
45
# self._db_pool = None # Type: Optional[asyncpg.pool.Pool]
53
46
self ._connection = None # Type: Optional[asyncpg.Connection]
54
47
55
48
self ._transaction_class = type (
56
49
'TransactionWrapper' , (TransactionWrapper , self .__class__ ), {}
57
50
)
58
51
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
+ )
60
60
try :
61
- self ._connection = await asyncpg .connect (self . dsn )
61
+ self ._connection = await asyncpg .connect (dsn )
62
62
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
65
65
)
66
66
except asyncpg .InvalidCatalogNameError :
67
67
raise DBConnectionError ("Can't establish connection to database {}" .format (
@@ -71,34 +71,26 @@ async def create_connection(self) -> None:
71
71
async def close (self ) -> None :
72
72
if self ._connection :
73
73
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
+ )
74
78
self ._connection = None
75
79
76
80
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 )
84
82
await self .execute_script (
85
83
'CREATE DATABASE "{}" OWNER "{}"' .format (self .database , self .user )
86
84
)
87
- await self ._connection . close () # type: ignore
85
+ await self .close ()
88
86
89
87
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 )
97
89
try :
98
90
await self .execute_script ('DROP DATABASE "{}"' .format (self .database ))
99
91
except asyncpg .InvalidCatalogNameError : # pragma: nocoverage
100
92
pass
101
- await self ._connection . close () # type: ignore
93
+ await self .close ()
102
94
103
95
def acquire_connection (self ) -> ConnectionWrapper :
104
96
return ConnectionWrapper (self ._connection )
0 commit comments