57 lines
2.4 KiB
PL/PgSQL
57 lines
2.4 KiB
PL/PgSQL
-- Script to remove the "Neopredeljeno" (Undefined) catch-all row from domain_primary.
|
|
-- Run manually against each environment's Postgres (dev/staging/prod) - the
|
|
-- postgres/init/*.sql seed scripts only run once, on first container init, so this
|
|
-- change does not otherwise propagate to already-initialized databases.
|
|
--
|
|
-- Safe to re-run: if the row is already gone, it's a no-op. If any dictionary or
|
|
-- consultancy_entry still references it, the delete is aborted (nothing is changed)
|
|
-- and the offending rows are listed below for manual reassignment.
|
|
|
|
-- Informational: list anything currently pointing at "Neopredeljeno", if it still exists.
|
|
SELECT 'dictionary' AS table_name, d.id, d.name_sl AS label, d.status::text AS status
|
|
FROM dictionary d
|
|
JOIN domain_primary dp ON dp.id = d.domain_primary_id
|
|
WHERE dp.name_sl = 'Neopredeljeno'
|
|
UNION ALL
|
|
SELECT 'consultancy_entry.domain_primary_id', ce.id, ce.title, ce.status::text
|
|
FROM consultancy_entry ce
|
|
JOIN domain_primary dp ON dp.id = ce.domain_primary_id
|
|
WHERE dp.name_sl = 'Neopredeljeno'
|
|
UNION ALL
|
|
SELECT 'consultancy_entry.domain_primary_id_initial', ce.id, ce.title, ce.status::text
|
|
FROM consultancy_entry ce
|
|
JOIN domain_primary dp ON dp.id = ce.domain_primary_id_initial
|
|
WHERE dp.name_sl = 'Neopredeljeno';
|
|
|
|
BEGIN;
|
|
|
|
DO $$
|
|
DECLARE
|
|
target_id smallint;
|
|
dict_count integer;
|
|
cons_count integer;
|
|
cons_initial_count integer;
|
|
BEGIN
|
|
SELECT id INTO target_id FROM domain_primary WHERE name_sl = 'Neopredeljeno';
|
|
|
|
IF target_id IS NULL THEN
|
|
RAISE NOTICE 'Row "Neopredeljeno" not found in domain_primary - nothing to do.';
|
|
RETURN;
|
|
END IF;
|
|
|
|
SELECT count(*) INTO dict_count FROM dictionary WHERE domain_primary_id = target_id;
|
|
SELECT count(*) INTO cons_count FROM consultancy_entry WHERE domain_primary_id = target_id;
|
|
SELECT count(*) INTO cons_initial_count FROM consultancy_entry WHERE domain_primary_id_initial = target_id;
|
|
|
|
IF dict_count > 0 OR cons_count > 0 OR cons_initial_count > 0 THEN
|
|
RAISE EXCEPTION
|
|
'Cannot remove "Neopredeljeno" (id=%): still referenced by % dictionary row(s), % consultancy_entry.domain_primary_id row(s), % consultancy_entry.domain_primary_id_initial row(s). Reassign these manually (see the SELECT above), then re-run this script.',
|
|
target_id, dict_count, cons_count, cons_initial_count;
|
|
END IF;
|
|
|
|
DELETE FROM domain_primary WHERE id = target_id;
|
|
RAISE NOTICE 'Removed domain_primary id=% ("Neopredeljeno").', target_id;
|
|
END $$;
|
|
|
|
COMMIT;
|