Files
ThothII/harness/tht/db/connection.py
T
marcopan fc5fbe6b65 refactor(harness): renaming prodotto tht (Onda -1)
Thoth (tht) è il prodotto, PSD è il cliente. Nessun riferimento al contesto
clinico nel codice.

Rinomine:
- comando+package nsp→tht (dir nsp/→tht/, 46 import, pyproject entry point)
- gate nsp-gate.js→tht-gate.js (+ rewrite token, relayIfNspFails→relayIfThtFails)
- workspace chirone.{example,test}.yaml→tht.{example,test}.yaml (generici)
- env THOTH_→THT_ (19 var) + NSP_ stragglers (NSP_HARNESS_ROOT, NSP_SESSION)
- commenti/docstring chirone/psdwp3/policlinico neutralizzati ('the reference
  implementation', 'the DWH')

Aggiunto [tool.setuptools.packages.find] include=['tht*'] (necessario: l'auto-
discovery rompeva con tht/ + workspaces/ come top-level multipli).

.env operatore aggiornato in-place (prefissi THT_, valori preservati, gitignored).

Verifica: pytest 109 passed, npm test 14 pass, tht phase meta --json OK, zero
residui nsp/THOTH_/NSP_/chirone nel package.
2026-06-27 10:33:16 +02:00

38 lines
1.2 KiB
Python

from sqlalchemy import Engine, create_engine, text
from tht.config import DatabaseConfig
def make_engine(cfg: DatabaseConfig) -> Engine:
url = (
f"postgresql+psycopg2://{cfg.user}:{cfg.password}"
f"@{cfg.host}:{cfg.port}/{cfg.database}"
)
return create_engine(url, echo=False)
def ping(engine: Engine) -> None:
with engine.connect() as conn:
conn.execute(text("SELECT 1"))
def writable_tables(engine: Engine, schema: str) -> list[str]:
"""Tabelle dello schema su cui l'utente corrente ha privilegi di scrittura."""
q = text("""
SELECT c.relname
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = :schema
AND c.relkind IN ('r', 'p')
AND has_table_privilege(current_user, c.oid, 'INSERT, UPDATE, DELETE')
ORDER BY c.relname
""")
with engine.connect() as conn:
return [row[0] for row in conn.execute(q, {"schema": schema})]
def can_create_in_schema(engine: Engine, schema: str) -> bool:
q = text("SELECT has_schema_privilege(current_user, :schema, 'CREATE')")
with engine.connect() as conn:
return bool(conn.execute(q, {"schema": schema}).scalar())