diff --git a/supabase/migrations/20260320000100_scan_history_country.sql b/supabase/migrations/20260320000100_scan_history_country.sql new file mode 100644 index 00000000..0899d5a9 --- /dev/null +++ b/supabase/migrations/20260320000100_scan_history_country.sql @@ -0,0 +1,18 @@ +-- Migration: Add scan_country to scan_history +-- Purpose: Captures user's catalog region at scan time (from user_preferences.country). +-- Enables country-scoped scan analytics and downstream country-aware features. +-- Nullable: Existing rows have no country context; old API callers still work. +-- Rollback: ALTER TABLE public.scan_history DROP COLUMN IF EXISTS scan_country; +-- Issue: #921 | Epic: #920 + +ALTER TABLE public.scan_history + ADD COLUMN IF NOT EXISTS scan_country text + REFERENCES public.country_ref(country_code); + +-- Partial index for country-scoped analytics queries +CREATE INDEX IF NOT EXISTS idx_sh_country + ON public.scan_history (scan_country) + WHERE scan_country IS NOT NULL; + +COMMENT ON COLUMN public.scan_history.scan_country IS + 'User catalog region at scan time (from user_preferences.country). NULL for legacy rows.'; diff --git a/supabase/tests/schema_contracts.test.sql b/supabase/tests/schema_contracts.test.sql index d5f16fde..8d5411cf 100644 --- a/supabase/tests/schema_contracts.test.sql +++ b/supabase/tests/schema_contracts.test.sql @@ -7,7 +7,7 @@ -- ───────────────────────────────────────────────────────────────────────────── BEGIN; -SELECT plan(288); +SELECT plan(297); -- ═══════════════════════════════════════════════════════════════════════════ -- 1. Core data tables exist @@ -421,5 +421,12 @@ SELECT has_function('public', 'api_unwatch_product', 'fu SELECT has_function('public', 'api_get_watchlist', 'function api_get_watchlist exists'); SELECT has_trigger('products', 'trg_record_score_change', 'trigger trg_record_score_change exists on products'); +-- ─── scan_history.scan_country (#921, epic #920) ───────────────────────────── +SELECT has_column('public', 'scan_history', 'scan_country', 'scan_history has scan_country column'); +SELECT col_is_null('public', 'scan_history', 'scan_country', 'scan_history.scan_country is nullable'); +SELECT fk_ok('public', 'scan_history', 'scan_country', + 'public', 'country_ref', 'country_code', + 'scan_history.scan_country references country_ref(country_code)'); + SELECT * FROM finish(); ROLLBACK;