From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 26CDF1F884 for ; Tue, 4 Feb 2020 04:44:26 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 5/5] over: simplify read-only vs read-write checking Date: Tue, 4 Feb 2020 04:44:25 +0000 Message-Id: <20200204044425.14031-6-e@yhbt.net> In-Reply-To: <20200204044425.14031-1-e@yhbt.net> References: <20200204044425.14031-1-e@yhbt.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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');