exchange

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

0002-purse_decision.sql (2723B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2014--2023 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 
     18 CREATE FUNCTION create_table_purse_decision(
     19   IN partition_suffix TEXT DEFAULT NULL
     20 )
     21 RETURNS VOID
     22 LANGUAGE plpgsql
     23 AS $$
     24 DECLARE
     25   table_name TEXT DEFAULT 'purse_decision';
     26 BEGIN
     27   PERFORM create_partitioned_table(
     28     'CREATE TABLE %I '
     29       '(purse_decision_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
     30       ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
     31       ',action_timestamp INT8 NOT NULL'
     32       ',refunded BOOL NOT NULL'
     33       ',PRIMARY KEY (purse_pub)'
     34     ') %s ;'
     35     ,table_name
     36     ,'PARTITION BY HASH (purse_pub)'
     37     ,partition_suffix
     38   );
     39   PERFORM comment_partitioned_table(
     40      'Purses that were decided upon (refund or merge)'
     41     ,table_name
     42     ,partition_suffix
     43   );
     44   PERFORM comment_partitioned_column(
     45      'Public key of the purse'
     46     ,'purse_pub'
     47     ,table_name
     48     ,partition_suffix
     49   );
     50 END
     51 $$;
     52 
     53 CREATE FUNCTION constrain_table_purse_decision(
     54   IN partition_suffix TEXT
     55 )
     56 RETURNS VOID
     57 LANGUAGE plpgsql
     58 AS $$
     59 DECLARE
     60   table_name TEXT DEFAULT 'purse_decision';
     61 BEGIN
     62   table_name = concat_ws('_', table_name, partition_suffix);
     63   EXECUTE FORMAT (
     64     'ALTER TABLE ' || table_name ||
     65     ' ADD CONSTRAINT ' || table_name || '_purse_action_serial_id_key'
     66     ' UNIQUE (purse_decision_serial_id) '
     67   );
     68   EXECUTE FORMAT (
     69     'ALTER TABLE ' || table_name ||
     70     ' ADD CONSTRAINT ' || table_name || '_purse_decision_purse_pub'
     71     ' UNIQUE (purse_pub) '
     72   );
     73 END
     74 $$;
     75 
     76 
     77 CREATE FUNCTION master_table_purse_decision()
     78 RETURNS VOID
     79 LANGUAGE plpgsql
     80 AS $$
     81 BEGIN
     82   CREATE TRIGGER purse_decision_on_insert
     83     AFTER INSERT
     84      ON purse_decision
     85      FOR EACH ROW EXECUTE FUNCTION purse_decision_insert_trigger();
     86 END $$;
     87 
     88 
     89 INSERT INTO exchange_tables
     90     (name
     91     ,version
     92     ,action
     93     ,partitioned
     94     ,by_range)
     95   VALUES
     96     ('purse_decision'
     97     ,'exchange-0002'
     98     ,'create'
     99     ,TRUE
    100     ,FALSE),
    101     ('purse_decision'
    102     ,'exchange-0002'
    103     ,'constrain'
    104     ,TRUE
    105     ,FALSE),
    106     ('purse_decision'
    107     ,'exchange-0002'
    108     ,'master'
    109     ,TRUE
    110     ,FALSE);