Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
Move method to function
  • Loading branch information
mcarans committed May 6, 2024
1 parent 702b489 commit d661153
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 45 deletions.
7 changes: 6 additions & 1 deletion documentation/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
26 changes: 7 additions & 19 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -37,33 +33,25 @@ 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
# via cffi
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
46 changes: 23 additions & 23 deletions src/hdx/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
4 changes: 2 additions & 2 deletions tests/hdx/database/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit d661153

Please sign in to comment.