-- Universal-CSV-Import setzt source = 'csv'. Alte CHECK-Constraints kennen das nicht.
-- Namen können je nach PG-Version abweichen → alle passenden CHECK Constraints zu source droppen.
DO $$
DECLARE
  r RECORD;
BEGIN
  FOR r IN
    SELECT c.conname
    FROM pg_constraint c
    JOIN pg_class t ON c.conrelid = t.oid
    WHERE t.relname = 'vitals_baseline'
      AND c.contype = 'c'
      AND pg_get_constraintdef(c.oid) ILIKE '%source%'
      AND pg_get_constraintdef(c.oid) ILIKE '%IN %'
  LOOP
    EXECUTE format('ALTER TABLE vitals_baseline DROP CONSTRAINT %I', r.conname);
  END LOOP;
END $$;

ALTER TABLE 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)';
