search for standard sites
pub-search.waow.tech
search
zig
blog
atproto
1#!/usr/bin/env -S uv run --script --quiet
2# /// script
3# requires-python = ">=3.12"
4# dependencies = ["httpx", "pydantic-settings"]
5# ///
6"""Rebuild publications_fts with base_path column for subdomain search."""
7import os
8import httpx
9from pydantic_settings import BaseSettings, SettingsConfigDict
10
11
12class Settings(BaseSettings):
13 model_config = SettingsConfigDict(
14 env_file=os.environ.get("ENV_FILE", ".env"), extra="ignore"
15 )
16 turso_url: str
17 turso_token: str
18
19 @property
20 def turso_host(self) -> str:
21 url = self.turso_url
22 if url.startswith("libsql://"):
23 url = url[len("libsql://") :]
24 return url
25
26
27settings = Settings() # type: ignore
28
29print("Rebuilding publications_fts with base_path column...")
30
31response = httpx.post(
32 f"https://{settings.turso_host}/v2/pipeline",
33 headers={
34 "Authorization": f"Bearer {settings.turso_token}",
35 "Content-Type": "application/json",
36 },
37 json={
38 "requests": [
39 {"type": "execute", "stmt": {"sql": "DROP TABLE IF EXISTS publications_fts"}},
40 {
41 "type": "execute",
42 "stmt": {
43 "sql": """
44 CREATE VIRTUAL TABLE publications_fts USING fts5(
45 uri UNINDEXED,
46 name,
47 description,
48 base_path
49 )
50 """
51 },
52 },
53 {
54 "type": "execute",
55 "stmt": {
56 "sql": """
57 INSERT INTO publications_fts (uri, name, description, base_path)
58 SELECT uri, name, COALESCE(description, ''), COALESCE(base_path, '')
59 FROM publications
60 """
61 },
62 },
63 {"type": "execute", "stmt": {"sql": "SELECT COUNT(*) FROM publications_fts"}},
64 {"type": "close"},
65 ]
66 },
67 timeout=60,
68)
69response.raise_for_status()
70data = response.json()
71
72for i, result in enumerate(data["results"][:-1]): # skip close
73 if result["type"] == "error":
74 print(f"Step {i} error: {result['error']}")
75 elif result["type"] == "ok":
76 if i == 3: # count query
77 rows = result["response"]["result"].get("rows", [])
78 if rows:
79 count = (
80 rows[0][0].get("value", rows[0][0])
81 if isinstance(rows[0][0], dict)
82 else rows[0][0]
83 )
84 print(f"Rebuilt with {count} publications")
85
86print("Done!")