exchange

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

0002-policy_fulfillments.sql (3002B)


      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 -- @author: Özgür Kesim
     18 
     19 CREATE FUNCTION create_table_policy_fulfillments(
     20   IN partition_suffix TEXT DEFAULT NULL
     21 )
     22 RETURNS VOID
     23 LANGUAGE plpgsql
     24 AS $$
     25 DECLARE
     26   table_name TEXT DEFAULT 'policy_fulfillments';
     27 BEGIN
     28   PERFORM create_partitioned_table(
     29     'CREATE TABLE %I '
     30       '(h_fulfillment_proof gnunet_hashcode PRIMARY KEY'
     31       ',fulfillment_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
     32       ',fulfillment_timestamp INT8 NOT NULL'
     33       ',fulfillment_proof TEXT'
     34       ',policy_hash_codes gnunet_hashcode[] NOT NULL'
     35       ') %s ;'
     36     ,table_name
     37     ,'PARTITION BY HASH (h_fulfillment_proof)'
     38     ,partition_suffix
     39   );
     40   PERFORM comment_partitioned_table(
     41     'Proofs of fulfillment of policies that were set in deposits'
     42     ,table_name
     43     ,partition_suffix
     44   );
     45   PERFORM comment_partitioned_column(
     46     'Timestamp of the arrival of a proof of fulfillment'
     47     ,'fulfillment_timestamp'
     48     ,table_name
     49     ,partition_suffix
     50   );
     51   PERFORM comment_partitioned_column(
     52     'JSON object with a proof of the fulfillment of a policy. Supported details depend on the policy extensions supported by the exchange.'
     53     ,'fulfillment_proof'
     54     ,table_name
     55     ,partition_suffix
     56   );
     57   PERFORM comment_partitioned_column(
     58     'Hash of the fulfillment_proof'
     59     ,'h_fulfillment_proof'
     60     ,table_name
     61     ,partition_suffix
     62   );
     63   PERFORM comment_partitioned_column(
     64     'Array of the policy_hash_code''s of all policy_details that are fulfilled by this proof'
     65     ,'policy_hash_codes'
     66     ,table_name
     67     ,partition_suffix
     68   );
     69 END
     70 $$;
     71 
     72 COMMENT ON FUNCTION create_table_policy_fulfillments
     73   IS 'Creates the policy_fulfillments table';
     74 
     75 CREATE FUNCTION constrain_table_policy_fulfillments(
     76   IN partition_suffix TEXT
     77 )
     78 RETURNS VOID
     79 LANGUAGE plpgsql
     80 AS $$
     81 DECLARE
     82   partition_name TEXT;
     83 BEGIN
     84   partition_name = concat_ws('_', 'policy_fulfillments', partition_suffix);
     85 
     86   EXECUTE FORMAT (
     87     'ALTER TABLE ' || partition_name ||
     88     ' ADD CONSTRAINT ' || partition_name || '_serial_id '
     89     ' UNIQUE (h_fulfillment_proof, fulfillment_id)'
     90   );
     91 END
     92 $$;
     93 INSERT INTO exchange_tables
     94   (name
     95   ,version
     96   ,action
     97   ,partitioned
     98   ,by_range)
     99 VALUES
    100   ('policy_fulfillments', 'exchange-0002', 'create',    TRUE ,FALSE),
    101   ('policy_fulfillments', 'exchange-0002', 'constrain', TRUE ,FALSE);