From 8915fb70b0ba77188f0e9a1e7f7c5b6e1a946cbd Mon Sep 17 00:00:00 2001
From: tjhunt <tjhunt>
Date: Fri, 13 Feb 2009 02:49:25 +0000
Subject: [PATCH] admin dbtransfer: MDL-18225 also, let the caller control how
 transactions are used.

---
 lib/dtl/database_importer.php | 29 +++++++++++++++++++++++++++--
 lib/dtl/database_mover.php    |  8 ++++++++
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/lib/dtl/database_importer.php b/lib/dtl/database_importer.php
index 45a9dcb502..7d1b8bf4dc 100644
--- a/lib/dtl/database_importer.php
+++ b/lib/dtl/database_importer.php
@@ -35,6 +35,10 @@ class database_importer {
      * @see begin_database_import).
      */
     protected $check_schema;
+    /**
+     * How to use transactions.
+     */
+    protected $transactionmode = 'allinone';
 
     /**
      * Object constructor.
@@ -52,6 +56,17 @@ class database_importer {
         $this->check_schema = $check_schema;
     }
 
+    /**
+     * How to use transactions during the import.
+     * @param string $mode 'pertable', 'allinone' or 'none'.
+     */
+    public function set_transaction_mode($mode) {
+        if (!in_array($mode, array('pertable', 'allinone', 'none'))) {
+            throw new coding_exception('Unknown transaction mode', $mode);
+        }
+        $this->transactionmode = $mode;
+    }
+
     /**
      * Callback function. Should be called only once database per import
      * operation, before any database changes are made. It will check the database
@@ -89,7 +104,9 @@ class database_importer {
             }
             throw new dbtransfer_exception('importschemaexception', $details);
         }
-        $this->mdb->begin_sql();
+        if ($this->transactionmode == 'allinone') {
+            $this->mdb->begin_sql();
+        }
     }
 
     /**
@@ -104,6 +121,9 @@ class database_importer {
      * @return void
      */
     public function begin_table_import($tablename, $schemaHash) {
+        if ($this->transactionmode == 'pertable') {
+            $this->mdb->begin_sql();
+        }
         if (!$table = $this->schema->getTable($tablename)) {
             throw new dbtransfer_exception('unknowntableexception', $tablename);
         }
@@ -132,6 +152,9 @@ class database_importer {
                 return;
             }
         }
+        if ($this->transactionmode == 'pertable') {
+            $this->mdb->commit_sql();
+        }
     }
 
     /**
@@ -140,7 +163,9 @@ class database_importer {
      * @return void
      */
     public function finish_database_import() {
-        $this->mdb->commit_sql();
+        if ($this->transactionmode == 'allinone') {
+            $this->mdb->commit_sql();
+        }
     }
 
     /**
diff --git a/lib/dtl/database_mover.php b/lib/dtl/database_mover.php
index 6236a88181..3856a520fd 100644
--- a/lib/dtl/database_mover.php
+++ b/lib/dtl/database_mover.php
@@ -31,6 +31,14 @@ class database_mover extends database_exporter {
         $this->importer = new database_importer($mdb_target, $check_schema);
     }
 
+    /**
+     * How to use transactions during the transfer.
+     * @param string $mode 'pertable', 'allinone' or 'none'.
+     */
+    public function set_transaction_mode($mode) {
+        $this->importer->set_transaction_mode($mode);
+    }
+
     /**
      * Callback function. Calls importer's begin_database_import callback method.
      *
-- 
2.39.5