anastasis

Credential backup and recovery protocol and service
Log | Files | Refs | Submodules | README | LICENSE

stasis-0004.sql (4647B)


      1 --
      2 -- This file is part of Anastasis
      3 -- Copyright (C) 2026 Anastasis SARL
      4 --
      5 -- ANASTASIS 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 -- ANASTASIS 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 -- ANASTASIS; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 --
     16 
     17 -- Everything in one big transaction
     18 BEGIN;
     19 
     20 -- Check patch versioning is in place.
     21 SELECT _v.register_patch('stasis-0004', NULL, NULL);
     22 
     23 SET search_path TO anastasis;
     24 
     25 -- A provider may now price its service in several currencies at once, so an
     26 -- amount is only meaningful together with the currency it is in.  The old
     27 -- taler_amount type has no room for one: it was written on the assumption
     28 -- that the whole provider used exactly one currency, which was then read out
     29 -- of [anastasis] CURRENCY at result-parsing time.  Replace it with the
     30 -- merchant's taler_amount_currency, which carries the currency in the row.
     31 --
     32 -- anastasis_do_insert_recdoc_payment() names taler_amount in its signature
     33 -- and therefore depends on the type.  It has to be dropped before the type
     34 -- can be, and dropping it here costs nothing: every anastasis-dbinit run
     35 -- recreates all stored procedures from procedures.sql.  Doing it explicitly
     36 -- rather than with DROP TYPE ... CASCADE keeps the dependency visible, and
     37 -- makes an unexpected second dependent an error instead of a silent drop.
     38 DROP FUNCTION IF EXISTS anastasis_do_insert_recdoc_payment;
     39 
     40 CREATE TYPE taler_amount_currency
     41   AS
     42   (val INT8
     43   ,frac INT4
     44   ,curr VARCHAR(12)
     45   );
     46 COMMENT ON TYPE taler_amount_currency
     47   IS 'Stores an amount with its currency, fraction is in units of 1/100000000 of the base value';
     48 
     49 -- Existing payment rows were all made in the provider's single configured
     50 -- currency, but that currency is in the configuration file and not reachable
     51 -- from here.  Stamp them KUDOS: these three columns have never been read back
     52 -- by any released version (the getters either did not select the column or
     53 -- selected it without binding a result), so no decision depends on the value
     54 -- being right, and the alternative -- a NULL currency -- would make them
     55 -- unparseable rather than merely historical.
     56 ALTER TABLE anastasis_truth_payment
     57   ALTER COLUMN amount TYPE taler_amount_currency
     58   USING ROW((amount).val, (amount).frac, 'KUDOS')::taler_amount_currency;
     59 COMMENT ON COLUMN anastasis_truth_payment.amount
     60   IS 'Amount we were paid, in the currency the user chose to pay in. Rows migrated from before multi-currency support read KUDOS regardless of what was actually paid.';
     61 
     62 ALTER TABLE anastasis_recdoc_payment
     63   ALTER COLUMN amount TYPE taler_amount_currency
     64   USING ROW((amount).val, (amount).frac, 'KUDOS')::taler_amount_currency;
     65 COMMENT ON COLUMN anastasis_recdoc_payment.amount
     66   IS 'Amount we were paid, in the currency the user chose to pay in. Rows migrated from before multi-currency support read KUDOS regardless of what was actually paid.';
     67 
     68 ALTER TABLE anastasis_challenge_payment
     69   ALTER COLUMN amount TYPE taler_amount_currency
     70   USING ROW((amount).val, (amount).frac, 'KUDOS')::taler_amount_currency;
     71 COMMENT ON COLUMN anastasis_challenge_payment.amount
     72   IS 'Amount we were paid, in the currency the user chose to pay in. This is what a refund of the challenge returns, so it must be the currency actually received. Rows migrated from before multi-currency support read KUDOS regardless of what was actually paid.';
     73 
     74 -- The IBAN column is different: it *is* read back, by
     75 -- ANASTASIS_DB_iterate_auth_iban_transfers(), and the IBAN plugin compares
     76 -- what it finds against the expected transfer amount.  Stamping it KUDOS
     77 -- would make every pre-upgrade transfer fail that comparison and silently
     78 -- invalidate all in-flight IBAN challenges.  Leave the currency NULL here;
     79 -- anastasis-dbinit fills it in from [anastasis] CURRENCY, which is the one
     80 -- place that knows the bank account's currency.
     81 ALTER TABLE anastasis_auth_iban_in
     82   ALTER COLUMN credit TYPE taler_amount_currency
     83   USING ROW((credit).val, (credit).frac, NULL)::taler_amount_currency;
     84 COMMENT ON COLUMN anastasis_auth_iban_in.credit
     85   IS 'Amount we were credited, in the currency of the provider bank account ([anastasis] CURRENCY)';
     86 
     87 DROP TYPE taler_amount;
     88 
     89 COMMIT;