-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathjanitor.py
More file actions
308 lines (266 loc) · 11.1 KB
/
Copy pathjanitor.py
File metadata and controls
308 lines (266 loc) · 11.1 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
"""Database Janitor."""
import inspect
from contextlib import asynccontextmanager, contextmanager
from pathlib import Path
from types import TracebackType
from typing import Callable, Iterator, Type, TypeVar
import psycopg
from packaging.version import parse
from psycopg import 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")
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 | None = None,
template_dbname: str | None = None,
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
: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
if not (dbname or template_dbname):
raise ValueError("At least one of the dbname or template_dbname has to be filled.")
self.dbname = dbname
self.template_dbname = template_dbname
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.is_template():
cur.execute(f'CREATE DATABASE "{self.template_dbname}" WITH is_template = true;')
elif self.template_dbname is None:
cur.execute(f'CREATE DATABASE "{self.dbname}";')
else:
# 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)
cur.execute(f'CREATE DATABASE "{self.dbname}" TEMPLATE "{self.template_dbname}";')
def is_template(self) -> bool:
"""Determine whether the DatabaseJanitor maintains template or database."""
return self.dbname is None
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.
db_to_drop = self.template_dbname if self.is_template() else self.dbname
assert db_to_drop
with self.cursor() as cur:
self._dont_datallowconn(cur, db_to_drop)
self._terminate_connection(cur, db_to_drop)
if self.is_template():
cur.execute(f'ALTER DATABASE "{db_to_drop}" with is_template false;')
cur.execute(f'DROP DATABASE IF EXISTS "{db_to_drop}";')
@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.
"""
db_to_load = self.template_dbname if self.is_template() else self.dbname
_loader = build_loader(load)
_loader(
host=self.host,
port=self.port,
user=self.user,
dbname=db_to_load,
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 for specific tasks."""
def __init__(
self,
*,
user: str,
host: str,
port: str | int,
version: str | float | Version, # type: ignore[valid-type]
dbname: str | None = None,
template_dbname: str | None = None,
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
: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
if not (dbname or template_dbname):
raise ValueError("At least one of the dbname or template_dbname has to be filled.")
self.dbname = dbname
self.template_dbname = template_dbname
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.is_template():
await cur.execute(f'CREATE DATABASE "{self.template_dbname}" WITH is_template = true;')
elif self.template_dbname is None:
await cur.execute(f'CREATE DATABASE "{self.dbname}";')
else:
# 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)
await cur.execute(f'CREATE DATABASE "{self.dbname}" TEMPLATE "{self.template_dbname}";')
def is_template(self) -> bool:
"""Determine whether the DatabaseJanitor maintains template or database."""
return self.dbname is None
async def drop(self) -> None:
"""Drop database in postgresql (async)."""
db_to_drop = self.template_dbname if self.is_template() else self.dbname
assert db_to_drop
async with self.cursor() as cur:
await self._dont_datallowconn(cur, db_to_drop)
await self._terminate_connection(cur, db_to_drop)
if self.is_template():
await cur.execute(f'ALTER DATABASE "{db_to_drop}" with is_template false;')
await cur.execute(f'DROP DATABASE IF EXISTS "{db_to_drop}";')
@staticmethod
async def _dont_datallowconn(cur, dbname: str) -> None:
await cur.execute(f'ALTER DATABASE "{dbname}" with allow_connections false;')
@staticmethod
async def _terminate_connection(cur, dbname: str) -> None:
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 (async).
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.
"""
db_to_load = self.template_dbname if self.is_template() else self.dbname
_loader = build_loader_async(load)
cor = _loader(
host=self.host,
port=self.port,
user=self.user,
dbname=db_to_load,
password=self.password,
)
if inspect.isawaitable(cor):
await cor
@asynccontextmanager
async def cursor(self, dbname: str = "postgres"):
"""Async context manager for postgresql cursor."""
async def connect() -> psycopg.AsyncConnection:
"""Return postgresql async cursor."""
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):
"""Initialize Database Janitor."""
await self.init()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
"""Exit from Database janitor context cleaning after itself."""
await self.drop()