From d66115334c7935eb9cf777ea408ec3dd5851cecd Mon Sep 17 00:00:00 2001 From: mcarans Date: Mon, 6 May 2024 16:36:05 +1200 Subject: [PATCH] Update documentation Move method to function --- documentation/main.md | 7 ++++- requirements.txt | 26 +++++----------- src/hdx/database/__init__.py | 46 ++++++++++++++--------------- tests/hdx/database/test_database.py | 4 +-- 4 files changed, 38 insertions(+), 45 deletions(-) diff --git a/documentation/main.md b/documentation/main.md index 09650e0..dbad40a 100755 --- a/documentation/main.md +++ b/documentation/main.md @@ -51,8 +51,13 @@ Versions from 1.0.6 no longer support Python 2.7. ## Database +A fresh schema can be created using the function `recreate_schema`. Any +existing schema with that name will be dropped eg. + + recreate_schema(engine, db_uri) + Your SQLAlchemy database tables must inherit from `Base` in -`hdx.database.no_timezone` or `hdx.database.with_timezone` eg. : +`hdx.database.no_timezone` or `hdx.database.with_timezone` eg. from hdx.database.no_timezone import Base class MyTable(Base): diff --git a/requirements.txt b/requirements.txt index a9b7efe..cd8633f 100755 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,5 @@ -# -# This file is autogenerated by pip-compile with Python 3.11 -# by the following command: -# -# pip-compile --all-extras --output-file=requirements.txt pyproject.toml -# +# This file was autogenerated by uv via the following command: +# uv pip compile pyproject.toml --resolver=backtracking --all-extras -o requirements.txt bcrypt==4.1.3 # via paramiko cffi==1.16.0 @@ -12,7 +8,7 @@ cffi==1.16.0 # pynacl cfgv==3.4.0 # via pre-commit -coverage[toml]==7.5.1 +coverage==7.5.1 # via pytest-cov cryptography==42.0.6 # via paramiko @@ -37,9 +33,7 @@ platformdirs==4.2.1 pluggy==1.5.0 # via pytest pre-commit==3.7.0 - # via hdx-python-database (pyproject.toml) -psycopg[binary]==3.1.18 - # via hdx-python-database (pyproject.toml) +psycopg==3.1.18 psycopg-binary==3.1.18 # via psycopg pycparser==2.22 @@ -47,23 +41,17 @@ pycparser==2.22 pynacl==1.5.0 # via paramiko pytest==8.2.0 - # via - # hdx-python-database (pyproject.toml) - # pytest-cov + # via pytest-cov pytest-cov==5.0.0 - # via hdx-python-database (pyproject.toml) pyyaml==6.0.1 # via pre-commit +setuptools==69.5.1 + # via nodeenv sqlalchemy==2.0.30 - # via hdx-python-database (pyproject.toml) sshtunnel==0.4.0 - # via hdx-python-database (pyproject.toml) typing-extensions==4.11.0 # via # psycopg # sqlalchemy virtualenv==20.26.1 # via pre-commit - -# The following packages are considered to be unsafe in a requirements file: -# setuptools diff --git a/src/hdx/database/__init__.py b/src/hdx/database/__init__.py index 93fb3f3..f416107 100644 --- a/src/hdx/database/__init__.py +++ b/src/hdx/database/__init__.py @@ -18,6 +18,29 @@ logger = logging.getLogger(__name__) +def recreate_schema( + engine: Engine, db_uri: str, schema_name: str = "public" +) -> None: + """Wipe and create empty schema in database using SQLAlchemy. + + Args: + engine (Engine): SQLAlchemy engine to use. + db_uri (str): Connection URI + schema_name (str): Schema name. Defaults to "public". + + Returns: + None + """ + # Wipe and create an empty schema + with engine.connect() as connection: + connection.execute( + DropSchema(schema_name, cascade=True, if_exists=True) + ) + connection.commit() + connection.execute(CreateSchema(schema_name, if_not_exists=True)) + connection.commit() + + class Database: """Database helper class to handle ssh tunnels, waiting for PostgreSQL to be up etc. Can be used in a with statement returning a Session object that @@ -155,26 +178,3 @@ def get_session( session = Session() session.reflected_classes = classes return session - - @staticmethod - def recreate_schema( - engine: Engine, db_uri: str, schema_name: str = "public" - ) -> None: - """Wipe and create empty schema in database using SQLAlchemy. - - Args: - engine (Engine): SQLAlchemy engine to use. - db_uri (str): Connection URI - schema_name (str): Schema name. Defaults to "public". - - Returns: - None - """ - # Wipe and create an empty schema - with engine.connect() as connection: - connection.execute( - DropSchema(schema_name, cascade=True, if_exists=True) - ) - connection.commit() - connection.execute(CreateSchema(schema_name, if_not_exists=True)) - connection.commit() diff --git a/tests/hdx/database/test_database.py b/tests/hdx/database/test_database.py index 8f370fe..204e3a1 100755 --- a/tests/hdx/database/test_database.py +++ b/tests/hdx/database/test_database.py @@ -12,7 +12,7 @@ from sshtunnel import SSHTunnelForwarder from .dbtestdate import DBTestDate, date_view -from hdx.database import Database +from hdx.database import Database, recreate_schema from hdx.database.no_timezone import Base as NoTZBase from hdx.database.with_timezone import Base as TZBase @@ -143,7 +143,7 @@ def test_get_session_ssh( self, mock_psycopg, mock_SSHTunnelForwarder, mock_engine ): db_uri = "postgresql+psycopg://myuser:mypass@0.0.0.0:12345/mydatabase" - Database.recreate_schema(mock_engine, db_uri) + recreate_schema(mock_engine, db_uri) with Database( ssh_host="mysshhost", **TestDatabase.params_pg ) as dbsession: