exchange

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

0002-aggregation_transient.sql (3517B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2014--2025 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 FUNCTION create_table_aggregation_transient(
     18   IN shard_suffix TEXT DEFAULT NULL
     19 )
     20 RETURNS VOID
     21 LANGUAGE plpgsql
     22 AS $$
     23 DECLARE
     24   table_name TEXT DEFAULT 'aggregation_transient';
     25 BEGIN
     26   PERFORM create_partitioned_table(
     27     'CREATE TABLE %I '
     28     '(amount taler_amount NOT NULL'
     29     ',wire_target_h_payto BYTEA CHECK (LENGTH(wire_target_h_payto)=32)'
     30     ',merchant_pub BYTEA CHECK (LENGTH(merchant_pub)=32)'
     31     ',exchange_account_section TEXT NOT NULL'
     32     ',legitimization_requirement_serial_id INT8 NOT NULL DEFAULT(0)'
     33     ',wtid_raw BYTEA NOT NULL CHECK (LENGTH(wtid_raw)=32)'
     34     ') %s ;'
     35     ,table_name
     36     ,'PARTITION BY HASH (wire_target_h_payto)'
     37     ,shard_suffix
     38   );
     39   PERFORM comment_partitioned_table(
     40     'aggregations currently happening (lacking wire_out, usually because the amount is too low); this table is not replicated'
     41     ,table_name
     42     ,shard_suffix
     43   );
     44   PERFORM comment_partitioned_column(
     45        'Sum of all of the aggregated deposits (without deposit fees)'
     46       ,'amount'
     47       ,table_name
     48       ,shard_suffix
     49   );
     50   PERFORM comment_partitioned_column(
     51        'public key of the merchant that authorized the deposits'
     52       ,'merchant_pub'
     53       ,table_name
     54       ,shard_suffix
     55   );
     56   PERFORM comment_partitioned_column(
     57        'unsalted hash of the (full) payto URI of the merchant account that should receive the funds'
     58       ,'wire_target_h_payto'
     59       ,table_name
     60       ,shard_suffix
     61   );
     62   PERFORM comment_partitioned_column(
     63        'identifier of the wire transfer'
     64       ,'wtid_raw'
     65       ,table_name
     66       ,shard_suffix
     67   );
     68 END
     69 $$;
     70 
     71 CREATE FUNCTION foreign_table_aggregation_transient()
     72 RETURNS void
     73 LANGUAGE plpgsql
     74 AS $$
     75 DECLARE
     76   table_name TEXT DEFAULT 'aggregation_transient';
     77 BEGIN
     78   EXECUTE FORMAT (
     79     'ALTER TABLE ' || table_name ||
     80     ' ADD CONSTRAINT ' || table_name || '_foreign_wire_target_h_payto'
     81     ' FOREIGN KEY (wire_target_h_payto) '
     82     ' REFERENCES wire_targets (wire_target_h_payto) ON DELETE RESTRICT'
     83   );
     84 END
     85 $$;
     86 
     87 CREATE FUNCTION constrain_table_aggregation_transient(
     88   IN partition_suffix TEXT
     89 )
     90 RETURNS VOID
     91 LANGUAGE plpgsql
     92 AS $$
     93 DECLARE
     94   table_name TEXT DEFAULT 'aggregation_transient';
     95 BEGIN
     96   table_name = concat_ws('_', table_name, partition_suffix);
     97   EXECUTE FORMAT (
     98     'ALTER TABLE ' || table_name ||
     99     ' ADD CONSTRAINT ' || table_name || '_wire_target_h_payto_and_wtid_unique'
    100     '   UNIQUE (wire_target_h_payto,wtid_raw)'
    101   );
    102 END $$;
    103 
    104 
    105 INSERT INTO exchange_tables
    106     (name
    107     ,version
    108     ,action
    109     ,partitioned
    110     ,by_range)
    111   VALUES
    112     ('aggregation_transient'
    113     ,'exchange-0002'
    114     ,'create'
    115     ,TRUE
    116     ,FALSE),
    117     ('aggregation_transient'
    118     ,'exchange-0002'
    119     ,'foreign'
    120     ,TRUE
    121     ,FALSE),
    122     ('aggregation_transient'
    123     ,'exchange-0002'
    124     ,'constrain'
    125     ,TRUE
    126     ,FALSE);