]> Sergey Matveev's repositories - public-inbox.git/commitdiff
msgmap: mid_insert ignores duplicates instead of die-ing
authorEric Wong <e@80x24.org>
Thu, 22 Jun 2017 19:51:23 +0000 (19:51 +0000)
committerEric Wong <e@80x24.org>
Thu, 22 Jun 2017 19:51:23 +0000 (19:51 +0000)
This will allow smoother imports as occasional Message-ID
duplicates happen and the best we can do is ignore the
second one.

lib/PublicInbox/Msgmap.pm
t/msgmap.t

index 3fb3805fe82abea0c23684890d1f33004e6815d5..a49b61e8b7f3636eb28cba61ca052c5000b5720c 100644 (file)
@@ -82,10 +82,10 @@ sub created_at {
 sub mid_insert {
        my ($self, $mid) = @_;
        my $dbh = $self->{dbh};
-       use constant MID_INSERT => 'INSERT INTO msgmap (mid) VALUES (?)';
-       my $sth = $self->{mid_insert} ||= $dbh->prepare(MID_INSERT);
+       my $sql = 'INSERT OR IGNORE INTO msgmap (mid) VALUES (?)';
+       my $sth = $self->{mid_insert} ||= $dbh->prepare($sql);
        $sth->bind_param(1, $mid);
-       $sth->execute;
+       return if $sth->execute == 0;
        $dbh->last_insert_id(undef, undef, 'msgmap', 'num');
 }
 
index 5c28e54df7542b7031081e5a37cecfa803a68c05..a5232fab6cbc06b75e0aeb3df541f2d8a43c3285 100644 (file)
@@ -27,9 +27,9 @@ foreach my $mid (@mids) {
 }
 
 $@ = undef;
-eval { $d->mid_insert('a@b') };
-ok($@, 'error raised when attempting duplicate message ID');
-
+my $ret = $d->mid_insert('a@b');
+is($ret, undef, 'duplicate mid_insert in undef result');
+is($d->num_for('a@b'), $mid2num{'a@b'}, 'existing number not clobbered');
 
 foreach my $n (keys %num2mid) {
        is($d->mid_for($n), $num2mid{$n}, "num:$n maps correctly");