@@ -563,6 +563,102 @@ BEGIN
563563END
564564$BODY$;
565565
566+ -- Custom after trigger script for very special scenario where transaction entries (and so SNS event publishing) is to be skipped
567+ -- for special DMLs.
568+ CREATE OR REPLACE FUNCTION xyz_config .naksha_space_after_trigger_to_skip_txn()
569+ RETURNS trigger
570+ LANGUAGE ' plpgsql' VOLATILE
571+ AS $BODY$
572+ DECLARE
573+ author text ;
574+ app_id text ;
575+ new_uuid uuid;
576+ txn uuid;
577+ ts_millis int8;
578+ rts_millis int8;
579+ i int8;
580+ xyz jsonb;
581+ newMarkerPresent bool;
582+ oldMarkerPresent bool;
583+ disable_txn bool;
584+
585+ BEGIN
586+ -- ----------------------------------------------------
587+ -- Use of this custom script is to support skipping transaction entries (thereby indirectly disabling SNS event publish),
588+ -- for special DMLs. It is disabled for those DML operations where either of the following criteria is true:
589+ --
590+ -- 1) Postgres config parameter "naksha_config.skip_txn" is found to be "true"
591+ -- 2) If NEW JSON Feature has xyz.tags with value "disable_event_publish", but OLD JSON Feature doesn't.
592+ -- No OLD entry is considered to be absence of tag.
593+ -- Check for OLD entry is to ensure event suppression happens only once for that "special" DML and not for subsequent "genuine" DMLs.
594+ --
595+ -- This is applicable across INSERT/UPDATE/DELETE operation.
596+ -- Transaction entry is skipped, but History entry is still made.
597+ -- If condition (1) is satisfied, then condition (2) is not checked.
598+ -- ----------------------------------------------------
599+
600+ xyz := OLD .jsondata - > ' properties' - > ' @ns:com:here:xyz' ;
601+
602+ disable_txn := FALSE;
603+ -- Disable Txn INSERT if session level flag 'naksha_config.skip_txn' is 'true'
604+ IF coalesce(current_setting(' naksha_config.skip_txn' , true), ' ' ) = ' true' THEN
605+ disable_txn := TRUE;
606+ END IF;
607+ -- (one more check - if it was inconclusive)
608+ -- Disable Txn INSERT if NEW tags has marker 'disable_event_publish' BUT OLD tags has not
609+ IF (NOT disable_txn) THEN
610+ oldMarkerPresent := coalesce(xyz- > ' tags' ? ' disable_event_publish' , false);
611+ newMarkerPresent := coalesce(NEW .jsondata - > ' properties' - > ' @ns:com:here:xyz' - > ' tags' ? ' disable_event_publish' , false);
612+ disable_txn := (NOT oldMarkerPresent AND newMarkerPresent);
613+ END IF;
614+
615+
616+ IF (disable_txn) THEN
617+ PERFORM xyz_config .naksha_tx_custom_insert (TG_TABLE_SCHEMA, TG_TABLE_NAME);
618+ ELSE
619+ PERFORM xyz_config .naksha_tx_insert (TG_TABLE_SCHEMA, TG_TABLE_NAME);
620+ END IF;
621+
622+
623+ IF TG_OP = ' INSERT' OR TG_OP = ' UPDATE' THEN
624+ EXECUTE format(' INSERT INTO %s."%s_hst" (jsondata,geo,i) VALUES(%L,%L,%L)' ,
625+ TG_TABLE_SCHEMA, TG_TABLE_NAME,
626+ NEW .jsondata , NEW .geo , NEW .i );
627+ RETURN NEW;
628+ END IF;
629+
630+ rts_millis := xyz_config .naksha_ts_millis (clock_timestamp());
631+ ts_millis := xyz_config .naksha_ts_millis (current_timestamp );
632+ txn := xyz_config .naksha_tx_current ();
633+ i = nextval(' "' || TG_TABLE_SCHEMA|| ' "."' || TG_TABLE_NAME|| ' _i_seq"' );
634+ new_uuid := xyz_config .naksha_uuid_feature_number (i, current_timestamp );
635+ author := xyz_config .naksha_tx_get_author (OLD .jsondata );
636+ app_id := xyz_config .naksha_tx_get_app_id ();
637+
638+ -- We do these updates, because in the "after-trigger" we only write into history.
639+ IF xyz IS NULL THEN
640+ xyz := ' {}' ::jsonb;
641+ END IF;
642+ xyz := jsonb_set(xyz, ' {"action"}' , (' "DELETE"' )::jsonb, true);
643+ xyz := jsonb_set(xyz, ' {"version"}' , coalesce((' ' || (xyz_config .naksha_json_version (OLD .jsondata )+ 1 ::int8)),' 1' )::jsonb, true);
644+ xyz := jsonb_set(xyz, ' {"author"}' , coalesce(author, ' null' )::jsonb, true);
645+ xyz := jsonb_set(xyz, ' {"appId"}' , coalesce(app_id, ' null' )::jsonb, true);
646+ xyz := jsonb_set(xyz, ' {"puuid"}' , xyz- > ' uuid' , true);
647+ xyz := jsonb_set(xyz, ' {"uuid"}' , (' "' || ((new_uuid)::text )|| ' "' )::jsonb, true);
648+ xyz := jsonb_set(xyz, ' {"txn"}' , (' "' || ((txn)::text )|| ' "' )::jsonb, true);
649+ -- createdAt and rtcts stay what they are
650+ xyz := jsonb_set(xyz, ' {"updatedAt"}' , (ts_millis::text )::jsonb, true);
651+ xyz := jsonb_set(xyz, ' {"rtuts"}' , (rts_millis::text )::jsonb, true);
652+ OLD .jsondata = jsonb_set(OLD .jsondata , ' {"properties","@ns:com:here:xyz"}' , xyz, true);
653+ OLD .i = i;
654+ EXECUTE format(' INSERT INTO %s."%s_hst" (jsondata,geo,i) VALUES(%L,%L,%L)' ,
655+ TG_TABLE_SCHEMA, TG_TABLE_NAME,
656+ OLD .jsondata , OLD .geo , OLD .i );
657+ RETURN OLD;
658+ END
659+ $BODY$
660+ ;
661+
566662-- Ensures that the given schema and table exist as storage location for a space.
567663CREATE OR REPLACE FUNCTION xyz_config .naksha_space_ensure(_schema text , _table text )
568664 RETURNS void
0 commit comments