hdx-python-database 1.3.6__py3-none-any.whl → 1.3.7__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
@@ -17,5 +17,5 @@ __version__: str
17
17
  __version_tuple__: VERSION_TUPLE
18
18
  version_tuple: VERSION_TUPLE
19
19
 
20
- __version__ = version = '1.3.6'
21
- __version_tuple__ = version_tuple = (1, 3, 6)
20
+ __version__ = version = '1.3.7'
21
+ __version_tuple__ = version_tuple = (1, 3, 7)
hdx/database/database.py CHANGED
@@ -281,17 +281,15 @@ class Database:
281
281
  engine = create_engine(db_uri, poolclass=NullPool, echo=False)
282
282
  if reflect:
283
283
  Base = automap_base(declarative_base=table_base)
284
- Base.prepare(
285
- autoload_with=engine, reflection_options={"views": True}
286
- )
284
+ Base.prepare(autoload_with=engine, reflection_options={"views": True})
287
285
  table_base = Base
288
286
  else:
289
287
  table_base.metadata.create_all(engine)
290
288
  return Session(engine), table_base
291
289
 
292
290
  @staticmethod
293
- def recreate_schema(engine: Engine, schema_name: str = "public") -> bool:
294
- """Wipe and create empty schema in database using SQLAlchemy.
291
+ def drop_schema(engine: Engine, schema_name: str = "public") -> bool:
292
+ """Wipe schema in database using SQLAlchemy.
295
293
 
296
294
  Args:
297
295
  engine (Engine): SQLAlchemy engine to use.
@@ -307,10 +305,30 @@ class Database:
307
305
  DropSchema(schema_name, cascade=True, if_exists=True)
308
306
  )
309
307
  connection.commit()
308
+ return True
309
+ except SQLAlchemyError:
310
+ return False
311
+
312
+ @staticmethod
313
+ def recreate_schema(engine: Engine, schema_name: str = "public") -> bool:
314
+ """Wipe and create empty schema in database using SQLAlchemy.
315
+
316
+ Args:
317
+ engine (Engine): SQLAlchemy engine to use.
318
+ schema_name (str): Schema name. Defaults to "public".
319
+
320
+ Returns:
321
+ bool: True if all successful, False if not
322
+ """
323
+ # Wipe and create an empty schema
324
+ try:
325
+ with engine.connect() as connection:
310
326
  connection.execute(
311
- CreateSchema(schema_name, if_not_exists=True)
327
+ DropSchema(schema_name, cascade=True, if_exists=True)
312
328
  )
313
329
  connection.commit()
330
+ connection.execute(CreateSchema(schema_name, if_not_exists=True))
331
+ connection.commit()
314
332
  return True
315
333
  except SQLAlchemyError:
316
334
  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.7
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=P4BCutErILqbg7G9_GBs9LhAib9fH-uALXBnLt2OtK8,511
3
+ hdx/database/database.py,sha256=1Xn-UYZ_OZquAtNnrimdR0CIgbtx-hQZD0NSAYHitHY,13055
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.7.dist-info/METADATA,sha256=Q9bf8Jl8u0cEVVqTM-Pv7KschLbWaUPpINPBVeWHA1Y,2841
11
+ hdx_python_database-1.3.7.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
12
+ hdx_python_database-1.3.7.dist-info/licenses/LICENSE,sha256=wc-4GpMn-ODs-U_bTe1YCiPVgvcjzrpYOx2wPuyAeII,1079
13
+ hdx_python_database-1.3.7.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,,