-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathjanitor.py
More file actions
317 lines (271 loc) · 11.2 KB
/
Copy pathjanitor.py
File metadata and controls
317 lines (271 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"""Database Janitor."""
import inspect
from contextlib import asynccontextmanager, contextmanager
from pathlib import Path
from types import TracebackType
from typing import AsyncIterator, Callable, Iterator, Type, TypeVar
import psycopg
from packaging.version import parse
from psycopg import AsyncCursor, Connection, Cursor
from pytest_postgresql.loader import build_loader, build_loader_async
from pytest_postgresql.retry import retry, retry_async
Version = type(parse("1"))
DatabaseJanitorType = TypeVar("DatabaseJanitorType", bound="DatabaseJanitor")
AsyncDatabaseJanitorType = TypeVar("AsyncDatabaseJanitorType", bound="AsyncDatabaseJanitor")
class DatabaseJanitor:
"""Manage database state for specific tasks."""
def __init__(
self,
*,
user: str,
host: str,
port: str | int,
version: str | float | Version, # type: ignore[valid-type]
dbname: str,
template_dbname: str | None = None,
as_template: bool = False,
password: str | None = None,
isolation_level: "psycopg.IsolationLevel | None" = None,
connection_timeout: int = 60,
) -> None:
"""Initialize janitor.
:param user: postgresql username
:param host: postgresql host
:param port: postgresql port
:param dbname: database name
:param template_dbname: template database name to clone from
:param as_template: whether to mark the database as a template
:param version: postgresql version number
:param password: optional postgresql password
:param isolation_level: optional postgresql isolation level
defaults to server's default
:param connection_timeout: how long to retry connection before
raising a TimeoutError
"""
self.user = user
self.password = password
self.host = host
self.port = port
self.dbname = dbname
self.template_dbname = template_dbname
self.as_template = as_template
self._connection_timeout = connection_timeout
self.isolation_level = isolation_level
if not isinstance(version, Version):
self.version = parse(str(version))
else:
self.version = version
def init(self) -> None:
"""Create database in postgresql."""
with self.cursor() as cur:
if self.template_dbname:
# And make sure no-one is left connected to the template database.
# Otherwise, Creating database from template will fail
self._terminate_connection(cur, self.template_dbname)
query = f'CREATE DATABASE "{self.dbname}" TEMPLATE "{self.template_dbname}"'
else:
query = f'CREATE DATABASE "{self.dbname}"'
if self.as_template:
query += " IS_TEMPLATE = true"
cur.execute(f"{query};")
def is_template(self) -> bool:
"""Determine whether the DatabaseJanitor maintains template or database."""
return self.as_template
def drop(self) -> None:
"""Drop database in postgresql."""
# We cannot drop the database while there are connections to it, so we
# terminate all connections first while not allowing new connections.
with self.cursor() as cur:
self._dont_datallowconn(cur, self.dbname)
self._terminate_connection(cur, self.dbname)
if self.as_template:
cur.execute(f'ALTER DATABASE "{self.dbname}" with is_template false;')
cur.execute(f'DROP DATABASE IF EXISTS "{self.dbname}";')
@staticmethod
def _dont_datallowconn(cur: Cursor, dbname: str) -> None:
cur.execute(f'ALTER DATABASE "{dbname}" with allow_connections false;')
@staticmethod
def _terminate_connection(cur: Cursor, dbname: str) -> None:
cur.execute(
"SELECT pg_terminate_backend(pg_stat_activity.pid)"
"FROM pg_stat_activity "
"WHERE pg_stat_activity.datname = %s;",
(dbname,),
)
def load(self, load: Callable | str | Path) -> None:
"""Load data into a database.
Expects:
* a Path to sql file, that'll be loaded
* an import path to import callable
* a callable that expects: host, port, user, dbname and password arguments.
"""
_loader = build_loader(load)
_loader(
host=self.host,
port=self.port,
user=self.user,
dbname=self.dbname,
password=self.password,
)
@contextmanager
def cursor(self, dbname: str = "postgres") -> Iterator[Cursor]:
"""Return postgresql cursor."""
def connect() -> Connection:
return psycopg.connect(
dbname=dbname,
user=self.user,
password=self.password,
host=self.host,
port=self.port,
)
conn = retry(connect, timeout=self._connection_timeout, possible_exception=psycopg.OperationalError)
conn.isolation_level = self.isolation_level
# We must not run a transaction since we create a database.
conn.autocommit = True
cur = conn.cursor()
try:
yield cur
finally:
cur.close()
conn.close()
def __enter__(self: DatabaseJanitorType) -> DatabaseJanitorType:
"""Initialize Database Janitor."""
self.init()
return self
def __exit__(
self: DatabaseJanitorType,
exc_type: Type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
"""Exit from Database janitor context cleaning after itself."""
self.drop()
class AsyncDatabaseJanitor:
"""Manage database state asynchronously for specific tasks."""
def __init__(
self,
*,
user: str,
host: str,
port: str | int,
version: str | float | Version, # type: ignore[valid-type]
dbname: str,
template_dbname: str | None = None,
as_template: bool = False,
password: str | None = None,
isolation_level: "psycopg.IsolationLevel | None" = None,
connection_timeout: int = 60,
) -> None:
"""Initialize async janitor.
:param user: postgresql username
:param host: postgresql host
:param port: postgresql port
:param dbname: database name
:param template_dbname: template database name to clone from
:param as_template: whether to mark the database as a template
:param version: postgresql version number
:param password: optional postgresql password
:param isolation_level: optional postgresql isolation level
defaults to server's default
:param connection_timeout: how long to retry connection before
raising a TimeoutError
"""
self.user = user
self.password = password
self.host = host
self.port = port
self.dbname = dbname
self.template_dbname = template_dbname
self.as_template = as_template
self._connection_timeout = connection_timeout
self.isolation_level = isolation_level
if not isinstance(version, Version):
self.version = parse(str(version))
else:
self.version = version
async def init(self) -> None:
"""Create database in postgresql."""
async with self.cursor() as cur:
if self.template_dbname:
# And make sure no-one is left connected to the template database.
# Otherwise, Creating database from template will fail
await self._terminate_connection(cur, self.template_dbname)
query = f'CREATE DATABASE "{self.dbname}" TEMPLATE "{self.template_dbname}"'
else:
query = f'CREATE DATABASE "{self.dbname}"'
if self.as_template:
query += " IS_TEMPLATE = true"
await cur.execute(f"{query};")
def is_template(self) -> bool:
"""Determine whether the AsyncDatabaseJanitor maintains template or database."""
return self.as_template
async def drop(self) -> None:
"""Drop database in postgresql."""
# We cannot drop the database while there are connections to it, so we
# terminate all connections first while not allowing new connections.
async with self.cursor() as cur:
await self._dont_datallowconn(cur, self.dbname)
await self._terminate_connection(cur, self.dbname)
if self.as_template:
await cur.execute(f'ALTER DATABASE "{self.dbname}" with is_template false;')
await cur.execute(f'DROP DATABASE IF EXISTS "{self.dbname}";')
@staticmethod
async def _dont_datallowconn(cur: AsyncCursor, dbname: str) -> None: # type: ignore[type-arg]
await cur.execute(f'ALTER DATABASE "{dbname}" with allow_connections false;')
@staticmethod
async def _terminate_connection(cur: AsyncCursor, dbname: str) -> None: # type: ignore[type-arg]
await cur.execute(
"SELECT pg_terminate_backend(pg_stat_activity.pid)"
"FROM pg_stat_activity "
"WHERE pg_stat_activity.datname = %s;",
(dbname,),
)
async def load(self, load: Callable | str | Path) -> None:
"""Load data into a database.
Expects:
* a Path to sql file, that'll be loaded
* an import path to import callable
* a callable that expects: host, port, user, dbname and password arguments.
"""
_loader = build_loader_async(load)
result = _loader(
host=self.host,
port=self.port,
user=self.user,
dbname=self.dbname,
password=self.password,
)
if inspect.isawaitable(result):
await result
@asynccontextmanager
async def cursor(self, dbname: str = "postgres") -> AsyncIterator[AsyncCursor]: # type: ignore[type-arg]
"""Return postgresql async cursor."""
async def connect() -> psycopg.AsyncConnection:
return await psycopg.AsyncConnection.connect(
dbname=dbname,
user=self.user,
password=self.password,
host=self.host,
port=self.port,
)
conn = await retry_async(connect, timeout=self._connection_timeout, possible_exception=psycopg.OperationalError)
try:
await conn.set_isolation_level(self.isolation_level)
await conn.set_autocommit(True)
# We must not run a transaction since we create a database.
async with conn.cursor() as cur:
yield cur
finally:
await conn.close()
async def __aenter__(self: AsyncDatabaseJanitorType) -> AsyncDatabaseJanitorType:
"""Initialize Async Database Janitor."""
await self.init()
return self
async def __aexit__(
self: AsyncDatabaseJanitorType,
exc_type: Type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
"""Exit from Async Database Janitor context cleaning after itself."""
await self.drop()