[Rt-commit] rt branch 4.4/importer-no-auto-commit-handle-originalid created. rt-4.4.5-113-gd227b75283

BPS Git Server git at git.bestpractical.com
Tue Jun 7 19:17:54 UTC 2022


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt".

The branch, 4.4/importer-no-auto-commit-handle-originalid has been created
        at  d227b75283a5dd92f9dbf5ea13776521bf22074f (commit)

- Log -----------------------------------------------------------------
commit d227b75283a5dd92f9dbf5ea13776521bf22074f
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Jun 8 01:36:39 2022 +0800

    Refactor the handle of --no-auto-commit to support --originalid
    
    When importing with --originalid, we need to add ticket custom field
    values using RT API, which calls $RT::Handle->BeginTransaction in
    RT::CustomField::AddValueForObject. But if AutoCommit if off, the
    BeginTransaction would error out at least with MySQL:
    
        DBD::mysql::db begin_work failed: Already in a transaction
    
    This commit refactors code to not turn off AutoCommit globally, but adds
    an additional transaction for each data file, which implicitly turns off
    AutoCommit and is also compatible with other BeginTransaction calls.

diff --git a/lib/RT/Migrate/Importer/File.pm b/lib/RT/Migrate/Importer/File.pm
index 8f6d5f5efd..2725cf572f 100644
--- a/lib/RT/Migrate/Importer/File.pm
+++ b/lib/RT/Migrate/Importer/File.pm
@@ -106,6 +106,7 @@ sub Import {
                 or die "Can't seek to $self->{Seek} in $self->{Filename}";
             $self->{Seek} = undef;
         }
+        $RT::Handle->BeginTransaction() unless $self->{AutoCommit};
         while (not eof($fh)) {
             $self->{Position} = tell($fh);
 
@@ -114,7 +115,7 @@ sub Import {
 
             $self->ReadStream( $fh );
         }
-        RT->DatabaseHandle->dbh->commit unless $self->{AutoCommit};
+        $RT::Handle->Commit() unless $self->{AutoCommit};
     }
 
     $self->CloseStream;
@@ -184,6 +185,19 @@ sub RestoreState {
 sub SaveState {
     my $self = shift;
 
+    unless ( $self->{AutoCommit} ) {
+        for my $depth ( reverse 1 .. ( $RT::Handle->TransactionDepth || 0 ) ) {
+            if ( $depth == 1 ) {
+                # This is the transaction we added for AutoCommit
+                $RT::Handle->Commit;
+            }
+            else {
+                # If there are nested transactions unfinished yet, roll them back.
+                $RT::Handle->Rollback;
+            }
+        }
+    }
+
     RT->DatabaseHandle->dbh->commit unless $self->{AutoCommit};
 
     my %data;
diff --git a/sbin/rt-importer.in b/sbin/rt-importer.in
index 2cb679461a..cda0c284ec 100644
--- a/sbin/rt-importer.in
+++ b/sbin/rt-importer.in
@@ -144,8 +144,6 @@ elsif ($OPT{'ignore-errors'}) {
     };
 }
 
-RT->DatabaseHandle->AutoCommit(0) unless $OPT{'auto-commit'};
-
 my $import = RT::Migrate::Importer::File->new(
     Directory           => $dir,
     OriginalId          => $OPT{originalid},

-----------------------------------------------------------------------


hooks/post-receive
-- 
rt


More information about the rt-commit mailing list