do_insert_recdoc_payment.sql (2115B)
1 -- 2 -- This file is part of Anastasis 3 -- Copyright (C) 2026 Anastasis SARL 4 -- 5 -- ANASTASIS 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 -- ANASTASIS 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 -- ANASTASIS; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 -- 16 17 DROP FUNCTION IF EXISTS anastasis_do_insert_recdoc_payment; 18 CREATE FUNCTION anastasis_do_insert_recdoc_payment ( 19 IN in_user_id BYTEA, 20 IN in_post_counter INT4, 21 IN in_amount taler_amount, 22 IN in_payment_identifier BYTEA, 23 IN in_creation_date INT8, 24 IN in_transient_expiration INT8) -- lifetime given to an account created here 25 RETURNS void 26 LANGUAGE plpgsql 27 AS $$ 28 BEGIN 29 30 -- anastasis_recdoc_payment references anastasis_user, so the account has to 31 -- exist first. ON CONFLICT DO NOTHING settles the race between two first 32 -- uploads for the same account inside the statement; the SELECT ... FOR 33 -- UPDATE this replaces held no lock at all, because it ran in its own 34 -- autocommit transaction and released the row before the inserts. 35 INSERT INTO anastasis_user 36 (user_id 37 ,expiration_date) 38 VALUES 39 (in_user_id 40 ,in_transient_expiration) 41 ON CONFLICT (user_id) DO NOTHING; 42 43 INSERT INTO anastasis_recdoc_payment 44 (user_id 45 ,post_counter 46 ,amount 47 ,payment_identifier 48 ,creation_date) 49 VALUES 50 (in_user_id 51 ,in_post_counter 52 ,in_amount 53 ,in_payment_identifier 54 ,in_creation_date); 55 56 END $$; 57 58 COMMENT ON FUNCTION anastasis_do_insert_recdoc_payment 59 IS 'Records the start of a payment for a recovery document upload, creating the account with a transient lifetime if it does not exist yet. Atomic, so concurrent first uploads for one account cannot collide on the primary key.';