-- Universal-CSV-Import setzt source = 'csv'. Alte CHECK-Constraints kennen das nicht.
-- Alle passenden CHECKs zu `source` droppen (PG speichert IN oder = ANY (ARRAY[...])).
DO $$
DECLARE
  r RECORD;
BEGIN
  FOR r IN
    SELECT c.conname
    FROM pg_constraint c
    JOIN pg_class t ON c.conrelid = t.oid
    JOIN pg_namespace n ON n.oid = t.relnamespace
    WHERE n.nspname = 'public'
      AND t.relname = 'vitals_baseline'
      AND c.contype = 'c'
      AND (
        c.conname = 'vitals_baseline_source_check'
        OR COALESCE(pg_get_constraintdef(c.oid), '') ILIKE '%source%'
      )
  LOOP
    EXECUTE format('ALTER TABLE public.vitals_baseline DROP CONSTRAINT %I', r.conname);
  END LOOP;
END $$;

ALTER TABLE public.vitals_baseline DROP CONSTRAINT IF EXISTS vitals_baseline_source_check;

ALTER TABLE public.vitals_baseline ADD CONSTRAINT vitals_baseline_source_check
    CHECK (source IN ('manual', 'apple_health', 'garmin', 'withings', 'csv'));

COMMENT ON COLUMN vitals_baseline.source IS 'manual | apple_health | garmin | withings | csv (Universal-Import)';
