refactor(dwh): adapt direct and REST transports

This commit is contained in:
2026-07-11 20:03:31 +02:00
parent a4eb6cc9e5
commit 717e5ecced
6 changed files with 193 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
"""Data-warehouse adapter implementations."""
from tht.adapters.dwh.postgres import PostgresDwhAdapter
from tht.adapters.dwh.thoth_rest import ThothRestDwhAdapter
__all__ = ["PostgresDwhAdapter", "ThothRestDwhAdapter"]
+41
View File
@@ -0,0 +1,41 @@
"""Direct PostgreSQL implementation of the DWH port."""
from tht.config import DatabaseConfig
from tht.db import execute
from tht.db.connection import make_engine, ping
from tht.db.introspect import introspect
from tht.execute import ExecResult, PlanSummary
from tht.mschema.models import PhysicalSchema
from tht.ports.dwh import DwhCapabilities, DwhHealth
class PostgresDwhAdapter:
capabilities = DwhCapabilities()
def __init__(self, config: DatabaseConfig):
self._config = config
self._engine = make_engine(config)
def health(self) -> DwhHealth:
try:
ping(self._engine)
except Exception as exc:
return DwhHealth(ok=False, detail=str(exc))
return DwhHealth(ok=True)
def introspect(self) -> PhysicalSchema:
return introspect(self._engine, self._config.database, self._config.db_schema)
def run_query(self, sql: str, *, limit: int | None = None) -> ExecResult:
return execute.run_query(self._engine, sql, limit=limit)
def explain(self, sql: str) -> PlanSummary:
return execute.explain(self._engine, sql)
def sample_column(self, table: str, column: str, *, limit: int) -> list[object]:
return execute.sample_column(
self._engine, self._config.db_schema, table, column, limit=limit
)
def distinct_values(self, table: str, column: str) -> list[object]:
return execute.distinct_values(self._engine, self._config.db_schema, table, column)
+51
View File
@@ -0,0 +1,51 @@
"""Thoth/PostgREST implementation of the DWH port."""
from tht.config import DatabaseConfig, RestConfig
from tht.db.introspect import introspect_rest
from tht.execute import ExecResult, PlanSummary
from tht.mschema.models import PhysicalSchema
from tht.ports.dwh import DwhCapabilities, DwhHealth
from tht.rest.client import RestClient, RestError
from tht.rest.execute import (
distinct_values_rest,
explain_rest,
run_controlled_rest,
sample_column_rest,
)
class ThothRestDwhAdapter:
capabilities = DwhCapabilities()
def __init__(self, database: DatabaseConfig, rest: RestConfig):
self._database = database
self._client = RestClient(rest)
def health(self) -> DwhHealth:
try:
result = self._client.ping()
except RestError as exc:
return DwhHealth(ok=False, detail=str(exc))
ok = bool(result.get("db_connected", True))
return DwhHealth(ok=ok, detail=None if ok else str(result))
def introspect(self) -> PhysicalSchema:
return introspect_rest(
self._client, self._database.database, self._database.db_schema
)
def run_query(self, sql: str, *, limit: int | None = None) -> ExecResult:
return run_controlled_rest(self._client, sql, limit=10 if limit is None else limit)
def explain(self, sql: str) -> PlanSummary:
return explain_rest(self._client, sql)
def sample_column(self, table: str, column: str, *, limit: int) -> list[object]:
return sample_column_rest(
self._client, self._database.db_schema, table, column, limit=limit
)
def distinct_values(self, table: str, column: str) -> list[object]:
return distinct_values_rest(
self._client, self._database.db_schema, table, column
)