50 lines
1.0 KiB
Go
50 lines
1.0 KiB
Go
// Command seed inserts test tag codes TEST000001..TEST000025.
|
|
// Idempotent: already-existing codes are skipped.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"wherewoof/frontend/internal/db"
|
|
)
|
|
|
|
const count = 25
|
|
|
|
func main() {
|
|
dsn := os.Getenv("DATABASE_URL")
|
|
if dsn == "" {
|
|
fmt.Fprintln(os.Stderr, "error: DATABASE_URL not set")
|
|
os.Exit(1)
|
|
}
|
|
|
|
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)
|
|
for i := 1; i <= count; i++ {
|
|
code := fmt.Sprintf("TEST%06d", i)
|
|
if _, err := q.InsertTag(ctx, code); err != nil {
|
|
var pgErr *pgconn.PgError
|
|
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
|
|
fmt.Println("skip (exists)", code)
|
|
continue
|
|
}
|
|
fmt.Fprintln(os.Stderr, "insert", code, ":", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("seeded", code)
|
|
}
|
|
fmt.Printf("done: %d test tags available\n", count)
|
|
}
|