fix(catalogseed): cast v.id to bigint in bulkUpdatePeople (#274)
A standalone VALUES clause gives Postgres no target column to infer parameter types from, so v.id defaulted to text and the UPDATE ... FROM join predicate became bigint = text, failing seed imports with SQLSTATE 42883 during the people-update phase. Fixes #267 Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
committed by
GitHub
co-authored by
Claude Fable 5
parent
d590eda22a
commit
631d95530a
@@ -1828,7 +1828,10 @@ func bulkUpdatePeople(ctx context.Context, tx pgx.Tx, people map[int64]*importPe
|
||||
FROM (VALUES `,
|
||||
rows,
|
||||
7,
|
||||
nil,
|
||||
// A standalone VALUES clause has no target column to infer types
|
||||
// from, so v.id must be cast explicitly or it defaults to text and
|
||||
// the join predicate becomes bigint = text (SQLSTATE 42883).
|
||||
map[int]string{0: "::bigint"},
|
||||
`) AS v(id, tmdb_id, imdb_id, tvdb_id, plex_guid, photo_path, photo_thumbhash)
|
||||
WHERE p.id = v.id`,
|
||||
nil,
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package catalogseed
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func newCatalogSeedTestPool(t *testing.T) *pgxpool.Pool {
|
||||
t.Helper()
|
||||
dsn := os.Getenv("SILO_TEST_DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("SILO_TEST_DATABASE_URL is not set")
|
||||
}
|
||||
pool, err := pgxpool.New(context.Background(), dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("connect test database: %v", err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
return pool
|
||||
}
|
||||
|
||||
func TestBulkUpdatePeopleComparesIDsAsBigint(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
pool := newCatalogSeedTestPool(t)
|
||||
|
||||
tx, err := pool.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("begin tx: %v", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
personID := time.Now().UnixNano()
|
||||
if _, err := tx.Exec(ctx, `
|
||||
INSERT INTO people (id, name, sort_name, tmdb_id)
|
||||
VALUES ($1, 'Import Test Person', 'import test person', '111')
|
||||
`, personID); err != nil {
|
||||
t.Fatalf("seed person: %v", err)
|
||||
}
|
||||
|
||||
people := map[int64]*importPersonState{
|
||||
personID: {
|
||||
ID: personID,
|
||||
Name: "Import Test Person",
|
||||
TmdbID: "222",
|
||||
NeedsUpdate: true,
|
||||
},
|
||||
}
|
||||
if err := bulkUpdatePeople(ctx, tx, people); err != nil {
|
||||
t.Fatalf("bulkUpdatePeople: %v", err)
|
||||
}
|
||||
|
||||
var tmdbID string
|
||||
if err := tx.QueryRow(ctx, `SELECT tmdb_id FROM people WHERE id = $1`, personID).Scan(&tmdbID); err != nil {
|
||||
t.Fatalf("read back person: %v", err)
|
||||
}
|
||||
if tmdbID != "222" {
|
||||
t.Fatalf("tmdb_id = %q, want %q", tmdbID, "222")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user