mitai-jinkendo/backend/migrations/049_vitals_baseline_source_csv_idempotent.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

31 lines
977 B
SQL

-- Idempotent: gleicher Zielzustand wie 048, auch wenn die CHECK schon existiert (z. B. 048 DB-seitig
-- erfolgreich, aber nicht in schema_migrations). Ein einziges DO vermeidet Mehrfach-Statement-Probleme.
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;
EXECUTE $ddl$
ALTER TABLE public.vitals_baseline ADD CONSTRAINT vitals_baseline_source_check
CHECK (source IN ('manual', 'apple_health', 'garmin', 'withings', 'csv'))
$ddl$;
EXCEPTION
WHEN duplicate_object THEN
NULL;
END $$;