r9166@llin: dpavlin | 2006-01-31 17:36:56 +0100
[BackupPC.git] / bin / BackupPC_updatedb
index 5e7fcf7..fc282ac 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,33 @@ if ($opt{c}) {
                $dbh->do( qq{ CREATE SEQUENCE $seq } );
        }
 
+       print " creating triggers ";
+       $dbh->do( qq{
+               create or replace function backup_parts_check() returns trigger as '
+               declare
+                       b_parts integer;
+                       b_counted integer;
+                       b_id    integer;
+               begin
+                       if (TG_OP=''UPDATE'') then
+                               b_id := old.id;
+                               b_parts := old.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);
+                       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();
+       });
 
        print "...\n";