380 |
} |
} |
381 |
|
|
382 |
print " creating triggers "; |
print " creating triggers "; |
383 |
$dbh->do( qq{ |
$dbh->do( <<__END_OF_TRIGGER__ ); |
384 |
create or replace function backup_parts_check() returns trigger as ' |
|
385 |
declare |
create or replace function backup_parts_check() returns trigger as ' |
386 |
b_parts integer; |
declare |
387 |
b_counted integer; |
b_parts integer; |
388 |
b_id integer; |
b_counted integer; |
389 |
begin |
b_id integer; |
390 |
if (TG_OP=''UPDATE'') then |
begin |
391 |
b_id := old.id; |
-- raise notice ''old/new parts %/% backup_id %/%'', old.parts, new.parts, old.id, new.id; |
392 |
b_parts := old.parts; |
if (TG_OP=''UPDATE'') then |
393 |
elsif (TG_OP = ''INSERT'') then |
b_id := new.id; |
394 |
b_id := new.id; |
b_parts := new.parts; |
395 |
b_parts := new.parts; |
elsif (TG_OP = ''INSERT'') then |
396 |
end if; |
b_id := new.id; |
397 |
b_counted := (select count(*) from backup_parts where backup_id = b_id); |
b_parts := new.parts; |
398 |
if ( b_parts != b_counted ) then |
end if; |
399 |
raise exception ''Update of backup % aborted, requested % parts and there are really % parts'', b_id, b_parts, b_counted; |
b_counted := (select count(*) from backup_parts where backup_id = b_id); |
400 |
end if; |
-- raise notice ''backup % parts %'', b_id, b_parts; |
401 |
return null; |
if ( b_parts != b_counted ) then |
402 |
end; |
raise exception ''Update of backup % aborted, requested % parts and there are really % parts'', b_id, b_parts, b_counted; |
403 |
' language plpgsql; |
end if; |
404 |
|
return null; |
405 |
create trigger do_backup_parts_check |
end; |
406 |
after insert or update or delete on backups |
' language plpgsql; |
407 |
for each row execute procedure backup_parts_check(); |
|
408 |
}); |
create trigger do_backup_parts_check |
409 |
|
after insert or update or delete on backups |
410 |
|
for each row execute procedure backup_parts_check(); |
411 |
|
|
412 |
|
create or replace function backup_backup_parts_check() returns trigger as ' |
413 |
|
declare |
414 |
|
b_id integer; |
415 |
|
my_part_nr integer; |
416 |
|
calc_part integer; |
417 |
|
begin |
418 |
|
if (TG_OP = ''INSERT'') then |
419 |
|
-- raise notice ''trigger: % backup_id %'', TG_OP, new.backup_id; |
420 |
|
b_id = new.backup_id; |
421 |
|
my_part_nr = new.part_nr; |
422 |
|
execute ''update backups set parts = parts + 1 where id = '' || b_id; |
423 |
|
elsif (TG_OP = ''DELETE'') then |
424 |
|
-- raise notice ''trigger: % backup_id %'', TG_OP, old.backup_id; |
425 |
|
b_id = old.backup_id; |
426 |
|
my_part_nr = old.part_nr; |
427 |
|
execute ''update backups set parts = parts - 1 where id = '' || b_id; |
428 |
|
end if; |
429 |
|
calc_part := (select count(part_nr) from backup_parts where backup_id = b_id); |
430 |
|
if ( my_part_nr != calc_part ) then |
431 |
|
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; |
432 |
|
end if; |
433 |
|
return null; |
434 |
|
end; |
435 |
|
' language plpgsql; |
436 |
|
|
437 |
|
create trigger do_backup_backup_parts_check |
438 |
|
after insert or update or delete on backup_parts |
439 |
|
for each row execute procedure backup_backup_parts_check(); |
440 |
|
|
441 |
|
__END_OF_TRIGGER__ |
442 |
|
|
443 |
print "...\n"; |
print "...\n"; |
444 |
|
|