syncdb_store_backup_TR.sql (2440B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2024 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 DROP FUNCTION IF EXISTS sync_do_store_backup; 19 CREATE FUNCTION sync_do_store_backup ( 20 IN in_account_pub BYTEA, 21 IN in_account_sig BYTEA, 22 IN in_backup_hash BYTEA, 23 IN in_backup_size INT4, 24 IN in_data BYTEA, 25 OUT out_payment_required BOOLEAN, 26 OUT out_old_backup_mismatch BOOLEAN, 27 OUT out_no_change BOOLEAN, 28 OUT out_inserted BOOLEAN) 29 LANGUAGE plpgsql 30 AS $$ 31 DECLARE 32 my_no_previous_hash BYTEA; 33 my_existing_hash BYTEA; 34 BEGIN 35 out_payment_required = FALSE; 36 out_old_backup_mismatch = FALSE; 37 out_no_change = FALSE; 38 out_inserted = FALSE; 39 my_no_previous_hash = '\x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'; 40 41 -- Check if account exists 42 PERFORM 1 43 FROM accounts 44 WHERE account_pub=in_account_pub; 45 46 IF NOT FOUND 47 THEN 48 out_payment_required = TRUE; 49 RETURN; 50 END IF; 51 52 -- Try to insert the backup 53 INSERT INTO backups 54 (account_pub 55 ,account_sig 56 ,prev_hash 57 ,backup_hash 58 ,data) 59 VALUES 60 (in_account_pub 61 ,in_account_sig 62 ,my_no_previous_hash 63 ,in_backup_hash 64 ,in_data) 65 ON CONFLICT DO NOTHING; 66 67 IF FOUND 68 THEN 69 out_inserted = TRUE; 70 RETURN; 71 END IF; 72 73 -- Check for existing backup hash 74 SELECT backup_hash 75 INTO my_existing_hash 76 FROM backups 77 WHERE account_pub=in_account_pub; 78 79 IF NOT FOUND 80 THEN 81 -- Shouldn't happen: account exists but insert failed and no backup exists 82 RAISE EXCEPTION 'unexpected state: account exists but no backup and insert failed'; 83 END IF; 84 85 IF my_existing_hash <> in_backup_hash 86 THEN 87 out_old_backup_mismatch = TRUE; 88 RETURN; 89 END IF; 90 91 -- Backup identical to what was provided 92 out_no_change = TRUE; 93 END $$;