postgresql-testing 0.0.3__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.3
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.3"
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":
@@ -140,7 +143,7 @@ def create_database(
140
143
  c: DatabaseConfig,
141
144
  *,
142
145
  template: str | None = None,
143
- on_existing: Literal["raise", "replace"] = "replace",
146
+ on_existing: Literal["raise", "use", "replace"] = "replace",
144
147
  ) -> None:
145
148
  template_str = "" if template is None else f'WITH TEMPLATE "{template}"'
146
149
  sql_create_database = f'CREATE DATABASE "{c.database}" {template_str} OWNER "{c.user}" STRATEGY=FILE_COPY'
@@ -151,7 +154,9 @@ def create_database(
151
154
  cur.execute(sql_create_database)
152
155
  except psycopg.errors.DuplicateDatabase:
153
156
  if on_existing == "raise":
154
- raise RuntimeError(f"Database {c.database} already exists")
157
+ raise PostgresqlTestingError(f"Database {c.database} already exists")
158
+ if on_existing == "use":
159
+ pass
155
160
  if on_existing == "replace":
156
161
  cur.execute(f'DROP DATABASE "{c.database}"')
157
162
  cur.execute(sql_create_database)
@@ -172,7 +177,7 @@ def create_template(
172
177
  template_exists = bool(cur.execute(f"SELECT 1 FROM pg_database WHERE datname = '{template}'").fetchall())
173
178
  if template_exists:
174
179
  if on_existing == "raise":
175
- raise RuntimeError(f"Template database {template} already exists")
180
+ raise PostgresqlTestingError(f"Template database {template} already exists")
176
181
  if on_existing == "replace":
177
182
  cur.execute(f"UPDATE pg_database SET datistemplate = false WHERE datname='{template}'")
178
183
  cur.execute(f'DROP DATABASE "{template}"')
@@ -190,7 +195,7 @@ def dump_archive(
190
195
  ) -> None:
191
196
  if archive.exists():
192
197
  if on_existing == "raise":
193
- raise RuntimeError(f"Archive {archive} already exists")
198
+ raise PostgresqlTestingError(f"Archive {archive} already exists")
194
199
  if on_existing == "replace":
195
200
  archive.unlink()
196
201
 
@@ -232,4 +237,4 @@ def _try_connect(dsn: str) -> None:
232
237
  return
233
238
  except psycopg.OperationalError:
234
239
  pass
235
- raise RuntimeError(f"Could not connect to {dsn}")
240
+ raise PostgresqlTestingError(f"Could not connect to {dsn}")