RezinaLMS-BackEnd/db/migrations/000106_lms_drop_one_practice_per_parent.up.sql

47 lines
1.6 KiB
SQL

-- 000105 dropped this unique by a single constraint name. Re-drop by definition so
-- Learn English can store multiple practices per course, module, or lesson even if
-- the unique was created under a different name or as an index.
DO $$
DECLARE
r record;
BEGIN
FOR r IN
SELECT c.conname
FROM pg_constraint c
JOIN pg_class t ON t.oid = c.conrelid
JOIN pg_namespace n ON n.oid = t.relnamespace
WHERE n.nspname = 'public'
AND t.relname = 'lms_practice_parents'
AND c.contype = 'u'
AND pg_get_constraintdef(c.oid) ~* 'UNIQUE\s*\(\s*parent_kind\s*,\s*parent_id\s*\)'
LOOP
EXECUTE format('ALTER TABLE public.lms_practice_parents DROP CONSTRAINT IF EXISTS %I', r.conname);
END LOOP;
END $$;
DO $$
DECLARE
r record;
BEGIN
FOR r IN
SELECT i.relname AS idxname
FROM pg_index x
JOIN pg_class i ON i.oid = x.indexrelid
JOIN pg_class t ON t.oid = x.indrelid
JOIN pg_namespace n ON n.oid = t.relnamespace
WHERE n.nspname = 'public'
AND t.relname = 'lms_practice_parents'
AND x.indisunique
AND NOT x.indisprimary
AND pg_get_indexdef(i.oid) LIKE '%(parent_kind, parent_id)%'
AND pg_get_indexdef(i.oid) NOT LIKE '%(practice_id, parent_kind, parent_id)%'
LOOP
EXECUTE format('DROP INDEX IF EXISTS public.%I', r.idxname);
END LOOP;
END $$;
DROP INDEX IF EXISTS lms_practice_parents_parent_kind_parent_id_key;
DROP INDEX IF EXISTS idx_lms_practices_one_per_course;
DROP INDEX IF EXISTS idx_lms_practices_one_per_module;
DROP INDEX IF EXISTS idx_lms_practices_one_per_lesson;