From 7a8a938077150b1cfd0994f60afbc8f916dafe94 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Thu, 1 Sep 2011 11:05:21 +0200 Subject: [PATCH] MDL-29198 DB - make delete_records transactional safe when needed --- lib/dml/moodle_database.php | 4 +++- lib/dml/simpletest/testdml.php | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index db88a9efe7b..d8bb8348205 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -1595,7 +1595,9 @@ abstract class moodle_database { * @throws dml_exception if error */ public function delete_records($table, array $conditions=null) { - if (is_null($conditions)) { + // truncate is drop/create (DDL), not transactional safe, + // so we don't use the shortcut within them. MDL-29198 + if (is_null($conditions) && empty($this->transactions)) { return $this->execute("TRUNCATE TABLE {".$table."}"); } list($select, $params) = $this->where_clause($table, $conditions); diff --git a/lib/dml/simpletest/testdml.php b/lib/dml/simpletest/testdml.php index 40ba8557a28..eb3f555e455 100644 --- a/lib/dml/simpletest/testdml.php +++ b/lib/dml/simpletest/testdml.php @@ -3851,6 +3851,25 @@ class dml_test extends UnitTestCase { $transaction->allow_commit(); $this->assertEqual(2, $DB2->count_records($tablename)); + // let's try delete all is also working on (this checks MDL-29198) + // initially both connections see all the records in the table (2) + $this->assertEqual(2, $DB->count_records($tablename)); + $this->assertEqual(2, $DB2->count_records($tablename)); + $transaction = $DB->start_delegated_transaction(); + + // delete all from within transaction + $DB->delete_records($tablename); + + // transactional $DB, sees 0 records now + $this->assertEqual(0, $DB->count_records($tablename)); + + // others ($DB2) get no changes yet + $this->assertEqual(2, $DB2->count_records($tablename)); + + // now commit and we should see changes + $transaction->allow_commit(); + $this->assertEqual(0, $DB2->count_records($tablename)); + $DB2->dispose(); }