Files
llm-intelligence/scripts/import_catalog_seed_verification.go
2026-05-29 18:48:48 +08:00

111 lines
2.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//go:build llm_script && !scripts_pkg
package main
import (
"database/sql"
"flag"
"fmt"
"os"
"strings"
_ "github.com/lib/pq"
)
const catalogSeedVerificationImporterKey = "import_catalog_seed_verification.go"
type catalogSeedVerificationConfig struct {
DryRun bool
}
func main() {
loadCatalogSeedVerificationEnv()
var cfg catalogSeedVerificationConfig
flag.BoolVar(&cfg.DryRun, "dry-run", false, "仅打印摘要,不写入数据库")
flag.Parse()
db, err := catalogSeedVerificationDB()
if err != nil {
fmt.Fprintf(os.Stderr, "open db: %v\n", err)
os.Exit(1)
}
defer db.Close()
if err := runCatalogSeedVerificationImport(db, cfg); err != nil {
fmt.Fprintf(os.Stderr, "import_catalog_seed_verification: %v\n", err)
os.Exit(1)
}
}
func loadCatalogSeedVerificationEnv() {
for _, path := range []string{".env.local", ".env"} {
data, err := os.ReadFile(path)
if err != nil {
continue
}
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
key, value, ok := strings.Cut(line, "=")
if !ok {
continue
}
key = strings.TrimSpace(key)
value = strings.Trim(strings.TrimSpace(value), `"'`)
if key == "" {
continue
}
if _, exists := os.LookupEnv(key); exists {
continue
}
_ = os.Setenv(key, value)
}
}
}
func catalogSeedVerificationDB() (*sql.DB, error) {
dsn := os.Getenv("DATABASE_URL")
if dsn == "" {
dsn = "postgres://long@/llm_intelligence?host=/var/run/postgresql"
}
return sql.Open("postgres", dsn)
}
func runCatalogSeedVerificationImport(db *sql.DB, cfg catalogSeedVerificationConfig) error {
var count int
if err := db.QueryRow(`
SELECT COUNT(*)
FROM plan_catalog_inventory
WHERE importer_key = $1
`, catalogSeedVerificationImporterKey).Scan(&count); err != nil {
return err
}
if cfg.DryRun {
fmt.Printf("source=catalog-seed-verification entries=%d dry_run=true\n", count)
return nil
}
_, err := db.Exec(`
UPDATE plan_catalog_inventory
SET plan_status = 'confirmed',
notes = CASE
WHEN position($2 in COALESCE(notes, '')) > 0 THEN notes
WHEN COALESCE(notes, '') = '' THEN $2
ELSE notes || '' || $2
END,
last_checked_at = CURRENT_TIMESTAMP,
updated_at = CURRENT_TIMESTAMP
WHERE importer_key = $1
`, catalogSeedVerificationImporterKey, strings.TrimSpace("当前链路为目录级官方入口核验,结构化公开价格待后续独立 importer 补齐。"))
if err != nil {
return err
}
fmt.Printf("source=catalog-seed-verification entries=%d dry_run=false\n", count)
return nil
}