Permalink
Browse files

MDL-29198 DB - make delete_records transactional safe when needed

  • Loading branch information...
1 parent 15141ad commit 687e61cdeafd342311619d57a39d71cf8e26d485 @stronk7 stronk7 committed Sep 1, 2011
Showing with 22 additions and 1 deletion.
  1. +3 −1 lib/dml/moodle_database.php
  2. +19 −0 lib/dml/simpletest/testdml.php
View
4 lib/dml/moodle_database.php
@@ -1575,7 +1575,9 @@ public function record_exists_sql($sql, array $params=null) {
* @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);
View
19 lib/dml/simpletest/testdml.php
@@ -3771,6 +3771,25 @@ function test_concurent_transactions() {
$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();
}

0 comments on commit 687e61c

Please sign in to comment.