]> Sergey Matveev's repositories - public-inbox.git/commitdiff
over: simplify read-only vs read-write checking
authorEric Wong <e@yhbt.net>
Tue, 4 Feb 2020 04:44:25 +0000 (04:44 +0000)
committerEric Wong <e@yhbt.net>
Tue, 4 Feb 2020 21:39:56 +0000 (21:39 +0000)
No need to call ref() and do a string comparison.  Add some
extra tests using the {ReadOnly} attribute in DBI.pm.

lib/PublicInbox/Over.pm
lib/PublicInbox/OverIdx.pm
t/over.t

index 0f8f433a9ae5a9803bef6a868869784dff4177bf..57c82bfc9efc440def82183ba4f46b8cfd0ad8a0 100644 (file)
@@ -14,17 +14,16 @@ use Compress::Zlib qw(uncompress);
 use constant DEFAULT_LIMIT => 1000;
 
 sub dbh_new {
-       my ($self) = @_;
-       my $ro = ref($self) eq 'PublicInbox::Over';
+       my ($self, $rw) = @_;
        my $f = $self->{filename};
-       if (!$ro && !-f $f) { # SQLite defaults mode to 0644, we want 0666
+       if ($rw && !-f $f) { # SQLite defaults mode to 0644, we want 0666
                open my $fh, '+>>', $f or die "failed to open $f: $!";
        }
        my $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', {
                AutoCommit => 1,
                RaiseError => 1,
                PrintError => 0,
-               ReadOnly => $ro,
+               ReadOnly => !$rw,
                sqlite_use_immediate_transaction => 1,
        });
        $dbh->{sqlite_unicode} = 1;
index 5f1007aa68767979cdc8dd0c9a2d8cd34dc886ae..a966a71000c37ab4458675095d8c79cad9fe0172 100644 (file)
@@ -20,7 +20,7 @@ use PublicInbox::Search;
 
 sub dbh_new {
        my ($self) = @_;
-       my $dbh = $self->SUPER::dbh_new;
+       my $dbh = $self->SUPER::dbh_new(1);
        $dbh->do('PRAGMA journal_mode = TRUNCATE');
        $dbh->do('PRAGMA cache_size = 80000');
        create_tables($dbh);
index 4e630bcd37d7ab6cc8f79ffd2e4c503f81f7dee3..daa7176f493d2b0cbda7c4aa7e45a696cbff99c5 100644 (file)
--- a/t/over.t
+++ b/t/over.t
@@ -18,10 +18,15 @@ is($y, $x+1, 'tid increases');
 $x = $over->sid('hello-world');
 is(int($x), $x, 'integer sid');
 $y = $over->sid('hello-WORLD');
-is($y, $x+1, 'sid ncreases');
+is($y, $x+1, 'sid increases');
 is($over->sid('hello-world'), $x, 'idempotent');
+ok(!$over->{dbh}->{ReadOnly}, 'OverIdx is not ReadOnly');
 $over->disconnect;
 
+$over = PublicInbox::Over->new("$tmpdir/over.sqlite3");
+$over->connect;
+ok($over->{dbh}->{ReadOnly}, 'Over is ReadOnly');
+
 $over = PublicInbox::OverIdx->new("$tmpdir/over.sqlite3");
 $over->connect;
 is($over->sid('hello-world'), $x, 'idempotent across reopen');