Skip to content

Commit

Permalink
refactor: moodle DB transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderschmitz committed Dec 15, 2022
1 parent ed603d0 commit 94577a1
Showing 1 changed file with 12 additions and 37 deletions.
49 changes: 12 additions & 37 deletions classes/package.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ public function store_in_db(int $contextid = 0) {
"icon" => $this->icon,
"license" => $this->license
];

$transaction = $DB->start_delegated_transaction();
$packageid = $DB->insert_record('qtype_questionpy_package', $packagedata);
$transaction->allow_commit();

// For each language store the localized package data as a separate record.
$languagedata = array();
Expand All @@ -231,25 +234,11 @@ public function store_in_db(int $contextid = 0) {
"tag" => $tag,
];
}
$transaction = $DB->start_delegated_transaction();
try {
try {
$DB->insert_records('qtype_questionpy_tags', $tagsdata);
$DB->insert_records('qtype_questionpy_language', $languagedata);
} catch (\dml_exception $e) {
$DB->rollback_delegated_transaction($transaction, $e);
}
} catch (\coding_exception | \dml_transaction_exception | \Throwable $e) {
throw new moodle_exception("dmltransactionexception", "error", "", null,
"Cannot delete records from DB. Transaction rollback failed.");
}

if ($transaction->is_disposed()) {
// Transaction was rolled back earlier.
throw new moodle_exception("invalidrecordunknown", "error", "", null,
"Cannot insert $tagsdata or $languagedata into table. Transaction rollback.");
}
$DB->commit_delegated_transaction($transaction);
$transaction = $DB->start_delegated_transaction();
$DB->insert_records('qtype_questionpy_tags', $tagsdata);
$DB->insert_records('qtype_questionpy_language', $languagedata);
$transaction->allow_commit();
}

/**
Expand All @@ -262,27 +251,13 @@ public function store_in_db(int $contextid = 0) {
*/
public function delete_from_db() {
global $DB;

$transaction = $DB->start_delegated_transaction();
$packageid = $DB->get_field('qtype_questionpy_package', 'id', ['hash' => $this->hash]);
try {
try {
$DB->delete_records('qtype_questionpy_package', ['id' => $packageid]);
$DB->delete_records('qtype_questionpy_language', ['packageid' => $packageid]);
$DB->delete_records('qtype_questionpy_tags', ['packageid' => $packageid]);
} catch (\dml_exception $e) {
$DB->rollback_delegated_transaction($transaction, $e);
}
} catch (\coding_exception | \dml_transaction_exception | \Throwable $e) {
throw new moodle_exception("dmltransactionexception", "error", "", null,
"Cannot delete records from DB. Transaction rollback failed: $e->debuginfo");
}

if ($transaction->is_disposed()) {
// Transaction was rolled back earlier.
throw new moodle_exception("invalidrecordunknown", "error", "", null,
"Cannot delete records from DB. Transaction rollback.");
}
$DB->commit_delegated_transaction($transaction);
$DB->delete_records('qtype_questionpy_package', ['id' => $packageid]);
$DB->delete_records('qtype_questionpy_language', ['packageid' => $packageid]);
$DB->delete_records('qtype_questionpy_tags', ['packageid' => $packageid]);
$transaction->allow_commit();
}

/**
Expand Down

0 comments on commit 94577a1

Please sign in to comment.