From 5357c9787bce49ed69241c0d86e221fdddea6b4a Mon Sep 17 00:00:00 2001 From: hsiegeln <37154749+hsiegeln@users.noreply.github.com> Date: Wed, 22 Apr 2026 14:14:44 +0200 Subject: [PATCH] refactor(views): ON CONFLICT DO UPDATE statt INSERT OR REPLACE Code-Review-Finding zu commit 6c8de6f: INSERT OR REPLACE ist intern DELETE+INSERT, das wuerde eventuelle FK-Children kuenftig stillschweigend mitloeschen. ON CONFLICT DO UPDATE bumpt nur das Timestamp-Feld und matcht den Stil der anderen Repos (shopping/repository.ts:43). Migration-Dateiname zu recipe_view (singular) angeglichen, matcht jetzt den Tabellennamen aus 543008b. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../{014_recipe_views.sql => 014_recipe_view.sql} | 0 src/lib/server/recipes/views.ts | 12 +++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) rename src/lib/server/db/migrations/{014_recipe_views.sql => 014_recipe_view.sql} (100%) diff --git a/src/lib/server/db/migrations/014_recipe_views.sql b/src/lib/server/db/migrations/014_recipe_view.sql similarity index 100% rename from src/lib/server/db/migrations/014_recipe_views.sql rename to src/lib/server/db/migrations/014_recipe_view.sql diff --git a/src/lib/server/recipes/views.ts b/src/lib/server/recipes/views.ts index 66b5c07..02fb3eb 100644 --- a/src/lib/server/recipes/views.ts +++ b/src/lib/server/recipes/views.ts @@ -5,12 +5,14 @@ export function recordView( profileId: number, recipeId: number ): void { - // INSERT OR REPLACE re-fires the DEFAULT (CURRENT_TIMESTAMP) on conflict, - // so subsequent views of the same recipe by the same profile bump the - // timestamp without breaking the composite PK. + // ON CONFLICT DO UPDATE bumps only the timestamp field — avoids the + // DELETE+INSERT that INSERT OR REPLACE performs under the hood, which would + // silently cascade-delete any future FK-referencing children. db.prepare( - `INSERT OR REPLACE INTO recipe_view (profile_id, recipe_id) - VALUES (?, ?)` + `INSERT INTO recipe_view (profile_id, recipe_id) + VALUES (?, ?) + ON CONFLICT(profile_id, recipe_id) DO UPDATE + SET last_viewed_at = CURRENT_TIMESTAMP` ).run(profileId, recipeId); }