--
-- 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/>
--

-- handle_purse_expired() re-reports every expired purse on every auditor
-- round and the INSERT had no dedup, so the table grew by one row per
-- purse per round for the ten years until GC removes the purse.  Keep
-- one report per purse.
DELETE FROM auditor_purse_not_closed_inconsistencies pnc
 WHERE pnc.row_id <
       (SELECT MAX(pnc2.row_id)
          FROM auditor_purse_not_closed_inconsistencies pnc2
         WHERE pnc2.purse_pub=pnc.purse_pub);

ALTER TABLE auditor_purse_not_closed_inconsistencies
  ADD CONSTRAINT auditor_purse_not_closed_inconsistencies_purse_pub_key
  UNIQUE (purse_pub);
