]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiMailSync.pm
lei_mail_sync: mv_src: use transaction, check UNIQUE
[public-inbox.git] / lib / PublicInbox / LeiMailSync.pm
index c6cd1bc58d0ae0b5d676b56288680f8dbb5c0eda..124eb96948c5421c1f1bd66478de7ca6bc0021ad 100644 (file)
@@ -48,7 +48,7 @@ sub lms_pause {
        my ($self) = @_;
        $self->{fmap} = {};
        my $dbh = delete $self->{dbh};
-       $dbh->do('PRAGMA optimize') if $dbh;
+       eval { $dbh->do('PRAGMA optimize') } if $dbh;
 }
 
 sub create_tables {
@@ -182,16 +182,20 @@ sub mv_src {
        my ($self, $folder, $oidbin, $id, $newbn) = @_;
        my $lk = $self->lock_for_scope;
        my $fid = $self->{fmap}->{$folder} //= fid_for($self, $folder, 1);
+       $self->{dbh}->begin_work;
        my $sth = $self->{dbh}->prepare_cached(<<'');
 UPDATE blob2name SET name = ? WHERE fid = ? AND oidbin = ? AND name = ?
 
-       my $nr = $sth->execute($newbn, $fid, $oidbin, $$id);
-       if ($nr == 0) { # may race with a clear_src, ensure new value exists
+       # eval since unique constraint may fail due to race
+       my $nr = eval { $sth->execute($newbn, $fid, $oidbin, $$id) };
+       if (!defined($nr) || $nr == 0) { # $nr may be `0E0'
+               # may race with a clear_src, ensure new value exists
                $sth = $self->{dbh}->prepare_cached(<<'');
 INSERT OR IGNORE INTO blob2name (oidbin, fid, name) VALUES (?, ?, ?)
 
                $sth->execute($oidbin, $fid, $newbn);
        }
+       $self->{dbh}->commit;
 }
 
 # read-only, iterates every oidbin + UID or name for a given folder
@@ -339,9 +343,12 @@ WHERE b.oidbin = ?
                        next unless -s $fh;
                        local $/;
                        my $raw = <$fh>;
-                       if ($vrfy && git_sha(1, \$raw)->hexdigest ne $oidhex) {
-                               warn "$f changed $oidhex\n";
-                               next;
+                       if ($vrfy) {
+                               my $got = git_sha(1, \$raw)->hexdigest;
+                               if ($got ne $oidhex) {
+                                       warn "$f changed $oidhex => $got\n";
+                                       next;
+                               }
                        }
                        return \$raw;
                }
@@ -466,7 +473,7 @@ sub arg2folder {
 # using `$res' instead of `$orig'
 EOM
                        } else {
-                               $lei->err($res) if defined $res;
+                               warn($res, "\n") if defined $res;
                                push @no, $orig;
                        }
                } elsif (m!\A(?:nntps?|s?news)://!i) {
@@ -478,7 +485,7 @@ EOM
 # using `$res' instead of `$orig'
 EOM
                        } else {
-                               $lei->err($res) if defined $res;
+                               warn($res, "\n") if defined $res;
                                push @no, $orig;
                        }
                } else {