postgresql-testing 0.0.2__tar.gz → 0.0.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {postgresql_testing-0.0.2 → postgresql_testing-0.0.4}/PKG-INFO +1 -1
- {postgresql_testing-0.0.2 → postgresql_testing-0.0.4}/pyproject.toml +1 -1
- {postgresql_testing-0.0.2 → postgresql_testing-0.0.4}/src/postgresql_testing/__init__.py +20 -7
- {postgresql_testing-0.0.2 → postgresql_testing-0.0.4}/README.md +0 -0
- {postgresql_testing-0.0.2 → postgresql_testing-0.0.4}/src/postgresql_testing/py.typed +0 -0
|
@@ -14,6 +14,9 @@ SUPERUSER = "postgres"
|
|
|
14
14
|
ROOT_DATABASE = "postgres"
|
|
15
15
|
|
|
16
16
|
|
|
17
|
+
class PostgresqlTestingError(RuntimeError): ...
|
|
18
|
+
|
|
19
|
+
|
|
17
20
|
@dataclass(kw_only=True)
|
|
18
21
|
class ClusterConfig:
|
|
19
22
|
host: str
|
|
@@ -73,7 +76,7 @@ def initdb(
|
|
|
73
76
|
) -> None:
|
|
74
77
|
if directory.exists():
|
|
75
78
|
if on_existing == "raise":
|
|
76
|
-
raise
|
|
79
|
+
raise PostgresqlTestingError(f"Directory {directory} already exists")
|
|
77
80
|
if on_existing == "replace":
|
|
78
81
|
shutil.rmtree(directory)
|
|
79
82
|
if on_existing == "use":
|
|
@@ -116,8 +119,16 @@ def serve(c: ClusterConfig) -> Iterator[None]:
|
|
|
116
119
|
_try_connect(c.superuser().dsn)
|
|
117
120
|
yield
|
|
118
121
|
finally:
|
|
122
|
+
# Stop any ongoing queries
|
|
123
|
+
with psycopg.connect(c.superuser().dsn) as conn, conn.cursor() as cur:
|
|
124
|
+
cur.execute("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid()")
|
|
125
|
+
# Shutdown
|
|
119
126
|
process.terminate()
|
|
120
|
-
|
|
127
|
+
try:
|
|
128
|
+
process.wait(timeout=1)
|
|
129
|
+
except subprocess.TimeoutExpired:
|
|
130
|
+
process.kill()
|
|
131
|
+
process.wait(timeout=5)
|
|
121
132
|
|
|
122
133
|
|
|
123
134
|
def ensure_user(c: DatabaseConfig) -> None:
|
|
@@ -132,7 +143,7 @@ def create_database(
|
|
|
132
143
|
c: DatabaseConfig,
|
|
133
144
|
*,
|
|
134
145
|
template: str | None = None,
|
|
135
|
-
on_existing: Literal["raise", "replace"] = "replace",
|
|
146
|
+
on_existing: Literal["raise", "use", "replace"] = "replace",
|
|
136
147
|
) -> None:
|
|
137
148
|
template_str = "" if template is None else f'WITH TEMPLATE "{template}"'
|
|
138
149
|
sql_create_database = f'CREATE DATABASE "{c.database}" {template_str} OWNER "{c.user}" STRATEGY=FILE_COPY'
|
|
@@ -143,7 +154,9 @@ def create_database(
|
|
|
143
154
|
cur.execute(sql_create_database)
|
|
144
155
|
except psycopg.errors.DuplicateDatabase:
|
|
145
156
|
if on_existing == "raise":
|
|
146
|
-
raise
|
|
157
|
+
raise PostgresqlTestingError(f"Database {c.database} already exists")
|
|
158
|
+
if on_existing == "use":
|
|
159
|
+
pass
|
|
147
160
|
if on_existing == "replace":
|
|
148
161
|
cur.execute(f'DROP DATABASE "{c.database}"')
|
|
149
162
|
cur.execute(sql_create_database)
|
|
@@ -164,7 +177,7 @@ def create_template(
|
|
|
164
177
|
template_exists = bool(cur.execute(f"SELECT 1 FROM pg_database WHERE datname = '{template}'").fetchall())
|
|
165
178
|
if template_exists:
|
|
166
179
|
if on_existing == "raise":
|
|
167
|
-
raise
|
|
180
|
+
raise PostgresqlTestingError(f"Template database {template} already exists")
|
|
168
181
|
if on_existing == "replace":
|
|
169
182
|
cur.execute(f"UPDATE pg_database SET datistemplate = false WHERE datname='{template}'")
|
|
170
183
|
cur.execute(f'DROP DATABASE "{template}"')
|
|
@@ -182,7 +195,7 @@ def dump_archive(
|
|
|
182
195
|
) -> None:
|
|
183
196
|
if archive.exists():
|
|
184
197
|
if on_existing == "raise":
|
|
185
|
-
raise
|
|
198
|
+
raise PostgresqlTestingError(f"Archive {archive} already exists")
|
|
186
199
|
if on_existing == "replace":
|
|
187
200
|
archive.unlink()
|
|
188
201
|
|
|
@@ -224,4 +237,4 @@ def _try_connect(dsn: str) -> None:
|
|
|
224
237
|
return
|
|
225
238
|
except psycopg.OperationalError:
|
|
226
239
|
pass
|
|
227
|
-
raise
|
|
240
|
+
raise PostgresqlTestingError(f"Could not connect to {dsn}")
|
|
File without changes
|
|
File without changes
|