tag-registry-product-link: seed 100 real preset IDs (seed-registry cmd), products/orders schema + tag linkage, fix case-sensitive tag-code bind
This commit is contained in:
75
frontend/cmd/seed-registry/main.go
Normal file
75
frontend/cmd/seed-registry/main.go
Normal file
@@ -0,0 +1,75 @@
|
||||
// Command seed-registry inserts the real preset tag IDs from
|
||||
// db/preset_tag_ids.txt into the tags table (the anti-scam registry).
|
||||
// Idempotent: already-existing IDs are skipped.
|
||||
// Usage: go run ./cmd/seed-registry -registry ../db/preset_tag_ids.txt
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"wherewoof/frontend/internal/db"
|
||||
)
|
||||
|
||||
func main() {
|
||||
registryPath := flag.String("registry", "../db/preset_tag_ids.txt", "path to preset tag ID file")
|
||||
flag.Parse()
|
||||
|
||||
dsn := os.Getenv("DATABASE_URL")
|
||||
if dsn == "" {
|
||||
fmt.Fprintln(os.Stderr, "error: DATABASE_URL not set")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
f, err := os.Open(*registryPath)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error opening registry:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, dsn)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "connect:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
q := db.New(pool)
|
||||
seeded, skipped, errs := 0, 0, 0
|
||||
sc := bufio.NewScanner(f)
|
||||
for sc.Scan() {
|
||||
line := strings.TrimSpace(sc.Text())
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
if _, err := q.InsertTag(ctx, line); err != nil {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
fmt.Fprintln(os.Stderr, "insert", line, ":", err)
|
||||
errs++
|
||||
continue
|
||||
}
|
||||
seeded++
|
||||
}
|
||||
if err := sc.Err(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "read:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("registry seed: %d inserted, %d already existed, %d errors\n", seeded, skipped, errs)
|
||||
if errs > 0 {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user