feat: add admin/internal database API with CRUD, read-only query, and natural-language SQL endpoints
Add a new `/dbapi` router package providing a generic database API over `dataset`, restricted to admin sessions, admin API keys, and the internal gateway key. Includes: - `tables.py`: list all tables and inspect table schemas - `crud.py`: full CRUD operations (GET, POST, PATCH, DELETE) with soft-delete awareness, born-live inserts, `?include_deleted`, `.../restore`, and `?hard=true` purge - `query.py`: validated read-only SELECT execution via sqlglot parsing, classification, and EXPLAIN dry-run; async query jobs with WebSocket streaming via `DbApiJobService` - `nl.py`: natural-language-to-SQL conversion using the platform AI gateway with re-prompting until validation passes Also register `DbApiJobService` and `PubSubService` in the service manager, add `DBAPI_DIR` to config data paths, and force cleartext `http://` connections to HTTP/1.1 in `curl_transport` to fix large request failures against uvicorn's HTTP/1.1-only internal gateway.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
|
||||
def test_nl_requires_auth(client):
|
||||
assert (
|
||||
client.post("/dbapi/nl", json={"question": "x", "table": "users"}).status_code
|
||||
== 403
|
||||
)
|
||||
|
||||
|
||||
def test_nl_missing_fields_is_400(client, auth):
|
||||
assert client.post("/dbapi/nl", json={"question": "x"}, headers=auth).status_code == 400
|
||||
|
||||
|
||||
def test_nl_denied_table_is_404(client, auth):
|
||||
response = client.post(
|
||||
"/dbapi/nl", json={"question": "x", "table": "sessions"}, headers=auth
|
||||
)
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_nl_is_graceful_when_gateway_fails(client, auth, monkeypatch):
|
||||
async def _boom(*args, **kwargs):
|
||||
raise RuntimeError("gateway down")
|
||||
|
||||
monkeypatch.setattr(
|
||||
"devplacepy.services.dbapi.nl2sql._complete", _boom
|
||||
)
|
||||
response = client.post(
|
||||
"/dbapi/nl",
|
||||
json={"question": "all administrators", "table": "users"},
|
||||
headers=auth,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
body = response.json()
|
||||
assert body["valid"] is False
|
||||
assert body["error"]
|
||||
Reference in New Issue
Block a user