Co-authored-by: orchestrator-agent <20+orchestrator-agent@code.cubecraftcreations.com>
This commit was merged in pull request #110.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
-- 0012_suggestion_name: a suggestion keeps the name its dish was suggested
|
||||
-- under. Its dinner can be renamed, even once archived by a Pass, so the
|
||||
-- declined names every later batch refuses are read from here, never from
|
||||
-- the dinner. A suggestion stored before this takes its dinner's name now.
|
||||
-- SQLite cannot add a NOT NULL column without a default, so the table is
|
||||
-- rebuilt as 0005 does: copy the rows aside, drop the table, create it
|
||||
-- again, copy the rows back and drop the copy. Nothing references
|
||||
-- suggestions.
|
||||
|
||||
CREATE TABLE suggestions_copy AS SELECT * FROM suggestions;
|
||||
|
||||
DROP TABLE suggestions;
|
||||
|
||||
CREATE TABLE suggestions (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
dinner_id TEXT NOT NULL REFERENCES dinners (id),
|
||||
batch_id TEXT NOT NULL REFERENCES suggestion_batches (id),
|
||||
name TEXT NOT NULL CHECK (name <> ''),
|
||||
reason TEXT NOT NULL,
|
||||
cited_regular_ids TEXT NOT NULL, -- JSON array of dinner ids
|
||||
status TEXT NOT NULL CHECK (status IN ('pending', 'accepted', 'declined')),
|
||||
decided_at TEXT,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
INSERT INTO suggestions (
|
||||
id, dinner_id, batch_id, name, reason, cited_regular_ids, status, decided_at, created_at, updated_at
|
||||
)
|
||||
SELECT
|
||||
suggestions_copy.id, suggestions_copy.dinner_id, suggestions_copy.batch_id, dinners.name,
|
||||
suggestions_copy.reason, suggestions_copy.cited_regular_ids, suggestions_copy.status,
|
||||
suggestions_copy.decided_at, suggestions_copy.created_at, suggestions_copy.updated_at
|
||||
FROM suggestions_copy
|
||||
JOIN dinners ON dinners.id = suggestions_copy.dinner_id;
|
||||
|
||||
DROP TABLE suggestions_copy;
|
||||
@@ -19,6 +19,19 @@ SELECT * FROM dinners
|
||||
WHERE status = ?
|
||||
ORDER BY normalized_name, id;
|
||||
|
||||
-- name: ListUnplannedQuestLogDinners :many
|
||||
-- The Quest log: dinners with status quest_log that no plan for the week
|
||||
-- starting from_week_start or later has on any day.
|
||||
SELECT * FROM dinners
|
||||
WHERE status = 'quest_log'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM plan_days
|
||||
JOIN week_plans ON week_plans.id = plan_days.plan_id
|
||||
WHERE plan_days.dinner_id = dinners.id
|
||||
AND week_plans.week_start >= sqlc.arg(from_week_start)
|
||||
)
|
||||
ORDER BY normalized_name, id;
|
||||
|
||||
-- name: CountRegularDinners :one
|
||||
-- How many Regulars the household has; archived dinners are not counted.
|
||||
SELECT COUNT(*) FROM dinners
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
-- Reads for the AI household context block (internal/ai/harness).
|
||||
|
||||
-- name: ListRecentDeclinedNames :many
|
||||
-- The names of the most recently declined suggestions, newest first.
|
||||
SELECT dinners.name
|
||||
-- The names the most recently declined suggestions were suggested under,
|
||||
-- newest first; renaming their dinners does not change them.
|
||||
SELECT name
|
||||
FROM suggestions
|
||||
JOIN dinners ON dinners.id = suggestions.dinner_id
|
||||
WHERE suggestions.status = 'declined'
|
||||
ORDER BY suggestions.decided_at DESC, suggestions.id DESC
|
||||
WHERE status = 'declined'
|
||||
ORDER BY decided_at DESC, id DESC
|
||||
LIMIT ?;
|
||||
|
||||
-- name: ListPlannedDinners :many
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
-- name: CreateSuggestionBatch :one
|
||||
INSERT INTO suggestion_batches (id, trigger, status, created_at, updated_at)
|
||||
VALUES (?, ?, 'pending', ?, ?)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetActiveSuggestionBatch :one
|
||||
-- The batch still pending or running, oldest first. Returns sql.ErrNoRows
|
||||
-- when there is none.
|
||||
SELECT * FROM suggestion_batches
|
||||
WHERE status IN ('pending', 'running')
|
||||
ORDER BY id
|
||||
LIMIT 1;
|
||||
|
||||
-- name: GetLatestSuggestionBatch :one
|
||||
-- The newest batch, whatever its status. Returns sql.ErrNoRows when there
|
||||
-- is none.
|
||||
SELECT * FROM suggestion_batches
|
||||
ORDER BY id DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- name: GetSuggestionBatchJobID :one
|
||||
-- The id of the job that runs batch batch_id.
|
||||
SELECT id FROM jobs
|
||||
WHERE kind = sqlc.arg(kind)
|
||||
AND json_extract(payload, '$.batch_id') = sqlc.arg(batch_id)
|
||||
ORDER BY id
|
||||
LIMIT 1;
|
||||
|
||||
-- name: StartSuggestionBatch :execrows
|
||||
-- Marks a pending batch running; a batch in any other status is left as
|
||||
-- it is (0 rows).
|
||||
UPDATE suggestion_batches
|
||||
SET status = 'running', updated_at = ?
|
||||
WHERE id = ? AND status = 'pending';
|
||||
|
||||
-- name: FinishSuggestionBatch :execrows
|
||||
-- Records a pending or running batch as done or failed; a batch already
|
||||
-- finished is left as it is (0 rows).
|
||||
UPDATE suggestion_batches
|
||||
SET status = ?, updated_at = ?
|
||||
WHERE id = ? AND status IN ('pending', 'running');
|
||||
|
||||
-- name: FailOrphanedSuggestionBatches :execrows
|
||||
-- Fails every batch still pending or running whose job is no longer queued
|
||||
-- or running: the process stopped, or recording the batch's end failed,
|
||||
-- before its handler recorded a result. Run before reading the active batch.
|
||||
UPDATE suggestion_batches
|
||||
SET status = 'failed', updated_at = sqlc.arg(updated_at)
|
||||
WHERE status IN ('pending', 'running')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM jobs
|
||||
WHERE jobs.kind = sqlc.arg(kind)
|
||||
AND jobs.status IN ('queued', 'running')
|
||||
AND json_extract(jobs.payload, '$.batch_id') = suggestion_batches.id
|
||||
);
|
||||
|
||||
-- name: CreateSuggestion :exec
|
||||
-- name is the name the dish is suggested under, kept whatever its dinner is
|
||||
-- later renamed.
|
||||
INSERT INTO suggestions (
|
||||
id, dinner_id, batch_id, name, reason, cited_regular_ids, status, created_at, updated_at
|
||||
) VALUES (
|
||||
?, ?, ?, ?, ?, ?, 'pending', ?, ?
|
||||
);
|
||||
|
||||
-- name: GetSuggestion :one
|
||||
-- A suggestion with its dinner. Returns sql.ErrNoRows for an unknown id.
|
||||
SELECT sqlc.embed(suggestions), sqlc.embed(dinners)
|
||||
FROM suggestions
|
||||
JOIN dinners ON dinners.id = suggestions.dinner_id
|
||||
WHERE suggestions.id = ?;
|
||||
|
||||
-- name: ListDeck :many
|
||||
-- The pending suggestions whose dinner is still suggested, oldest first,
|
||||
-- with their dinners.
|
||||
SELECT sqlc.embed(suggestions), sqlc.embed(dinners)
|
||||
FROM suggestions
|
||||
JOIN dinners ON dinners.id = suggestions.dinner_id
|
||||
WHERE suggestions.status = 'pending' AND dinners.status = 'suggested'
|
||||
ORDER BY suggestions.created_at, suggestions.id;
|
||||
|
||||
-- name: CountDeck :one
|
||||
-- How many suggestions ListDeck returns.
|
||||
SELECT COUNT(*)
|
||||
FROM suggestions
|
||||
JOIN dinners ON dinners.id = suggestions.dinner_id
|
||||
WHERE suggestions.status = 'pending' AND dinners.status = 'suggested';
|
||||
|
||||
-- name: ListDeclinedSuggestionNames :many
|
||||
-- The name every declined suggestion was suggested under, oldest decision
|
||||
-- first; renaming its dinner does not change it.
|
||||
SELECT name
|
||||
FROM suggestions
|
||||
WHERE status = 'declined'
|
||||
ORDER BY decided_at, id;
|
||||
|
||||
-- name: DecideSuggestion :execrows
|
||||
-- Records the household's decision on a pending suggestion; one already
|
||||
-- decided is left as it is (0 rows).
|
||||
UPDATE suggestions
|
||||
SET status = sqlc.arg(status), decided_at = sqlc.arg(now), updated_at = sqlc.arg(now)
|
||||
WHERE id = sqlc.arg(id) AND status = 'pending';
|
||||
|
||||
-- name: MoveSuggestedDinner :execrows
|
||||
-- Moves a dinner still suggested to status (quest_log or archived),
|
||||
-- recording updated_by; a dinner no longer suggested is left as it is
|
||||
-- (0 rows).
|
||||
UPDATE dinners
|
||||
SET status = sqlc.arg(status), updated_by = sqlc.arg(updated_by), updated_at = sqlc.arg(updated_at)
|
||||
WHERE id = sqlc.arg(id) AND status = 'suggested';
|
||||
Reference in New Issue
Block a user