]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 1443537 - Add utility functions for switching database handle to writeable or...
authorDylan William Hardison <dylan@hardison.net>
Tue, 6 Mar 2018 18:55:13 +0000 (13:55 -0500)
committerGitHub <noreply@github.com>
Tue, 6 Mar 2018 18:55:13 +0000 (13:55 -0500)
Bugzilla/Util.pm
extensions/PhabBugz/lib/Feed.pm

index 91f06e650d74951430c4863c4babb74d47fd97e7..7d85a4dfd3a80ee725fd27b0ad2827547b002d20 100644 (file)
@@ -14,6 +14,7 @@ use warnings;
 use base qw(Exporter);
 @Bugzilla::Util::EXPORT = qw(trick_taint detaint_natural
                              detaint_signed
+                             with_writable_database with_readonly_database
                              html_quote url_quote xml_quote
                              css_class_quote html_light_quote
                              i_am_cgi i_am_webservice correct_urlbase remote_ip
@@ -44,6 +45,31 @@ use Encode qw(encode decode resolve_alias);
 use Encode::Guess;
 use POSIX qw(floor ceil);
 use Taint::Util qw(untaint);
+use Try::Tiny;
+
+sub with_writable_database(&) {
+    my ($code) = @_;
+    my $dbh = Bugzilla->dbh_main;
+    local Bugzilla->request_cache->{dbh} = $dbh;
+    local Bugzilla->request_cache->{error_mode} = ERROR_MODE_DIE;
+    try {
+        $dbh->bz_start_transaction;
+        $code->();
+        $dbh->bz_commit_transaction;
+    } catch {
+        $dbh->bz_rollback_transaction;
+        # re-throw
+        die $_;
+    };
+}
+
+sub with_readonly_database(&) {
+    my ($code) = @_;
+    local Bugzilla->request_cache->{dbh} = undef;
+    local Bugzilla->request_cache->{error_mode} = ERROR_MODE_DIE;
+    Bugzilla->switch_to_shadow_db();
+    $code->();
+}
 
 sub trick_taint {
     untaint($_[0]);
index 3b158d2d6fef55d4436e3bc39b00338e956d7844..9904d509075807e26778a9301cad2bd43419cd78 100644 (file)
@@ -15,7 +15,7 @@ use Moo;
 
 use Bugzilla::Constants;
 use Bugzilla::Search;
-use Bugzilla::Util qw(diff_arrays);
+use Bugzilla::Util qw(diff_arrays with_writable_database with_readonly_database);
 
 use Bugzilla::Extension::PhabBugz::Constants;
 use Bugzilla::Extension::PhabBugz::Policy;
@@ -105,7 +105,9 @@ sub feed_query {
             }
         }
 
-        $self->process_revision_change($object_phid, $story_text);
+        with_writable_database {
+            $self->process_revision_change($object_phid, $story_text);
+        };
         $self->save_last_id($story_id, 'feed');
     }
 
@@ -131,7 +133,9 @@ sub feed_query {
         $self->logger->debug("USER REALNAME: $user_realname");
         $self->logger->debug("OBJECT PHID: $object_phid");
 
-        $self->process_new_user($user_data);
+        with_readonly_database {
+            $self->process_new_user($user_data);
+        };
         $self->save_last_id($user_id, 'user');
     }
 }
@@ -170,10 +174,6 @@ sub process_revision_change {
 
     # Pre setup before making changes
     my $old_user = set_phab_user();
-    my $is_shadow_db = Bugzilla->is_shadow_db;                                                                                                                                                                      Bugzilla->switch_to_main_db if $is_shadow_db;
-    my $dbh = Bugzilla->dbh;
-    $dbh->bz_start_transaction;
-
     my $bug = Bugzilla::Bug->new({ id => $revision->bug_id, cache => 1 });
 
     # REVISION SECURITY POLICY
@@ -360,9 +360,6 @@ sub process_revision_change {
         Bugzilla::BugMail::Send($bug_id, { changer => Bugzilla->user });
     }
 
-    $dbh->bz_commit_transaction;
-    Bugzilla->switch_to_shadow_db if $is_shadow_db;
-
     Bugzilla->set_user($old_user);
 
     $self->logger->info('SUCCESS: Revision D' . $revision->id . ' processed');
@@ -384,8 +381,6 @@ sub process_new_user {
     # Pre setup before querying DB
     my $old_user = set_phab_user();
 
-    Bugzilla->switch_to_shadow_db();
-
     my $params = {
         f3  => 'OP',
         j3  => 'OR',