Skip to content

Commit 70e5ef9

Browse files
author
vrana
committed
Use transactions in user status Conduit methods
Test Plan: Add status, verify DB. Remove status, verify DB. Reviewers: epriestley Reviewed By: epriestley CC: aran, Koolvin Differential Revision: https://secure.phabricator.com/D2397
1 parent ba98089 commit 70e5ef9

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/applications/conduit/method/user/addstatus/ConduitAPI_user_addstatus_Method.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,18 @@ protected function execute(ConduitAPIRequest $request) {
5858
throw new ConduitException('ERR-BAD-EPOCH');
5959
}
6060

61-
// TODO: This SELECT should have LOCK IN SHARE MODE and be in transaction
62-
// with the next INSERT.
63-
$overlap = id(new PhabricatorUserStatus())->loadAllWhere(
61+
$table = new PhabricatorUserStatus();
62+
$table->openTransaction();
63+
$table->beginWriteLocking();
64+
65+
$overlap = $table->loadAllWhere(
6466
'userPHID = %s AND dateFrom < %d AND dateTo > %d',
6567
$user_phid,
6668
$to,
6769
$from);
6870
if ($overlap) {
71+
$table->endWriteLocking();
72+
$table->killTransaction();
6973
throw new ConduitException('ERR-OVERLAP');
7074
}
7175

@@ -83,6 +87,9 @@ protected function execute(ConduitAPIRequest $request) {
8387
->setDateTo($to)
8488
->setStatus($status)
8589
->save();
90+
91+
$table->endWriteLocking();
92+
$table->saveTransaction();
8693
}
8794

8895
}

src/applications/conduit/method/user/removestatus/ConduitAPI_user_removestatus_Method.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ protected function execute(ConduitAPIRequest $request) {
5555
throw new ConduitException('ERR-BAD-EPOCH');
5656
}
5757

58-
$overlap = id(new PhabricatorUserStatus())->loadAllWhere(
58+
$table = new PhabricatorUserStatus();
59+
$table->openTransaction();
60+
$table->beginReadLocking();
61+
62+
$overlap = $table->loadAllWhere(
5963
'userPHID = %s AND dateFrom < %d AND dateTo > %d',
6064
$user_phid,
6165
$to,
@@ -80,6 +84,9 @@ protected function execute(ConduitAPIRequest $request) {
8084
$status->delete();
8185
}
8286
}
87+
88+
$table->endReadLocking();
89+
$table->saveTransaction();
8390
return count($overlap);
8491
}
8592

0 commit comments

Comments
 (0)