--
-- This file is part of TALER
-- Copyright (C) 2026 Taler Systems SA
--
-- TALER is free software; you can redistribute it and/or modify it under the
-- terms of the GNU General Public License as published by the Free Software
-- Foundation; either version 3, or (at your option) any later version.
--
-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-- A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along with
-- TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>

-- @file merchant-0041.sql
-- @brief Add trigger to notify merchant on account changes
-- @author Christian Grothoff

BEGIN;

SELECT _v.register_patch('merchant-0041', NULL, NULL);

SET search_path TO merchant;

CREATE PROCEDURE merchant.merchant_0041_init(s TEXT)
  LANGUAGE plpgsql
  AS $OUTER$
BEGIN
  EXECUTE format('SET LOCAL search_path TO %I', s);

  CREATE OR REPLACE FUNCTION merchant_account_change_trigger()
    RETURNS TRIGGER LANGUAGE plpgsql AS $$ BEGIN RETURN NULL; END $$;
  CREATE TRIGGER merchant_accounts_on_insert
    AFTER INSERT OR UPDATE OR DELETE
    ON merchant_accounts
    FOR EACH ROW EXECUTE FUNCTION merchant_account_change_trigger();

  SET LOCAL search_path TO merchant;
END
$OUTER$;

INSERT INTO merchant.instance_fixups
  (migration_name
  ,version)
  VALUES
  ('merchant_0041_init'
  ,41);
-- Apply new fix-up to existing instances
CALL merchant.fixup_instance_schema (41::INT8);

COMMIT;
