mitai-jinkendo/backend/migrations/048_vitals_baseline_source_csv.sql
Lars 1855f6e57a
All checks were successful
Deploy Development / deploy (push) Successful in 57s
Build Test / pytest-backend (push) Successful in 4s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 18s
refactor(migrations): Improve idempotency and constraint handling for vitals_baseline source
- Updated migration scripts to ensure idempotent behavior for the source CHECK constraint, allowing for consistent application even if previous migrations were partially successful.
- Enhanced SQL logic to drop existing constraints safely and re-add them, ensuring compatibility with the universal CSV import.
- Clarified comments for better understanding of migration context and functionality.
2026-04-10 16:17:35 +02:00

30 lines
1.0 KiB
SQL

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