--
-- UPGRADE_DISTRIBUTED_TRIGGERS_AFTER
--
-- In PG15, Renaming triggers on partitioned tables
-- recurses to renaming the triggers on the partitions as well.
-- Relevant PG commit:
-- 80ba4bb383538a2ee846fece6a7b8da9518b6866
--
-- this test is relevant only for pg14-15 upgrade
--
SHOW server_version \gset
SELECT substring(:'server_version', '\d+')::int = 15 AND EXISTS (SELECT * FROM pg_namespace WHERE nspname = 'upgrade_distributed_triggers') AS is_14_15_pg_upgrade
\gset
\if :is_14_15_pg_upgrade
\else
\q
\endif
SET search_path TO upgrade_distributed_triggers, public;
SET citus.shard_count TO 4;
SET citus.enable_unsafe_triggers TO true;
SELECT run_command_on_workers('ALTER SYSTEM SET citus.enable_unsafe_triggers TO true;');
       run_command_on_workers
---------------------------------------------------------------------
 (localhost,10201,t,"ALTER SYSTEM")
 (localhost,10202,t,"ALTER SYSTEM")
(2 rows)

SELECT run_command_on_workers('SELECT pg_reload_conf();');
 run_command_on_workers
---------------------------------------------------------------------
 (localhost,10201,t,t)
 (localhost,10202,t,t)
(2 rows)

-- after PG15 upgrade, all child triggers have the same name with the parent triggers
-- check that the workers are also updated
SELECT * FROM sale_triggers ORDER BY 1, 2;
           tgname            |     tgrelid     | tgenabled
---------------------------------------------------------------------
 another_renamed_trigger     | sale            | O
 another_renamed_trigger     | sale_newyork    | O
 another_renamed_trigger     | sale_california | O
 not_renamed_trigger         | sale            | O
 not_renamed_trigger         | sale_newyork    | O
 not_renamed_trigger         | sale_california | O
 renamed_record_sale_trigger | sale            | O
 renamed_record_sale_trigger | sale_newyork    | O
 renamed_record_sale_trigger | sale_california | O
 truncate_trigger_xxxxxxx      | sale            | O
 truncate_trigger_xxxxxxx      | sale_newyork    | O
 truncate_trigger_xxxxxxx      | sale_california | O
(12 rows)

SELECT tgrelid::regclass::text, tgname FROM pg_trigger WHERE tgname like 'record_sale_trigger%' ORDER BY 1,2;
 tgrelid | tgname
---------------------------------------------------------------------
(0 rows)

SELECT tgrelid::regclass::text, tgname FROM pg_trigger WHERE tgname like 'renamed_record_sale_trigger%' ORDER BY 1,2;
     tgrelid     |           tgname
---------------------------------------------------------------------
 sale            | renamed_record_sale_trigger
 sale_california | renamed_record_sale_trigger
 sale_newyork    | renamed_record_sale_trigger
(3 rows)

SELECT run_command_on_workers($$SELECT count(*) FROM pg_trigger WHERE tgname like 'record_sale_trigger%';$$);
 run_command_on_workers
---------------------------------------------------------------------
 (localhost,10201,t,0)
 (localhost,10202,t,0)
(2 rows)

SELECT run_command_on_workers($$SELECT count(*) FROM pg_trigger WHERE tgname like 'renamed_record_sale_trigger%';$$);
 run_command_on_workers
---------------------------------------------------------------------
 (localhost,10201,t,9)
 (localhost,10202,t,9)
(2 rows)

-- create another partition to verify that all is safe and sound
CREATE TABLE sale_alabama PARTITION OF sale FOR VALUES IN ('AL');
SELECT * FROM sale_triggers ORDER BY 1, 2;
           tgname            |     tgrelid     | tgenabled
---------------------------------------------------------------------
 another_renamed_trigger     | sale            | O
 another_renamed_trigger     | sale_newyork    | O
 another_renamed_trigger     | sale_california | O
 another_renamed_trigger     | sale_alabama    | O
 not_renamed_trigger         | sale            | O
 not_renamed_trigger         | sale_newyork    | O
 not_renamed_trigger         | sale_california | O
 not_renamed_trigger         | sale_alabama    | O
 renamed_record_sale_trigger | sale            | O
 renamed_record_sale_trigger | sale_newyork    | O
 renamed_record_sale_trigger | sale_california | O
 renamed_record_sale_trigger | sale_alabama    | O
 truncate_trigger_xxxxxxx      | sale            | O
 truncate_trigger_xxxxxxx      | sale_newyork    | O
 truncate_trigger_xxxxxxx      | sale_california | O
 truncate_trigger_xxxxxxx      | sale_alabama    | O
(16 rows)

SELECT tgrelid::regclass::text, tgname FROM pg_trigger WHERE tgname like 'record_sale_trigger%' ORDER BY 1,2;
 tgrelid | tgname
---------------------------------------------------------------------
(0 rows)

SELECT tgrelid::regclass::text, tgname FROM pg_trigger WHERE tgname like 'renamed_record_sale_trigger%' ORDER BY 1,2;
     tgrelid     |           tgname
---------------------------------------------------------------------
 sale            | renamed_record_sale_trigger
 sale_alabama    | renamed_record_sale_trigger
 sale_california | renamed_record_sale_trigger
 sale_newyork    | renamed_record_sale_trigger
(4 rows)

SELECT run_command_on_workers($$SELECT count(*) FROM pg_trigger WHERE tgname like 'record_sale_trigger%';$$);
 run_command_on_workers
---------------------------------------------------------------------
 (localhost,10201,t,0)
 (localhost,10202,t,0)
(2 rows)

SELECT run_command_on_workers($$SELECT count(*) FROM pg_trigger WHERE tgname like 'renamed_record_sale_trigger%';$$);
 run_command_on_workers
---------------------------------------------------------------------
 (localhost,10201,t,12)
 (localhost,10202,t,12)
(2 rows)

-- drop a trigger to verify that all is safe and sound
DROP TRIGGER another_renamed_trigger ON sale;
SELECT * FROM sale_triggers ORDER BY 1, 2;
           tgname            |     tgrelid     | tgenabled
---------------------------------------------------------------------
 not_renamed_trigger         | sale            | O
 not_renamed_trigger         | sale_newyork    | O
 not_renamed_trigger         | sale_california | O
 not_renamed_trigger         | sale_alabama    | O
 renamed_record_sale_trigger | sale            | O
 renamed_record_sale_trigger | sale_newyork    | O
 renamed_record_sale_trigger | sale_california | O
 renamed_record_sale_trigger | sale_alabama    | O
 truncate_trigger_xxxxxxx      | sale            | O
 truncate_trigger_xxxxxxx      | sale_newyork    | O
 truncate_trigger_xxxxxxx      | sale_california | O
 truncate_trigger_xxxxxxx      | sale_alabama    | O
(12 rows)

-- rename a trigger - note that it also renames the triggers on the partitions
ALTER TRIGGER "renamed_record_sale_trigger" ON "sale" RENAME TO "final_renamed_record_sale_trigger";
SELECT * FROM sale_triggers ORDER BY 1, 2;
              tgname               |     tgrelid     | tgenabled
---------------------------------------------------------------------
 final_renamed_record_sale_trigger | sale            | O
 final_renamed_record_sale_trigger | sale_newyork    | O
 final_renamed_record_sale_trigger | sale_california | O
 final_renamed_record_sale_trigger | sale_alabama    | O
 not_renamed_trigger               | sale            | O
 not_renamed_trigger               | sale_newyork    | O
 not_renamed_trigger               | sale_california | O
 not_renamed_trigger               | sale_alabama    | O
 truncate_trigger_xxxxxxx            | sale            | O
 truncate_trigger_xxxxxxx            | sale_newyork    | O
 truncate_trigger_xxxxxxx            | sale_california | O
 truncate_trigger_xxxxxxx            | sale_alabama    | O
(12 rows)

SELECT tgrelid::regclass::text, tgname FROM pg_trigger WHERE tgname like 'renamed_record_sale_trigger%' ORDER BY 1,2;
 tgrelid | tgname
---------------------------------------------------------------------
(0 rows)

SELECT tgrelid::regclass::text, tgname FROM pg_trigger WHERE tgname like 'final_renamed_record_sale_trigger%' ORDER BY 1,2;
     tgrelid     |              tgname
---------------------------------------------------------------------
 sale            | final_renamed_record_sale_trigger
 sale_alabama    | final_renamed_record_sale_trigger
 sale_california | final_renamed_record_sale_trigger
 sale_newyork    | final_renamed_record_sale_trigger
(4 rows)

SELECT run_command_on_workers($$SELECT count(*) FROM pg_trigger WHERE tgname like 'renamed_record_sale_trigger%';$$);
 run_command_on_workers
---------------------------------------------------------------------
 (localhost,10201,t,0)
 (localhost,10202,t,0)
(2 rows)

SELECT run_command_on_workers($$SELECT count(*) FROM pg_trigger WHERE tgname like 'final_renamed_record_sale_trigger%';$$);
 run_command_on_workers
---------------------------------------------------------------------
 (localhost,10201,t,12)
 (localhost,10202,t,12)
(2 rows)

DROP TRIGGER final_renamed_record_sale_trigger ON sale;
-- create another trigger and rename it
CREATE TRIGGER yet_another_trigger
AFTER INSERT OR UPDATE OR DELETE ON sale
FOR EACH ROW EXECUTE FUNCTION upgrade_distributed_triggers.record_sale();
ALTER TRIGGER "yet_another_trigger" ON "sale" RENAME TO "renamed_yet_another_trigger";
SELECT * FROM sale_triggers ORDER BY 1, 2;
           tgname            |     tgrelid     | tgenabled
---------------------------------------------------------------------
 not_renamed_trigger         | sale            | O
 not_renamed_trigger         | sale_newyork    | O
 not_renamed_trigger         | sale_california | O
 not_renamed_trigger         | sale_alabama    | O
 renamed_yet_another_trigger | sale            | O
 renamed_yet_another_trigger | sale_newyork    | O
 renamed_yet_another_trigger | sale_california | O
 renamed_yet_another_trigger | sale_alabama    | O
 truncate_trigger_xxxxxxx      | sale            | O
 truncate_trigger_xxxxxxx      | sale_newyork    | O
 truncate_trigger_xxxxxxx      | sale_california | O
 truncate_trigger_xxxxxxx      | sale_alabama    | O
(12 rows)

-- after upgrade to PG15, test that we can't rename a distributed clone trigger
ALTER TRIGGER "renamed_yet_another_trigger" ON "sale_alabama" RENAME TO "another_trigger_name";
ERROR:  cannot rename trigger "renamed_yet_another_trigger" on table "sale_alabama"
HINT:  Rename the trigger on the partitioned table "sale" instead.
SELECT count(*) FROM pg_trigger WHERE tgname like 'another_trigger_name%';
 count
---------------------------------------------------------------------
     0
(1 row)

SELECT run_command_on_workers($$SELECT count(*) FROM pg_trigger WHERE tgname like 'another_trigger_name%';$$);
 run_command_on_workers
---------------------------------------------------------------------
 (localhost,10201,t,0)
 (localhost,10202,t,0)
(2 rows)

DROP SCHEMA upgrade_distributed_triggers CASCADE;
NOTICE:  drop cascades to 4 other objects
DETAIL:  drop cascades to function record_sale()
drop cascades to table sale
drop cascades to table record_sale
drop cascades to view sale_triggers
