- 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.
31 lines
977 B
SQL
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 $$;
|