/[BackupPC]/trunk/sql/09_backup_parts_trigger.sql
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/sql/09_backup_parts_trigger.sql

Parent Directory Parent Directory | Revision Log Revision Log


Revision 370 - (show annotations)
Mon May 7 07:33:49 2007 UTC (17 years, 1 month ago) by iklaric
File size: 2137 byte(s)
fixed problem with invalid trigger when deleting entries from backup_parts


1 alter table backups alter column parts set default 0;
2
3 create or replace function backup_parts_check() returns trigger as '
4 declare
5 b_parts integer;
6 b_counted integer;
7 b_id integer;
8 begin
9 -- raise notice ''old/new parts %/% backup_id %/%'', old.parts, new.parts, old.id, new.id;
10 if (TG_OP=''UPDATE'') then
11 b_id := new.id;
12 b_parts := new.parts;
13 elsif (TG_OP = ''INSERT'') then
14 b_id := new.id;
15 b_parts := new.parts;
16 end if;
17 b_counted := (select count(*) from backup_parts where backup_id = b_id);
18 -- raise notice ''backup % parts %'', b_id, b_parts;
19 if ( b_parts != b_counted ) then
20 raise exception ''Update of backup % aborted, requested % parts and there are really % parts'', b_id, b_parts, b_counted;
21 end if;
22 return null;
23 end;
24 ' language plpgsql;
25
26 drop trigger do_backup_parts_check on backups;
27
28 create trigger do_backup_parts_check
29 after insert or update or delete on backups
30 for each row execute procedure backup_parts_check();
31
32 create or replace function backup_backup_parts_check() returns trigger as '
33 declare
34 b_id integer;
35 old_b_id integer;
36 my_part_nr integer;
37 calc_part integer;
38 begin
39 if (TG_OP = ''INSERT'') then
40 raise notice ''trigger: % backup_id %'', TG_OP, new.backup_id;
41 b_id = new.backup_id;
42 my_part_nr = new.part_nr;
43 execute ''update backups set parts = parts + 1 where id = '' || b_id;
44 elsif (TG_OP = ''DELETE'') then
45 raise notice ''trigger: % backup_id %, old.part_nr %'', TG_OP, old.backup_id, old.part_nr;
46 b_id = old.backup_id;
47 my_part_nr = old.part_nr - 1;
48 execute ''update backups set parts = parts - 1 where id = '' || b_id;
49 end if;
50 calc_part := (select count(part_nr) from backup_parts where backup_id = b_id);
51 if ( my_part_nr != calc_part ) then
52 raise exception ''Update of backup_parts with backup_id % aborted, requested part_nr is % and calulated next is %'', b_id, my_part_nr, calc_part;
53
54 end if;
55 return null;
56 end;
57 ' language plpgsql;
58
59 drop trigger do_backup_backup_parts_check on backup_parts;
60
61 create trigger do_backup_backup_parts_check
62 after insert or update or delete on backup_parts
63 for each row execute procedure backup_backup_parts_check();
64

  ViewVC Help
Powered by ViewVC 1.1.26