From 253a86b7bf4a3d81118e91f4ebb77844cfbd2b32 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 4 Feb 2020 04:44:25 +0000 Subject: [PATCH] over: simplify read-only vs read-write checking 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 | 7 +++---- lib/PublicInbox/OverIdx.pm | 2 +- t/over.t | 7 ++++++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/PublicInbox/Over.pm b/lib/PublicInbox/Over.pm index 0f8f433a..57c82bfc 100644 --- a/lib/PublicInbox/Over.pm +++ b/lib/PublicInbox/Over.pm @@ -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; diff --git a/lib/PublicInbox/OverIdx.pm b/lib/PublicInbox/OverIdx.pm index 5f1007aa..a966a710 100644 --- a/lib/PublicInbox/OverIdx.pm +++ b/lib/PublicInbox/OverIdx.pm @@ -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); diff --git a/t/over.t b/t/over.t index 4e630bcd..daa7176f 100644 --- 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'); -- 2.44.0