--
-- 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-0042.sql
-- @brief drop the stray per-instance copies of tan_challenges and
--        merchant_pending_webhooks created by merchant-0036-init

-- Background: the 0036 refactor moved most tables into per-instance schemas,
-- but tan_challenges and merchant_pending_webhooks are GLOBAL: all C code
-- addresses them as merchant.tan_challenges / merchant.merchant_pending_webhooks,
-- and merchant-0039 still ALTERs the global merchant.tan_challenges.
-- merchant-0036-init nevertheless also creates per-instance copies of both.
-- Those copies are never read or written, and are wrong-shaped for such use
-- (the per-instance tan_challenges lacks instance_name, the per-instance
-- merchant_pending_webhooks lacks merchant_serial).  merchant-0036 has already
-- been released (v1.6.9, v1.6.10), so it is left untouched and the stray
-- tables are dropped here instead; registering the fix-up also removes them
-- from instances created in the future.

BEGIN;

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

SET search_path TO merchant;

CREATE PROCEDURE merchant.merchant_0042_init(s TEXT)
  LANGUAGE plpgsql
  AS $OUTER$
BEGIN
  -- Schema-qualify explicitly: identically named GLOBAL tables exist in
  -- the "merchant" schema and must NOT be affected.
  EXECUTE format('DROP TABLE IF EXISTS %I.tan_challenges', s);
  EXECUTE format('DROP TABLE IF EXISTS %I.merchant_pending_webhooks', s);
END
$OUTER$;

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

COMMIT;
