hdx-python-database 1.3.6__py3-none-any.whl → 1.3.8__py3-none-any.whl

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.
hdx/database/_version.py CHANGED
@@ -1,7 +1,14 @@
1
1
  # file generated by setuptools-scm
2
2
  # don't change, don't track in version control
3
3
 
4
- __all__ = ["__version__", "__version_tuple__", "version", "version_tuple"]
4
+ __all__ = [
5
+ "__version__",
6
+ "__version_tuple__",
7
+ "version",
8
+ "version_tuple",
9
+ "__commit_id__",
10
+ "commit_id",
11
+ ]
5
12
 
6
13
  TYPE_CHECKING = False
7
14
  if TYPE_CHECKING:
@@ -9,13 +16,19 @@ if TYPE_CHECKING:
9
16
  from typing import Union
10
17
 
11
18
  VERSION_TUPLE = Tuple[Union[int, str], ...]
19
+ COMMIT_ID = Union[str, None]
12
20
  else:
13
21
  VERSION_TUPLE = object
22
+ COMMIT_ID = object
14
23
 
15
24
  version: str
16
25
  __version__: str
17
26
  __version_tuple__: VERSION_TUPLE
18
27
  version_tuple: VERSION_TUPLE
28
+ commit_id: COMMIT_ID
29
+ __commit_id__: COMMIT_ID
19
30
 
20
- __version__ = version = '1.3.6'
21
- __version_tuple__ = version_tuple = (1, 3, 6)
31
+ __version__ = version = '1.3.8'
32
+ __version_tuple__ = version_tuple = (1, 3, 8)
33
+
34
+ __commit_id__ = commit_id = None
hdx/database/database.py CHANGED
@@ -244,12 +244,10 @@ class Database:
244
244
  Returns:
245
245
  Tuple[Session, Any]: (SQLAlchemy session, base)
246
246
  """
247
- batches = range(len(rows) // batch_size + 1)
248
- for batch in batches:
249
- start_row = batch * batch_size
250
- end_row = start_row + batch_size
251
- batch_rows = rows[start_row:end_row]
252
- self._session.execute(insert(dbtable), batch_rows)
247
+
248
+ for i in range(0, len(rows), batch_size):
249
+ batch = rows[i : i + batch_size]
250
+ self._session.execute(insert(dbtable), batch)
253
251
  self._session.commit()
254
252
 
255
253
  @staticmethod
@@ -281,17 +279,15 @@ class Database:
281
279
  engine = create_engine(db_uri, poolclass=NullPool, echo=False)
282
280
  if reflect:
283
281
  Base = automap_base(declarative_base=table_base)
284
- Base.prepare(
285
- autoload_with=engine, reflection_options={"views": True}
286
- )
282
+ Base.prepare(autoload_with=engine, reflection_options={"views": True})
287
283
  table_base = Base
288
284
  else:
289
285
  table_base.metadata.create_all(engine)
290
286
  return Session(engine), table_base
291
287
 
292
288
  @staticmethod
293
- def recreate_schema(engine: Engine, schema_name: str = "public") -> bool:
294
- """Wipe and create empty schema in database using SQLAlchemy.
289
+ def drop_schema(engine: Engine, schema_name: str = "public") -> bool:
290
+ """Wipe schema in database using SQLAlchemy.
295
291
 
296
292
  Args:
297
293
  engine (Engine): SQLAlchemy engine to use.
@@ -307,10 +303,30 @@ class Database:
307
303
  DropSchema(schema_name, cascade=True, if_exists=True)
308
304
  )
309
305
  connection.commit()
306
+ return True
307
+ except SQLAlchemyError:
308
+ return False
309
+
310
+ @staticmethod
311
+ def recreate_schema(engine: Engine, schema_name: str = "public") -> bool:
312
+ """Wipe and create empty schema in database using SQLAlchemy.
313
+
314
+ Args:
315
+ engine (Engine): SQLAlchemy engine to use.
316
+ schema_name (str): Schema name. Defaults to "public".
317
+
318
+ Returns:
319
+ bool: True if all successful, False if not
320
+ """
321
+ # Wipe and create an empty schema
322
+ try:
323
+ with engine.connect() as connection:
310
324
  connection.execute(
311
- CreateSchema(schema_name, if_not_exists=True)
325
+ DropSchema(schema_name, cascade=True, if_exists=True)
312
326
  )
313
327
  connection.commit()
328
+ connection.execute(CreateSchema(schema_name, if_not_exists=True))
329
+ connection.commit()
314
330
  return True
315
331
  except SQLAlchemyError:
316
332
  return False
@@ -59,6 +59,12 @@ def restore_from_pgfile(db_uri: str, pg_restore_file: str) -> str:
59
59
  str: Output from the pg_restore command
60
60
  """
61
61
  db_params = get_params_from_connection_uri(db_uri)
62
+ if pg_restore_file[-3:] == ".xz":
63
+ decompress = subprocess.Popen(
64
+ ("unxz", "-c", "-d", pg_restore_file), stdout=subprocess.PIPE
65
+ ).stdout
66
+ else:
67
+ decompress = None
62
68
  subprocess_params = ["pg_restore", "-c"]
63
69
  for key, value in db_params.items():
64
70
  match key:
@@ -74,13 +80,18 @@ def restore_from_pgfile(db_uri: str, pg_restore_file: str) -> str:
74
80
  continue
75
81
  subprocess_params.append(f"{value}")
76
82
 
77
- subprocess_params.append(f"{pg_restore_file}")
83
+ if not decompress:
84
+ subprocess_params.append(pg_restore_file)
78
85
  env = environ.copy()
79
86
  password = db_params.get("password")
80
87
  if password:
81
88
  env["PGPASSWORD"] = password
82
89
  process = subprocess.run(
83
- subprocess_params, env=env, capture_output=True, encoding="utf-8"
90
+ subprocess_params,
91
+ env=env,
92
+ capture_output=True,
93
+ encoding="utf-8",
94
+ stdin=decompress,
84
95
  )
85
96
  try:
86
97
  process.check_returncode()
hdx/database/utils.py CHANGED
@@ -13,7 +13,5 @@ def camel_to_snake_case(string: str) -> str:
13
13
  Returns:
14
14
  str: String in snake case
15
15
  """
16
- string = re.sub(
17
- r"((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))", r"_\1", string
18
- )
16
+ string = re.sub(r"((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))", r"_\1", string)
19
17
  return string.lower().lstrip("_")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hdx-python-database
3
- Version: 1.3.6
3
+ Version: 1.3.8
4
4
  Summary: HDX Python database utilities
5
5
  Project-URL: Homepage, https://github.com/OCHA-DAP/hdx-python-database
6
6
  Author-email: Michael Rans <rans@email.com>
@@ -24,7 +24,7 @@ Classifier: Programming Language :: Python :: 3.10
24
24
  Classifier: Programming Language :: Python :: 3.11
25
25
  Classifier: Programming Language :: Python :: 3.12
26
26
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
27
- Requires-Python: >=3.8
27
+ Requires-Python: >=3.10
28
28
  Requires-Dist: sqlalchemy>=2
29
29
  Provides-Extra: dev
30
30
  Requires-Dist: pre-commit; extra == 'dev'
@@ -35,8 +35,10 @@ Requires-Dist: psycopg[binary]; extra == 'postgresql'
35
35
  Provides-Extra: sshtunnel
36
36
  Requires-Dist: sshtunnel; extra == 'sshtunnel'
37
37
  Provides-Extra: test
38
+ Requires-Dist: psycopg[binary]; extra == 'test'
38
39
  Requires-Dist: pytest; extra == 'test'
39
40
  Requires-Dist: pytest-cov; extra == 'test'
41
+ Requires-Dist: sshtunnel; extra == 'test'
40
42
  Description-Content-Type: text/markdown
41
43
 
42
44
  [![Build Status](https://github.com/OCHA-DAP/hdx-python-database/actions/workflows/run-python-tests.yaml/badge.svg)](https://github.com/OCHA-DAP/hdx-python-database/actions/workflows/run-python-tests.yaml)
@@ -0,0 +1,13 @@
1
+ hdx/database/__init__.py,sha256=sG2OYV5LHskgiDkB4IeVeQyPHL22Ta1kuxf-xN92-5E,155
2
+ hdx/database/_version.py,sha256=_hPMnsWQOeGw9lqV0fueN3y4Gw7vLcBaoT0Bio9pJWo,704
3
+ hdx/database/database.py,sha256=pOmshEfhkvlVN-cWJ9LX8ZL99TyZnwPAHOC-v0nF1pM,12926
4
+ hdx/database/dburi.py,sha256=rw8RuLpU85vxYl5-Zk1SdZRAz-tqre_ot5aEnkjhxAQ,2977
5
+ hdx/database/no_timezone.py,sha256=Lw087G3qOf8H70o8Hq_VvOMZ3ANei-lIA_sUlpFU_Uo,1130
6
+ hdx/database/postgresql.py,sha256=5xmGFbzjTPTr9clQw4rOD1ZQEzYuY25Ho2hhz-WdCC8,2923
7
+ hdx/database/utils.py,sha256=nzChgDj2_Mq8GS6xWaNmqOgpAiwK9tGYV_skKbErEVA,401
8
+ hdx/database/views.py,sha256=U4BN9tbjlr93qKkeNbZNo_PCIwGsng09mQ2xFqh5CH0,1636
9
+ hdx/database/with_timezone.py,sha256=GUUylmTwQHp22yedmCVKpVJzvjWgdS9lg9KeqciNJkY,235
10
+ hdx_python_database-1.3.8.dist-info/METADATA,sha256=fC99IRhtjoty5SpmkVDobyMMYS3SYhs68ftDUoxOrZM,2841
11
+ hdx_python_database-1.3.8.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
12
+ hdx_python_database-1.3.8.dist-info/licenses/LICENSE,sha256=wc-4GpMn-ODs-U_bTe1YCiPVgvcjzrpYOx2wPuyAeII,1079
13
+ hdx_python_database-1.3.8.dist-info/RECORD,,
@@ -1,13 +0,0 @@
1
- hdx/database/__init__.py,sha256=sG2OYV5LHskgiDkB4IeVeQyPHL22Ta1kuxf-xN92-5E,155
2
- hdx/database/_version.py,sha256=O6KrLlaUs-R3ppT6iunhqb7DyTGig2RAYgNqUiGEk0w,511
3
- hdx/database/database.py,sha256=hXjdRraeFouQ1afy9vHpCUkyaP-I2tPFGkDXE1z8A0A,12403
4
- hdx/database/dburi.py,sha256=rw8RuLpU85vxYl5-Zk1SdZRAz-tqre_ot5aEnkjhxAQ,2977
5
- hdx/database/no_timezone.py,sha256=Lw087G3qOf8H70o8Hq_VvOMZ3ANei-lIA_sUlpFU_Uo,1130
6
- hdx/database/postgresql.py,sha256=i5s_CJQZagMTuzgmnLakCOnSnvczpgJ26Q1CV6lz2_A,2646
7
- hdx/database/utils.py,sha256=6lLymWriMFCq8BZgrDt4V5z14GR3PbvG_WXZeBc99Ws,415
8
- hdx/database/views.py,sha256=U4BN9tbjlr93qKkeNbZNo_PCIwGsng09mQ2xFqh5CH0,1636
9
- hdx/database/with_timezone.py,sha256=GUUylmTwQHp22yedmCVKpVJzvjWgdS9lg9KeqciNJkY,235
10
- hdx_python_database-1.3.6.dist-info/METADATA,sha256=cPzaaLmmPam1abq5Ebh-Jed_qgv3jw-4FOnzLTc77f8,2750
11
- hdx_python_database-1.3.6.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
12
- hdx_python_database-1.3.6.dist-info/licenses/LICENSE,sha256=wc-4GpMn-ODs-U_bTe1YCiPVgvcjzrpYOx2wPuyAeII,1079
13
- hdx_python_database-1.3.6.dist-info/RECORD,,