-- Persist a permanent backup of aisle values BEFORE running migration 0015.
-- Run via Docker (no host psql required):
--
--   docker compose exec db psql -U mealplanner -d mealplanner \
--     -f /dev/stdin < backend/scripts/persist_aisle_backup.sql
--
-- Creates two permanent backup tables in the public schema. They are NOT
-- auto-dropped; you can DROP them manually after you confirm the migration
-- is correct:
--
--   DROP TABLE public.ingredient_aisle_backup_0015;
--   DROP TABLE public.grocery_item_aisle_backup_0015;

CREATE TABLE IF NOT EXISTS public.ingredient_aisle_backup_0015 AS
  SELECT id, aisle, created_at FROM public.ingredient WHERE aisle IS NOT NULL;

CREATE TABLE IF NOT EXISTS public.grocery_item_aisle_backup_0015 AS
  SELECT id, aisle FROM public.grocery_item WHERE aisle IS NOT NULL;

SELECT 'ingredient backup'   AS table_name, COUNT(*) AS rows_backed_up
  FROM public.ingredient_aisle_backup_0015
UNION ALL
SELECT 'grocery_item backup' AS table_name, COUNT(*) AS rows_backed_up
  FROM public.grocery_item_aisle_backup_0015;
