Scope the season's plant lookup to the season
Build image / build-and-push (push) Successful in 4s

ListReferencedPlants(includeRemoved=true) returned every plant the garden has
ever held, so drawing one season loaded a decade of catalog on a decade-old
garden. It also meant the plant list and the plop list were selected by different
rules, which is the kind of near-miss that stays correct only by accident.

It now takes the same year the plops do, so the two selections always agree.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01H3zbym8Doka2d7D48maSgZ
This commit is contained in:
2026-07-21 01:47:14 -04:00
co-authored by Claude Opus 4.8
parent 1029aff2c7
commit 34ff4b99f5
3 changed files with 72 additions and 10 deletions
+15 -9
View File
@@ -27,21 +27,27 @@ func scanPlant(s scanner) (*domain.Plant, error) {
// ListReferencedPlants returns the distinct plants used by a garden's plantings
// — the catalog subset the editor needs to render them. Always a non-nil slice.
//
// includeRemoved widens it past the active plops to every plop the garden has
// ever held, which is what a past season needs: a plant pulled last July is not
// active, but its plops still have to render with the right icon and color.
func (d *DB) ListReferencedPlants(ctx context.Context, gardenID int64, includeRemoved bool) ([]domain.Plant, error) {
activeOnly := ` AND pl.removed_at IS NULL`
if includeRemoved {
activeOnly = ``
// year nil means the active plops only. A year widens it to the plops that were
// in the ground THAT YEAR, which is what a past season needs: a plant pulled
// last July is not active, but its plops still have to render with the right
// icon and colour. It is scoped to the same year as the plops rather than to the
// garden's whole history, so the two selections always agree and a decade-old
// garden doesn't load a decade of catalog to draw one season.
func (d *DB) ListReferencedPlants(ctx context.Context, gardenID int64, year *int) ([]domain.Plant, error) {
where := ` AND pl.removed_at IS NULL`
args := []any{gardenID}
if year != nil {
where = ` AND (pl.planted_at IS NULL OR pl.planted_at <= ?)
AND (pl.removed_at IS NULL OR pl.removed_at >= ?)`
args = append(args, fmt.Sprintf("%04d-12-31", *year), fmt.Sprintf("%04d-01-01", *year))
}
rows, err := d.sql.QueryContext(ctx,
`SELECT DISTINCT `+qualifyColumns("p", plantColumns)+` FROM plants p
JOIN plantings pl ON pl.plant_id = p.id
JOIN garden_objects o ON o.id = pl.object_id
WHERE o.garden_id = ?`+activeOnly+`
WHERE o.garden_id = ?`+where+`
ORDER BY p.id`,
gardenID,
args...,
)
if err != nil {
return nil, fmt.Errorf("store: list referenced plants: %w", err)