Spaces:
Sleeping
Sleeping
Commit ·
e433935
1
Parent(s): a4a1f7f
add admin pull-db endpoint to sync DB from dataset repo
Browse files- backend/app/main.py +29 -0
backend/app/main.py
CHANGED
|
@@ -53,6 +53,35 @@ def health():
|
|
| 53 |
}
|
| 54 |
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# Serve built frontend in production (when dist exists)
|
| 57 |
if STATIC_DIR.is_dir():
|
| 58 |
app.mount("/assets", StaticFiles(directory=STATIC_DIR / "assets"), name="static-assets")
|
|
|
|
| 53 |
}
|
| 54 |
|
| 55 |
|
| 56 |
+
@app.post("/api/admin/pull-db")
|
| 57 |
+
def pull_db():
|
| 58 |
+
"""Pull latest DB from dataset repo and replace the local one."""
|
| 59 |
+
from huggingface_hub import hf_hub_download
|
| 60 |
+
import shutil
|
| 61 |
+
|
| 62 |
+
store = deps.get_store()
|
| 63 |
+
db_path = store.db_path
|
| 64 |
+
|
| 65 |
+
# Download from dataset repo
|
| 66 |
+
downloaded = hf_hub_download(
|
| 67 |
+
repo_id="Creatiste/taste-data",
|
| 68 |
+
filename="taste.db",
|
| 69 |
+
repo_type="dataset",
|
| 70 |
+
local_dir="/tmp/hf-pull",
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Close current connection, replace file, reinit
|
| 74 |
+
store.conn.close()
|
| 75 |
+
shutil.copy2(downloaded, str(db_path))
|
| 76 |
+
|
| 77 |
+
# Reinit store with new DB
|
| 78 |
+
deps.init()
|
| 79 |
+
|
| 80 |
+
new_store = deps.get_store()
|
| 81 |
+
total = new_store.conn.execute("SELECT COUNT(*) FROM products").fetchone()[0]
|
| 82 |
+
return {"ok": True, "total_products": total, "source": downloaded}
|
| 83 |
+
|
| 84 |
+
|
| 85 |
# Serve built frontend in production (when dist exists)
|
| 86 |
if STATIC_DIR.is_dir():
|
| 87 |
app.mount("/assets", StaticFiles(directory=STATIC_DIR / "assets"), name="static-assets")
|