Skip to content

Commit

Permalink
Move Base for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike committed Mar 5, 2023
1 parent c9029f5 commit f05a7f8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
14 changes: 10 additions & 4 deletions doc/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Additional postgresql functionality is available if this library is installed wi

## Breaking changes

From 1.2.3, Base must be chosen from `hdx.database.no_timezone`
(`db_has_tz=False`: the default) or `hdx.database.with_timezone`
(`db_has_tz=True`).

From 1.2.2, database datetime columns are assumed to be timezoneless unless
db_has_tz is set to True.

Expand All @@ -38,9 +42,9 @@ Versions from 1.0.6 no longer support Python 2.7.
## Database

Your SQLAlchemy database tables must inherit from `Base` in
`hdx.database` or `hdx.database.database_notz` eg. :
`hdx.database.no_timezone` or `hdx.database.with_timezone` eg. :

from hdx.database import Base
from hdx.database.no_timezone import Base
class MyTable(Base):
my_col = Column(Integer, ForeignKey(MyTable2.col2), primary_key=True)

Expand All @@ -58,8 +62,10 @@ tunnel:
session.query(...)

`db_has_tz` which defaults to `False` indicates whether database datetime
columns have timezones. If not, conversion occurs between Python datetimes
with timezones to timezoneless database columns.
columns have timezones. If `db_has_tz` is `True`, use `Base` from
`hdx.database.with_timezone`, otherwise use `Base` from
`hdx.database.no_timezone`. If `db_has_tz` is `False`, conversion occurs
between Python datetimes with timezones to timezoneless database columns.

## Connection URI

Expand Down
10 changes: 2 additions & 8 deletions src/hdx/database/__init__.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
from ._version import version as __version__ # noqa: F401
from .dburi import get_connection_uri

"""Database utilities"""
import logging
from typing import Any, Optional, Type, Union

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker
from sqlalchemy.pool import NullPool
from sshtunnel import SSHTunnelForwarder

from .dburi import get_connection_uri
from .no_timezone import Base as NoTZBase
from .postgresql import wait_for_postgresql
from .with_timezone import Base

logger = logging.getLogger(__name__)


class Base(DeclarativeBase):
@declared_attr.directive
def __tablename__(cls):
return f"{cls.__name__.lower()}s"


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.
Expand Down
7 changes: 7 additions & 0 deletions src/hdx/database/with_timezone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from sqlalchemy.orm import DeclarativeBase, declared_attr


class Base(DeclarativeBase):
@declared_attr.directive
def __tablename__(cls):
return f"{cls.__name__.lower()}s"

0 comments on commit f05a7f8

Please sign in to comment.