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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: postgresql-testing
3
- Version: 0.0.2
3
+ Version: 0.0.4
4
4
  Summary: PostgreSQL testing helpers
5
5
  Author: Oli Russell
6
6
  Author-email: Oli Russell <mrxoliver@gmail.com>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "postgresql-testing"
3
- version = "0.0.2"
3
+ version = "0.0.4"
4
4
  description = "PostgreSQL testing helpers"
5
5
  readme = "README.md"
6
6
  authors = [
@@ -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 RuntimeError(f"Directory {directory} already exists")
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
- process.wait()
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 RuntimeError(f"Database {c.database} already exists")
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 RuntimeError(f"Template database {template} already exists")
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 RuntimeError(f"Archive {archive} already exists")
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 RuntimeError(f"Could not connect to {dsn}")
240
+ raise PostgresqlTestingError(f"Could not connect to {dsn}")