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

-- delete_pending_deposit() deletes by batch_deposit_serial_id and its
-- caller (clear_finished_transfer_cb) asserts that at most one row was
-- removed, but insert_pending_deposit() had no dedup and the column had
-- no unique index.  Drop the duplicates (keeping the most recent report
-- for each deposit) and constrain the column.
DELETE FROM auditor_pending_deposits ap
 WHERE ap.row_id <
       (SELECT MAX(ap2.row_id)
          FROM auditor_pending_deposits ap2
         WHERE ap2.batch_deposit_serial_id=ap.batch_deposit_serial_id);

ALTER TABLE auditor_pending_deposits
  ADD CONSTRAINT auditor_pending_deposits_batch_deposit_serial_id_key
  UNIQUE (batch_deposit_serial_id);
