donau

Donation authority for GNU Taler (experimental)
Log | Files | Refs | Submodules | README | LICENSE

donau-0003.sql (2528B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2026 Taler Systems SA
      4 --
      5 -- TALER is free software; you can redistribute it and/or modify it under the
      6 -- terms of the GNU General Public License as published by the Free Software
      7 -- Foundation; either version 3, or (at your option) any later version.
      8 --
      9 -- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10 -- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11 -- A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 --
     13 -- You should have received a copy of the GNU General Public License along with
     14 -- TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 --
     16 
     17 BEGIN;
     18 
     19 SELECT _v.register_patch('donau-0003', NULL, NULL);
     20 SET search_path TO donau;
     21 
     22 -- The `history' table is documented as holding "the yearly donation amount
     23 -- for each charity", but its primary key was `charity_id' alone, so it could
     24 -- hold at most ONE year per charity: the second year was a unique violation.
     25 -- The key must include the year.
     26 ALTER TABLE history
     27   DROP CONSTRAINT history_pkey;
     28 ALTER TABLE history
     29   ADD CONSTRAINT history_pkey PRIMARY KEY (charity_id, donation_year);
     30 
     31 COMMENT ON COLUMN history.donation_year
     32   IS 'Year the final_amount was accumulated in; part of the primary key.';
     33 
     34 -- receipts_issued.receipt_id was GENERATED BY DEFAULT AS IDENTITY, which
     35 -- permits explicit inserts (bulk load, partial restore, ad-hoc migration)
     36 -- and thus permits leaving the identity sequence behind the largest stored
     37 -- value.  Every subsequent insert then fails with a unique violation on
     38 -- receipts_issued_receipt_id_key -- which donau-httpd cannot distinguish
     39 -- from "this receipt_hash already exists".  The value is never supplied by
     40 -- the daemon, so make it unforgeable from the outside.
     41 ALTER TABLE receipts_issued
     42   ALTER COLUMN receipt_id SET GENERATED ALWAYS;
     43 
     44 -- receipts_issued.receipt_hash is the idempotence key of POST /batch-issue:
     45 -- it is what stops a charity from having the same blinded UDIs signed twice.
     46 -- With ON DELETE CASCADE that guarantee lasted only as long as the charity
     47 -- row: deleting and re-registering a charity freed every one of its receipt
     48 -- hashes for replay, and destroyed the record of what had been issued.
     49 ALTER TABLE receipts_issued
     50   DROP CONSTRAINT receipts_issued_charity_id_fkey;
     51 ALTER TABLE receipts_issued
     52   ADD CONSTRAINT receipts_issued_charity_id_fkey
     53   FOREIGN KEY (charity_id)
     54   REFERENCES charities (charity_id)
     55   ON DELETE RESTRICT;
     56 
     57 COMMIT;