r9171@llin: dpavlin | 2006-01-31 23:04:30 +0100
[BackupPC.git] / bin / BackupPC_updatedb
index 5e7fcf7..3657d28 100755 (executable)
@@ -113,8 +113,8 @@ sub hest_update {
 
        my $skip_check = $opt{j} && print STDERR "Skipping check for existing files -- this should be used only with initital import\n";
 
-       unless (defined($index_node_url)) {
-               print STDERR "HyperEstraier support not enabled in configuration\n";
+       unless ($index_node_url && $index_node_url =~ m#^http://#) {
+               print STDERR "HyperEstraier support not enabled or index node invalid\n" if ($debug);
                $index_node_url = 0;
                return;
        }
@@ -126,8 +126,8 @@ sub hest_update {
        my $offset = 0;
        my $added = 0;
 
-       print " opening index $index_node_url";
        if ($index_node_url) {
+               print " opening index $index_node_url";
                $hest_node ||= Search::Estraier::Node->new(
                        url => $index_node_url,
                        user => 'admin',
@@ -135,8 +135,6 @@ sub hest_update {
                        croak_on_error => 1,
                );
                print " via node URL";
-       } else {
-               die "don't know how to use Hyper Estraier Index $index_node_url";
        }
 
        my $results = 0;
@@ -199,7 +197,7 @@ sub hest_update {
                while (my $row = $sth->fetchrow_hashref()) {
 
                        my $uri = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
-                       unless ($skip_check) {
+                       if (! $skip_check && $hest_node) {
                                my $id = $hest_node->uri_to_id($uri);
                                next if ($id && $id == -1);
                        }
@@ -226,11 +224,8 @@ sub hest_update {
                        print STDERR $doc->dump_draft,"\n" if ($debug > 1);
 
                        # register the document object to the database
-                       if ($hest_node) {
-                               $hest_node->put_doc($doc);
-                       } else {
-                               die "not supported";
-                       }
+                       $hest_node->put_doc($doc) if ($hest_node);
+
                        $added++;
                }
 
@@ -304,7 +299,7 @@ if ($opt{c}) {
                        size    bigint          not null,
                        inc_size bigint         not null default -1,
                        inc_deleted boolean     default false,
-                       parts   integer         not null default 1,
+                       parts   integer         not null default 0,
                        PRIMARY KEY(id)
                );            
 
@@ -384,6 +379,66 @@ if ($opt{c}) {
                $dbh->do( qq{ CREATE SEQUENCE $seq } );
        }
 
+       print " creating triggers ";
+       $dbh->do( <<__END_OF_TRIGGER__ );
+
+create or replace function backup_parts_check() returns trigger as '
+declare
+       b_parts integer;
+       b_counted integer;
+       b_id    integer;
+begin
+       -- raise notice ''old/new parts %/% backup_id %/%'', old.parts, new.parts, old.id, new.id;
+       if (TG_OP=''UPDATE'') then
+               b_id := new.id;
+               b_parts := new.parts;
+       elsif (TG_OP = ''INSERT'') then
+               b_id := new.id;
+               b_parts := new.parts;
+       end if;
+       b_counted := (select count(*) from backup_parts where backup_id = b_id);
+       -- raise notice ''backup % parts %'', b_id, b_parts;
+       if ( b_parts != b_counted ) then
+               raise exception ''Update of backup % aborted, requested % parts and there are really % parts'', b_id, b_parts, b_counted;
+       end if;
+       return null;
+end;
+' language plpgsql;
+
+create trigger do_backup_parts_check
+       after insert or update or delete on backups
+       for each row execute procedure backup_parts_check();
+
+create or replace function backup_backup_parts_check() returns trigger as '
+declare
+       b_id            integer;
+       my_part_nr      integer;
+       calc_part       integer;
+begin
+       if (TG_OP = ''INSERT'') then
+               -- raise notice ''trigger: % backup_id %'', TG_OP, new.backup_id;
+               b_id = new.backup_id;
+               my_part_nr = new.part_nr;
+               execute ''update backups set parts = parts + 1 where id = '' || b_id;
+       elsif (TG_OP = ''DELETE'') then
+               -- raise notice ''trigger: % backup_id %'', TG_OP, old.backup_id;
+               b_id = old.backup_id;
+               my_part_nr = old.part_nr;
+               execute ''update backups set parts = parts - 1 where id = '' || b_id;
+       end if;
+       calc_part := (select count(part_nr) from backup_parts where backup_id = b_id);
+       if ( my_part_nr != calc_part ) then
+               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;
+       end if;
+       return null;
+end;
+' language plpgsql;
+
+create trigger do_backup_backup_parts_check
+       after insert or update or delete on backup_parts
+       for each row execute procedure backup_backup_parts_check();
+
+__END_OF_TRIGGER__
 
        print "...\n";