taler-api-0001.sql (3907B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2024-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 SELECT _v.register_patch('taler-api-0001', NULL, NULL); 17 18 CREATE SCHEMA taler_api; 19 SET search_path TO taler_api; 20 21 CREATE TYPE taler_amount AS (val INT8, frac INT4); 22 COMMENT ON TYPE taler_amount IS 'Stores an amount, fraction is in units of 1/100000000 of the base value'; 23 24 CREATE TABLE tx_in ( 25 tx_in_id INT8 PRIMARY KEY GENERATED ALWAYS AS IDENTITY, 26 amount taler_amount NOT NULL, 27 subject TEXT NOT NULL, 28 debit_payto TEXT NOT NULL, 29 created_at INT8 NOT NULL 30 ); 31 COMMENT ON TABLE tx_in IS 'Incoming transactions'; 32 33 CREATE TABLE tx_out ( 34 tx_out_id INT8 PRIMARY KEY GENERATED ALWAYS AS IDENTITY, 35 amount taler_amount NOT NULL, 36 subject TEXT NOT NULL, 37 credit_payto TEXT NOT NULL, 38 created_at INT8 NOT NULL 39 ); 40 COMMENT ON TABLE tx_out IS 'Outgoing transactions'; 41 42 CREATE TYPE incoming_type AS ENUM 43 ('reserve' ,'kyc', 'map'); 44 COMMENT ON TYPE incoming_type IS 'Types of incoming talerable transactions'; 45 46 CREATE TABLE taler_in ( 47 taler_in_id INT8 GENERATED ALWAYS AS IDENTITY PRIMARY KEY, 48 tx_in_id INT8 NOT NULL UNIQUE REFERENCES tx_in(tx_in_id) ON DELETE CASCADE, 49 type incoming_type NOT NULL, 50 account_pub BYTEA NOT NULL CHECK (LENGTH(account_pub)=32), 51 authorization_pub BYTEA CHECK (LENGTH(authorization_pub)=32), 52 authorization_sig BYTEA CHECK (LENGTH(authorization_sig)=64) 53 ); 54 COMMENT ON TABLE taler_in IS 'Incoming talerable transactions'; 55 56 CREATE UNIQUE INDEX taler_in_unique_reserve_pub ON taler_in (account_pub) WHERE type = 'reserve'; 57 58 CREATE TYPE transfer_status AS ENUM ( 59 'pending' 60 ,'transient_failure' 61 ,'permanent_failure' 62 ,'success' 63 ); 64 COMMENT ON TYPE transfer_status IS 'Status of a Wire Gateway transfer'; 65 66 CREATE TABLE transfer ( 67 transfer_id INT8 PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, 68 tx_out_id INT8 NOT NULL UNIQUE REFERENCES tx_out(tx_out_id) ON DELETE CASCADE, 69 request_uid BYTEA UNIQUE NOT NULL CHECK (LENGTH(request_uid)=64), 70 wtid BYTEA UNIQUE NOT NULL CHECK (LENGTH(wtid)=32), 71 exchange_base_url TEXT NOT NULL, 72 metadata TEXT, 73 status transfer_status NOT NULL, 74 status_msg TEXT 75 ); 76 COMMENT ON TABLE transfer IS 'Wire Gateway transfers'; 77 78 CREATE TABLE bounced( 79 tx_in_id INT8 NOT NULL UNIQUE REFERENCES tx_in(tx_in_id) ON DELETE CASCADE 80 ); 81 COMMENT ON TABLE bounced IS 'Bounced transaction'; 82 83 CREATE TABLE prepared_in ( 84 type incoming_type NOT NULL, 85 account_pub BYTEA NOT NULL CHECK (LENGTH(account_pub)=32), 86 authorization_pub BYTEA UNIQUE NOT NULL CHECK (LENGTH(authorization_pub)=32), 87 authorization_sig BYTEA NOT NULL CHECK (LENGTH(authorization_sig)=64), 88 recurrent BOOLEAN NOT NULL, 89 registered_at INT8 NOT NULL, 90 tx_in_id INT8 UNIQUE REFERENCES tx_in(tx_in_id) ON DELETE CASCADE 91 ); 92 COMMENT ON TABLE prepared_in IS 'Prepared incoming transaction'; 93 CREATE UNIQUE INDEX prepared_in_unique_reserve_pub 94 ON prepared_in (account_pub) WHERE type = 'reserve'; 95 96 CREATE TABLE pending_recurrent_in( 97 tx_in_id INT8 NOT NULL UNIQUE REFERENCES tx_in(tx_in_id) ON DELETE CASCADE, 98 authorization_pub BYTEA NOT NULL REFERENCES prepared_in(authorization_pub) 99 ); 100 CREATE INDEX pending_recurrent_inc_auth_pub 101 ON pending_recurrent_in (authorization_pub); 102 COMMENT ON TABLE pending_recurrent_in IS 'Pending recurrent incoming transaction'; 103