-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathlakebase.py
More file actions
60 lines (45 loc) · 1.88 KB
/
Copy pathlakebase.py
File metadata and controls
60 lines (45 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Lakebase (Databricks-managed Postgres) connection helper.
Connects using a single LAKEBASE_URL (a standard Postgres connection URL,
e.g. postgresql://role:password@host:5432/databricks_postgres?sslmode=require)
pointing at a native Postgres role with a static, non-expiring password.
This keeps setup to a single secret instead of five separate env vars.
"""
import base64
import os
from contextlib import contextmanager
import psycopg2
from databricks.sdk import WorkspaceClient
from psycopg2.extras import RealDictCursor
from sqlalchemy import create_engine
_w = WorkspaceClient()
_SCOPE = os.environ.get("LAKEBASE_SECRET_SCOPE", "database")
_KEY = os.environ.get("LAKEBASE_SECRET_KEY", "lakebase-url")
def _lakebase_url() -> str:
"""Fetch and decode the Lakebase connection URL from the Databricks secret scope."""
secret = _w.secrets.get_secret(scope=_SCOPE, key=_KEY)
return base64.b64decode(secret.value).decode("utf-8")
@contextmanager
def get_connection():
"""Yield a raw psycopg2 connection with a RealDictCursor factory."""
conn = psycopg2.connect(_lakebase_url(), cursor_factory=RealDictCursor)
try:
yield conn
finally:
conn.close()
def get_engine():
"""Return a SQLAlchemy engine for Lakebase."""
return create_engine(_lakebase_url())
def run_query(sql: str, params: tuple | dict | None = None) -> list[dict]:
"""Run a read query against Lakebase and return rows as list[dict]."""
with get_connection() as conn:
with conn.cursor() as cur:
cur.execute(sql, params)
return cur.fetchall()
def run_write(sql: str, params: tuple | dict | None = None) -> int:
"""Run an INSERT/UPDATE/DELETE against Lakebase, return affected row count."""
with get_connection() as conn:
with conn.cursor() as cur:
cur.execute(sql, params)
conn.commit()
return cur.rowcount