Skip to content

Commit

Permalink
Fix the ENABLE TRIGGER step for application triggers in the rollback …
Browse files Browse the repository at this point in the history
…processus. Replica or Always application triggers were re-enabled as Origin triggers. This bug was introduced in commit 3ecfbb6.
  • Loading branch information
beaud76 committed Feb 27, 2021
1 parent aa4134b commit c97dfab
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 32 deletions.
10 changes: 5 additions & 5 deletions sql/emaj--3.4.0--devel.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@ $_handle_trigger_fk_tbl$
-- Inputs: the action to perform: ENABLE_TRIGGER/DISABLE_TRIGGER/ADD_TRIGGER/DROP_TRIGGER/ADD_FK/DROP_FK
-- the full name of the application table (schema qualified and quoted if needed)
-- the trigger or constraint name
-- the object definition for foreign keys
-- the object definition for foreign keys, or the trigger type (ALWAYS/REPLICA/'') for triggers
-- The function is defined as SECURITY DEFINER so that emaj_adm role can perform the action on any application table.
DECLARE
v_stack TEXT;
Expand All @@ -1511,8 +1511,8 @@ $_handle_trigger_fk_tbl$
EXECUTE format('ALTER TABLE %s DISABLE TRIGGER %I',
p_fullTableName, p_objectName);
ELSIF p_action = 'ENABLE_TRIGGER' THEN
EXECUTE format('ALTER TABLE %s ENABLE TRIGGER %I',
p_fullTableName, p_objectName);
EXECUTE format('ALTER TABLE %s ENABLE %s TRIGGER %I',
p_fullTableName, p_objectDef, p_objectName);
ELSIF p_action = 'ADD_TRIGGER' AND p_objectName = 'emaj_log_trg' THEN
EXECUTE format('CREATE TRIGGER emaj_log_trg'
' AFTER INSERT OR UPDATE OR DELETE ON %s'
Expand Down Expand Up @@ -8724,10 +8724,10 @@ $_rlbk_session_exec$
PERFORM emaj._handle_trigger_fk_tbl('ADD_FK', v_fullTableName, r_step.rlbp_object, r_step.rlbp_object_def);
WHEN 'ENA_APP_TRG' THEN
-- process an application trigger enable
PERFORM emaj._handle_trigger_fk_tbl('ENABLE_TRIGGER', v_fullTableName, r_step.rlbp_object);
PERFORM emaj._handle_trigger_fk_tbl('ENABLE_TRIGGER', v_fullTableName, r_step.rlbp_object, r_step.rlbp_object_def);
WHEN 'ENA_LOG_TRG' THEN
-- process a log trigger enable
PERFORM emaj._handle_trigger_fk_tbl('ENABLE_TRIGGER', v_fullTableName, 'emaj_log_trg');
PERFORM emaj._handle_trigger_fk_tbl('ENABLE_TRIGGER', v_fullTableName, 'emaj_log_trg', '');
END CASE;
-- update the emaj_rlbk_plan table to set the step duration
-- NB: the computed duration does not include the time needed to update the emaj_rlbk_plan table
Expand Down
10 changes: 5 additions & 5 deletions sql/emaj--devel.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1760,7 +1760,7 @@ $_handle_trigger_fk_tbl$
-- Inputs: the action to perform: ENABLE_TRIGGER/DISABLE_TRIGGER/ADD_TRIGGER/DROP_TRIGGER/ADD_FK/DROP_FK
-- the full name of the application table (schema qualified and quoted if needed)
-- the trigger or constraint name
-- the object definition for foreign keys
-- the object definition for foreign keys, or the trigger type (ALWAYS/REPLICA/'') for triggers
-- The function is defined as SECURITY DEFINER so that emaj_adm role can perform the action on any application table.
DECLARE
v_stack TEXT;
Expand All @@ -1780,8 +1780,8 @@ $_handle_trigger_fk_tbl$
EXECUTE format('ALTER TABLE %s DISABLE TRIGGER %I',
p_fullTableName, p_objectName);
ELSIF p_action = 'ENABLE_TRIGGER' THEN
EXECUTE format('ALTER TABLE %s ENABLE TRIGGER %I',
p_fullTableName, p_objectName);
EXECUTE format('ALTER TABLE %s ENABLE %s TRIGGER %I',
p_fullTableName, p_objectDef, p_objectName);
ELSIF p_action = 'ADD_TRIGGER' AND p_objectName = 'emaj_log_trg' THEN
EXECUTE format('CREATE TRIGGER emaj_log_trg'
' AFTER INSERT OR UPDATE OR DELETE ON %s'
Expand Down Expand Up @@ -9005,10 +9005,10 @@ $_rlbk_session_exec$
PERFORM emaj._handle_trigger_fk_tbl('ADD_FK', v_fullTableName, r_step.rlbp_object, r_step.rlbp_object_def);
WHEN 'ENA_APP_TRG' THEN
-- process an application trigger enable
PERFORM emaj._handle_trigger_fk_tbl('ENABLE_TRIGGER', v_fullTableName, r_step.rlbp_object);
PERFORM emaj._handle_trigger_fk_tbl('ENABLE_TRIGGER', v_fullTableName, r_step.rlbp_object, r_step.rlbp_object_def);
WHEN 'ENA_LOG_TRG' THEN
-- process a log trigger enable
PERFORM emaj._handle_trigger_fk_tbl('ENABLE_TRIGGER', v_fullTableName, 'emaj_log_trg');
PERFORM emaj._handle_trigger_fk_tbl('ENABLE_TRIGGER', v_fullTableName, 'emaj_log_trg', '');
END CASE;
-- update the emaj_rlbk_plan table to set the step duration
-- NB: the computed duration does not include the time needed to update the emaj_rlbk_plan table
Expand Down
10 changes: 5 additions & 5 deletions sql/emaj-devel.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1767,7 +1767,7 @@ $_handle_trigger_fk_tbl$
-- Inputs: the action to perform: ENABLE_TRIGGER/DISABLE_TRIGGER/ADD_TRIGGER/DROP_TRIGGER/ADD_FK/DROP_FK
-- the full name of the application table (schema qualified and quoted if needed)
-- the trigger or constraint name
-- the object definition for foreign keys
-- the object definition for foreign keys, or the trigger type (ALWAYS/REPLICA/'') for triggers
-- The function is defined as SECURITY DEFINER so that emaj_adm role can perform the action on any application table.
DECLARE
v_stack TEXT;
Expand All @@ -1787,8 +1787,8 @@ $_handle_trigger_fk_tbl$
EXECUTE format('ALTER TABLE %s DISABLE TRIGGER %I',
p_fullTableName, p_objectName);
ELSIF p_action = 'ENABLE_TRIGGER' THEN
EXECUTE format('ALTER TABLE %s ENABLE TRIGGER %I',
p_fullTableName, p_objectName);
EXECUTE format('ALTER TABLE %s ENABLE %s TRIGGER %I',
p_fullTableName, p_objectDef, p_objectName);
ELSIF p_action = 'ADD_TRIGGER' AND p_objectName = 'emaj_log_trg' THEN
EXECUTE format('CREATE TRIGGER emaj_log_trg'
' AFTER INSERT OR UPDATE OR DELETE ON %s'
Expand Down Expand Up @@ -9012,10 +9012,10 @@ $_rlbk_session_exec$
PERFORM emaj._handle_trigger_fk_tbl('ADD_FK', v_fullTableName, r_step.rlbp_object, r_step.rlbp_object_def);
WHEN 'ENA_APP_TRG' THEN
-- process an application trigger enable
PERFORM emaj._handle_trigger_fk_tbl('ENABLE_TRIGGER', v_fullTableName, r_step.rlbp_object);
PERFORM emaj._handle_trigger_fk_tbl('ENABLE_TRIGGER', v_fullTableName, r_step.rlbp_object, r_step.rlbp_object_def);
WHEN 'ENA_LOG_TRG' THEN
-- process a log trigger enable
PERFORM emaj._handle_trigger_fk_tbl('ENABLE_TRIGGER', v_fullTableName, 'emaj_log_trg');
PERFORM emaj._handle_trigger_fk_tbl('ENABLE_TRIGGER', v_fullTableName, 'emaj_log_trg', '');
END CASE;
-- update the emaj_rlbk_plan table to set the step duration
-- NB: the computed duration does not include the time needed to update the emaj_rlbk_plan table
Expand Down
28 changes: 26 additions & 2 deletions test/10/expected/rollback.out
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ select emaj.emaj_modify_table('myschema1','mytbl2','{"ignored_triggers":"mytbl2t
1
(1 row)

-- switch the myTbl2trg1 as an ALWAYS trigger
ALTER TABLE mySchema1.myTbl2 DISABLE TRIGGER myTbl2trg1, ENABLE ALWAYS TRIGGER myTbl2trg1;
-- disable event triggers to test cases with missing components
select emaj.emaj_disable_protection_by_event_triggers();
emaj_disable_protection_by_event_triggers
Expand Down Expand Up @@ -1690,6 +1692,16 @@ select col20, col21 from myschema1.myTbl2b order by col20;
-------+-------
(0 rows)

-- check application triggers state
select nspname, relname, tgname, tgenabled from pg_trigger, pg_class, pg_namespace
where relnamespace = pg_namespace.oid and tgrelid = pg_class.oid and tgname like 'mytbl2trg%'
order by 1,2,3;
nspname | relname | tgname | tgenabled
-----------+---------+------------+-----------
myschema1 | mytbl2 | mytbl2trg1 | A
myschema1 | mytbl2 | mytbl2trg2 | O
(2 rows)

-----------------------------
-- unlogged rollback of logged rollbacks #3
-----------------------------
Expand Down Expand Up @@ -2471,6 +2483,18 @@ select emaj.emaj_cleanup_rollback_state();
1
(1 row)

-----------------------------
-- check application triggers state
-----------------------------
select nspname, relname, tgname, tgenabled from pg_trigger, pg_class, pg_namespace
where relnamespace = pg_namespace.oid and tgrelid = pg_class.oid and tgname like 'mytbl2trg%'
order by 1,2,3;
nspname | relname | tgname | tgenabled
-----------+---------+------------+-----------
myschema1 | mytbl2 | mytbl2trg1 | O
myschema1 | mytbl2 | mytbl2trg2 | O
(2 rows)

-----------------------------
-- check rollback tables
-----------------------------
Expand Down Expand Up @@ -2561,7 +2585,7 @@ select rlbp_rlbk_id, rlbp_step, rlbp_schema, rlbp_table, rlbp_object, rlbp_targe
4010 | DELETE_LOG | myschema1 | mytbl2b | | 4028 | 2 | 1 | | 6 | 3 | 6
4010 | SET_FK_IMM | myschema1 | mytbl4 | mytbl4_col43_fkey | | 1 | 1 | | 6 | 2 |
4010 | ADD_FK | myschema1 | mytbl4 | mytbl4_col44_fkey | | 1 | 1 | FOREIGN KEY (col44, col45) REFERENCES myschema1.mytbl1(col11, col12) ON UPDATE SET NULL ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED | 0 | 3 |
4010 | ENA_APP_TRG | myschema1 | mytbl2 | mytbl2trg1 | | 1 | 1 | | | 3 |
4010 | ENA_APP_TRG | myschema1 | mytbl2 | mytbl2trg1 | | 1 | 1 | ALWAYS | | 3 |
4010 | ENA_LOG_TRG | myschema1 | mytbl1 | | | 1 | 1 | | | 2 |
4010 | ENA_LOG_TRG | myschema1 | mytbl2 | | | 1 | 1 | | | 2 |
4010 | ENA_LOG_TRG | myschema1 | mytbl2b | | | 2 | 1 | | | 2 |
Expand All @@ -2583,7 +2607,7 @@ select rlbp_rlbk_id, rlbp_step, rlbp_schema, rlbp_table, rlbp_object, rlbp_targe
4012 | RLBK_TABLE | myschema1 | mytbl4 | | 4035 | 2 | 1 | | 2 | 1 | 1
4012 | SET_FK_IMM | myschema1 | mytbl4 | mytbl4_col43_fkey | | 2 | 1 | | 4 | 2 |
4012 | ADD_FK | myschema1 | mytbl4 | mytbl4_col44_fkey | | 2 | 1 | FOREIGN KEY (col44, col45) REFERENCES myschema1.mytbl1(col11, col12) ON UPDATE SET NULL ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED | 0 | 3 |
4012 | ENA_APP_TRG | myschema1 | mytbl2 | mytbl2trg1 | | 2 | 1 | | | 2 |
4012 | ENA_APP_TRG | myschema1 | mytbl2 | mytbl2trg1 | | 2 | 1 | ALWAYS | | 2 |
4012 | CTRL+DBLINK | | | | | | | | 11 | 2 |
4013 | DIS_LOG_TRG | myschema1 | myTbl3 | | | 1 | 1 | | | 2 |
4013 | DIS_LOG_TRG | myschema1 | mytbl1 | | | 2 | 1 | | | 2 |
Expand Down
10 changes: 5 additions & 5 deletions test/11/expected/install_psql.out
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ $_handle_trigger_fk_tbl$
-- Inputs: the action to perform: ENABLE_TRIGGER/DISABLE_TRIGGER/ADD_TRIGGER/DROP_TRIGGER/ADD_FK/DROP_FK
-- the full name of the application table (schema qualified and quoted if needed)
-- the trigger or constraint name
-- the object definition for foreign keys
-- the object definition for foreign keys, or the trigger type (ALWAYS/REPLICA/'') for triggers
-- The function is defined as SECURITY DEFINER so that emaj_adm role can perform the action on any application table.
DECLARE
v_stack TEXT;
Expand All @@ -1734,8 +1734,8 @@ $_handle_trigger_fk_tbl$
EXECUTE format('ALTER TABLE %s DISABLE TRIGGER %I',
p_fullTableName, p_objectName);
ELSIF p_action = 'ENABLE_TRIGGER' THEN
EXECUTE format('ALTER TABLE %s ENABLE TRIGGER %I',
p_fullTableName, p_objectName);
EXECUTE format('ALTER TABLE %s ENABLE %s TRIGGER %I',
p_fullTableName, p_objectDef, p_objectName);
ELSIF p_action = 'ADD_TRIGGER' AND p_objectName = 'emaj_log_trg' THEN
EXECUTE format('CREATE TRIGGER emaj_log_trg'
' AFTER INSERT OR UPDATE OR DELETE ON %s'
Expand Down Expand Up @@ -8847,10 +8847,10 @@ $_rlbk_session_exec$
PERFORM emaj._handle_trigger_fk_tbl('ADD_FK', v_fullTableName, r_step.rlbp_object, r_step.rlbp_object_def);
WHEN 'ENA_APP_TRG' THEN
-- process an application trigger enable
PERFORM emaj._handle_trigger_fk_tbl('ENABLE_TRIGGER', v_fullTableName, r_step.rlbp_object);
PERFORM emaj._handle_trigger_fk_tbl('ENABLE_TRIGGER', v_fullTableName, r_step.rlbp_object, r_step.rlbp_object_def);
WHEN 'ENA_LOG_TRG' THEN
-- process a log trigger enable
PERFORM emaj._handle_trigger_fk_tbl('ENABLE_TRIGGER', v_fullTableName, 'emaj_log_trg');
PERFORM emaj._handle_trigger_fk_tbl('ENABLE_TRIGGER', v_fullTableName, 'emaj_log_trg', '');
END CASE;
-- update the emaj_rlbk_plan table to set the step duration
-- NB: the computed duration does not include the time needed to update the emaj_rlbk_plan table
Expand Down

0 comments on commit c97dfab

Please sign in to comment.