Files
MealQuest/migrations/queries/shopping.sql
T
dev_agentandClaude Opus 5.5 1ca66931bb
CI / ci (push) Successful in 5m2s
CI / ci (pull_request) Successful in 4m29s
Generate the week's Supply run from its plan
Add internal/shopping with Generate(weekStart), one entry point for the
first generation and every regeneration. It builds one group per plan
day (dinner_id, date, position by date) and one item per ingredient of
the dinner's recipe, in ingredient order, reusing the values recipe
grounding stored: a fetched recipe's normalized name with qty and unit
joined for display, or a manual recipe's line as typed with no qty. A
day whose dinner has no recipe or no ingredients gets no group and is
reported in Skipped. Regenerating keeps the id and needed flag of each
item whose (dinner_id, name) is unchanged, matching repeated names in
order; new items are needed and items no longer planned are removed. It
is deterministic and makes no model call. A frozen list is refused.

Migration 0016 makes shopping_lists.title nullable (never empty) so a
list exists untitled until the AI titles it, and adds shopping_groups.date.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 03:58:18 -04:00

61 lines
1.9 KiB
SQL

-- name: GetShoppingList :one
-- The Supply run of the week starting week_start. Returns sql.ErrNoRows
-- when there is none.
SELECT * FROM shopping_lists
WHERE week_start = ?;
-- name: CreateShoppingList :exec
-- A new, untitled Supply run: the title is written later, by the AI.
INSERT INTO shopping_lists (id, week_start, plan_id, status, created_at, updated_at)
VALUES (?, ?, ?, 'current', ?, ?);
-- name: RegenerateShoppingList :exec
-- Records a generation of list id from plan plan_id.
UPDATE shopping_lists
SET plan_id = ?, updated_at = ?
WHERE id = ?;
-- name: ListShoppingGroups :many
-- The groups of list list_id, in list order.
SELECT * FROM shopping_groups
WHERE list_id = ?
ORDER BY position, id;
-- name: ListShoppingItems :many
-- The items of list list_id with their group's dinner, in list order: by
-- group, then by item.
SELECT sqlc.embed(shopping_items), shopping_groups.dinner_id
FROM shopping_items
JOIN shopping_groups ON shopping_groups.id = shopping_items.group_id
WHERE shopping_groups.list_id = ?
ORDER BY shopping_groups.position, shopping_items.position, shopping_items.id;
-- name: CreateShoppingGroup :exec
INSERT INTO shopping_groups (id, list_id, dinner_id, date, position, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?);
-- name: MoveShoppingGroup :exec
UPDATE shopping_groups
SET position = ?, updated_at = ?
WHERE id = ?;
-- name: DeleteShoppingGroup :exec
DELETE FROM shopping_groups
WHERE id = ?;
-- name: CreateShoppingItem :exec
-- A new item is needed until the household marks it "have it".
INSERT INTO shopping_items (id, group_id, name, qty, needed, position, created_at, updated_at)
VALUES (?, ?, ?, ?, 1, ?, ?, ?);
-- name: MoveShoppingItem :exec
-- Places item id in group group_id at position with quantity qty; its
-- name and needed flag are kept.
UPDATE shopping_items
SET group_id = ?, qty = ?, position = ?, updated_at = ?
WHERE id = ?;
-- name: DeleteShoppingItem :exec
DELETE FROM shopping_items
WHERE id = ?;