-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.py
More file actions
239 lines (191 loc) · 7.42 KB
/
db.py
File metadata and controls
239 lines (191 loc) · 7.42 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import os
from contextlib import contextmanager
from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple
# Lazy imports inside functions to avoid hard dependency if not used
def _is_postgres() -> bool:
url = os.getenv("DATABASE_URL") or os.getenv("DB_URL") or ""
backend = os.getenv("DB_BACKEND", "").lower()
return backend == "postgres" or url.startswith("postgresql://") or url.startswith("postgres://")
def _get_sqlite_path() -> str:
return os.getenv("DB_PATH", "comparisons.db")
def autoincrement_pk_sql() -> str:
"""Return backend-appropriate SQL for an auto-incrementing primary key column.
Example usage: f"id {autoincrement_pk_sql()}"
"""
if _is_postgres():
return "SERIAL PRIMARY KEY"
else:
return "INTEGER PRIMARY KEY AUTOINCREMENT"
def bool_default(value: bool) -> str:
"""Return backend-appropriate DEFAULT for boolean columns."""
if _is_postgres():
return "DEFAULT TRUE" if value else "DEFAULT FALSE"
else:
return "DEFAULT 1" if value else "DEFAULT 0"
def _convert_placeholders(sql: str) -> str:
"""Convert SQLite-style '?' placeholders to backend-specific ones.
- SQLite: '?' (no change)
- Postgres (psycopg): '%s'
"""
if _is_postgres():
# Replace each '?' with '%s'. We assume '?' only used for placeholders in our SQL.
return sql.replace("?", "%s")
return sql
@contextmanager
def cursor_adapter():
"""Yield a connection and a cursor that auto-convert placeholders.
Use this when you need a raw cursor but still want portable '?' placeholders.
Yields: (conn, cursor)
"""
with connect() as (conn, cursor):
class AdapterCursor:
def __init__(self, real):
self._c = real
def execute(self, sql, params=None):
sql_conv = _convert_placeholders(sql)
if params is None:
return self._c.execute(sql_conv)
return self._c.execute(sql_conv, params)
def executemany(self, sql, seq_of_params):
sql_conv = _convert_placeholders(sql)
return self._c.executemany(sql_conv, seq_of_params)
def fetchone(self):
return self._c.fetchone()
def fetchall(self):
return self._c.fetchall()
@property
def rowcount(self):
return self._c.rowcount
@property
def lastrowid(self):
return getattr(self._c, "lastrowid", None)
def __getattr__(self, item):
return getattr(self._c, item)
yield conn, AdapterCursor(cursor)
@contextmanager
def connect(dict_rows: bool = False):
"""Context manager yielding a DB connection and cursor.
Args:
dict_rows: If True, rows are returned as dict-like mappings when supported.
Yields:
(conn, cursor)
"""
if _is_postgres():
dsn = os.getenv("DATABASE_URL") or os.getenv("DB_URL")
if not dsn:
raise RuntimeError(
(
"DATABASE_URL/DB_URL not set for Postgres backend. "
"Set DB_BACKEND=sqlite to use SQLite."
)
)
# Determine driver once (do not catch exceptions thrown from with-body)
driver = None
try:
import psycopg # type: ignore
from psycopg.rows import dict_row # type: ignore
driver = ("psycopg3", psycopg, dict_row)
except Exception:
try:
import psycopg2 # type: ignore
from psycopg2.extras import RealDictCursor # type: ignore
driver = ("psycopg2", psycopg2, RealDictCursor)
except Exception:
raise
name, mod, helper = driver
if name == "psycopg3":
# helper is dict_row
if dict_rows:
conn = mod.connect(dsn, row_factory=helper)
else:
conn = mod.connect(dsn)
try:
yield conn, conn.cursor()
finally:
conn.close()
else:
# psycopg2 path; helper is RealDictCursor
conn = mod.connect(dsn)
cur = conn.cursor(cursor_factory=helper) if dict_rows else conn.cursor()
try:
yield conn, cur
finally:
conn.close()
else:
import sqlite3
path = _get_sqlite_path()
conn = sqlite3.connect(path)
try:
if dict_rows:
conn.row_factory = sqlite3.Row
yield conn, conn.cursor()
finally:
conn.close()
def execute(sql: str, params: Sequence[Any] = ()) -> None:
sql_conv = _convert_placeholders(sql)
with connect() as (conn, cur):
cur.execute(sql_conv, params)
conn.commit()
def executemany(sql: str, param_list: Iterable[Sequence[Any]]) -> None:
sql_conv = _convert_placeholders(sql)
with connect() as (conn, cur):
cur.executemany(sql_conv, param_list)
conn.commit()
def execute_with_rowcount(sql: str, params: Sequence[Any] = ()) -> int:
"""Execute a DML statement and return affected rowcount."""
sql_conv = _convert_placeholders(sql)
with connect() as (conn, cur):
cur.execute(sql_conv, params)
rc = getattr(cur, "rowcount", -1) or 0
conn.commit()
return int(rc)
def query(sql: str, params: Sequence[Any] = ()) -> List[Tuple[Any, ...]]:
sql_conv = _convert_placeholders(sql)
with connect() as (conn, cur):
cur.execute(sql_conv, params)
rows = cur.fetchall()
return rows
def query_one(sql: str, params: Sequence[Any] = ()) -> Optional[Tuple[Any, ...]]:
sql_conv = _convert_placeholders(sql)
with connect() as (conn, cur):
cur.execute(sql_conv, params)
row = cur.fetchone()
return row
def query_dicts(sql: str, params: Sequence[Any] = ()) -> List[Dict[str, Any]]:
sql_conv = _convert_placeholders(sql)
with connect(dict_rows=True) as (conn, cur):
cur.execute(sql_conv, params)
rows = cur.fetchall()
# sqlite returns sqlite3.Row which behaves like a mapping; psycopg returns dicts directly
# Normalize to plain dicts
result: List[Dict[str, Any]] = []
for r in rows:
if isinstance(r, dict):
result.append({str(k): v for k, v in r.items()})
elif hasattr(r, "keys"):
# sqlite3.Row
keys = list(r.keys()) # type: ignore[call-arg]
result.append({str(k): r[k] for k in keys})
else:
# Fallback: represent as indexed string keys
try:
seq = list(r)
result.append({str(i): v for i, v in enumerate(seq)})
except Exception:
result.append({})
return result
def init_migrations_table_if_needed() -> None:
"""Ensure the migrations table exists with backend-appropriate schema."""
# Note: Use explicit DDL for PK based on backend
ddl = f"""
CREATE TABLE IF NOT EXISTS migrations (
id {autoincrement_pk_sql()},
version TEXT NOT NULL,
name TEXT NOT NULL,
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
success BOOLEAN {bool_default(False)}
)
"""
execute(ddl)
def backend_name() -> str:
return "postgres" if _is_postgres() else "sqlite"