Files
2026-05-21 18:26:30 +02:00

41 lines
1.4 KiB
Python

#!/usr/bin/env python3
"""Remove project prefix `autingo-159109.` from cards 10221-10229.
Reason: Metabase field-filter substitution emits `psql_dcpublic.<table>.<col>` (no project).
BigQuery rejects since the rest of the SQL uses `autingo-159109.psql_dcpublic.<table>`.
Mixed qualifiers in same query break.
"""
import subprocess
import httpx
API_KEY = subprocess.check_output(["pass", "show", "metabase/aurgi-api-key"], text=True).strip().splitlines()[0]
BASE = "https://reports.autingo.es"
client = httpx.Client(base_url=BASE, headers={"x-api-key": API_KEY}, timeout=120)
for cid in range(10221, 10230):
card = client.get(f"/api/card/{cid}").json()
dq = card["dataset_query"]
# MBQL5 stages format: dq["stages"][0]["native"] is the string, "template-tags" lives at stage level
stage = dq["stages"][0]
sql = stage["native"]
new_sql = sql.replace("`autingo-159109.psql_dcpublic.", "`psql_dcpublic.")
if new_sql == sql:
print(f" {cid}: no change")
continue
# Rewrite as legacy native query (Metabase writes legacy on PUT)
new_dq = {
"type": "native",
"database": dq.get("database", 6),
"native": {
"query": new_sql,
"template-tags": stage.get("template-tags", {}),
},
}
r = client.put(f"/api/card/{cid}", json={"dataset_query": new_dq})
r.raise_for_status()
print(f" {cid}: updated")
client.close()
print("done")