exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

0002-purse_deletion.sql (2893B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2014--2022 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 CREATE OR REPLACE FUNCTION create_table_purse_deletion(
     18   IN partition_suffix TEXT DEFAULT NULL
     19 )
     20 RETURNS VOID
     21 LANGUAGE plpgsql
     22 AS $$
     23 DECLARE
     24   table_name TEXT DEFAULT 'purse_deletion';
     25 BEGIN
     26   PERFORM create_partitioned_table(
     27     'CREATE TABLE IF NOT EXISTS %I'
     28       '(purse_deletion_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
     29       ',purse_sig BYTEA CHECK (LENGTH(purse_sig)=64)'
     30       ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
     31     ') %s ;'
     32     ,table_name
     33     ,'PARTITION BY HASH (purse_pub)'
     34     ,partition_suffix
     35   );
     36   PERFORM comment_partitioned_table(
     37      'signatures affirming explicit purse deletions'
     38     ,table_name
     39     ,partition_suffix
     40   );
     41   PERFORM comment_partitioned_column(
     42      'signature of type WALLET_PURSE_DELETE'
     43     ,'purse_sig'
     44     ,table_name
     45     ,partition_suffix
     46   );
     47 END $$;
     48 
     49 COMMENT ON FUNCTION create_table_purse_deletion
     50   IS 'Creates the purse_deletion table';
     51 
     52 
     53 CREATE OR REPLACE FUNCTION constrain_table_purse_deletion(
     54   IN partition_suffix TEXT DEFAULT NULL
     55 )
     56 RETURNS void
     57 LANGUAGE plpgsql
     58 AS $$
     59 DECLARE
     60   table_name TEXT DEFAULT 'purse_deletion';
     61 BEGIN
     62   table_name = concat_ws('_', table_name, partition_suffix);
     63   EXECUTE FORMAT (
     64     'ALTER TABLE ' || table_name ||
     65       ' ADD CONSTRAINT ' || table_name || '_delete_serial_key '
     66         'UNIQUE (purse_deletion_serial_id)'
     67   );
     68 END $$;
     69 
     70 
     71 CREATE OR REPLACE FUNCTION master_table_purse_requests_was_deleted (
     72 )
     73 RETURNS void
     74 LANGUAGE plpgsql
     75 AS $$
     76 DECLARE
     77   table_name TEXT DEFAULT 'purse_requests';
     78 BEGIN
     79   EXECUTE FORMAT (
     80     'ALTER TABLE exchange.' || table_name ||
     81     ' ADD COLUMN'
     82     ' was_deleted BOOLEAN NOT NULL DEFAULT(FALSE)'
     83   );
     84   COMMENT ON COLUMN purse_requests.was_deleted
     85     IS 'TRUE if the purse was explicitly deleted (purse must have an entry in the purse_deletion table)';
     86 END $$;
     87 
     88 
     89 INSERT INTO exchange_tables
     90     (name
     91     ,version
     92     ,action
     93     ,partitioned
     94     ,by_range)
     95   VALUES
     96     ('purse_deletion'
     97     ,'exchange-0002'
     98     ,'create'
     99     ,TRUE
    100     ,FALSE),
    101     ('purse_deletion'
    102     ,'exchange-0002'
    103     ,'constrain'
    104     ,TRUE
    105     ,FALSE),
    106     ('purse_requests_was_deleted'
    107     ,'exchange-0002'
    108     ,'master'
    109     ,TRUE
    110     ,FALSE);