sync

Backup service to store encrypted wallet databases (experimental)
Log | Files | Refs | Submodules | README | LICENSE

syncdb_update_backup_TR.sql (2663B)


      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_update_backup;
     19 CREATE FUNCTION sync_do_update_backup (
     20   IN in_account_pub BYTEA,
     21   IN in_old_backup_hash BYTEA,
     22   IN in_account_sig BYTEA,
     23   IN in_backup_hash BYTEA,
     24   IN in_backup_size INT4,
     25   IN in_data BYTEA,
     26   OUT out_updated BOOLEAN,
     27   OUT out_payment_required BOOLEAN,
     28   OUT out_old_backup_missing BOOLEAN,
     29   OUT out_old_backup_mismatch BOOLEAN,
     30   OUT out_no_change BOOLEAN)
     31 LANGUAGE plpgsql
     32 AS $$
     33 DECLARE
     34   my_existing_hash BYTEA;
     35 BEGIN
     36   out_updated = FALSE;
     37   out_payment_required = FALSE;
     38   out_old_backup_missing = FALSE;
     39   out_old_backup_mismatch = FALSE;
     40   out_no_change = FALSE;
     41 
     42   -- Try to update the backup (matches on account_pub AND old backup_hash)
     43   UPDATE backups
     44      SET backup_hash=in_backup_hash
     45         ,account_sig=in_account_sig
     46         ,prev_hash=in_old_backup_hash
     47         ,data=in_data
     48    WHERE account_pub=in_account_pub
     49      AND backup_hash=in_old_backup_hash;
     50 
     51   IF FOUND
     52   THEN
     53     out_updated = TRUE;
     54     RETURN;
     55   END IF;
     56 
     57   -- Update failed; check if account exists
     58   PERFORM 1
     59     FROM accounts
     60    WHERE account_pub=in_account_pub;
     61 
     62   IF NOT FOUND
     63   THEN
     64     out_payment_required = TRUE;
     65     RETURN;
     66   END IF;
     67 
     68   -- Account exists; check existing backup hash
     69   SELECT backup_hash
     70     INTO my_existing_hash
     71     FROM backups
     72    WHERE account_pub=in_account_pub;
     73 
     74   IF NOT FOUND
     75   THEN
     76     out_old_backup_missing = TRUE;
     77     RETURN;
     78   END IF;
     79 
     80   -- Had an existing backup; is the new hash identical?
     81   IF my_existing_hash = in_backup_hash
     82   THEN
     83     out_no_change = TRUE;
     84     RETURN;
     85   END IF;
     86 
     87   -- Check if existing hash matches expected old hash
     88   IF my_existing_hash = in_old_backup_hash
     89   THEN
     90     -- All constraints satisfied but update failed => hard error
     91     -- (this shouldn't normally happen)
     92     RAISE EXCEPTION 'unexpected state: backup hash matches but update failed';
     93   END IF;
     94 
     95   -- Previous backup does not match old_backup_hash
     96   out_old_backup_mismatch = TRUE;
     97 END $$;