Skip to content

Commit 5193c50

Browse files
committed
Add human readable value to SQL
1 parent 0930009 commit 5193c50

File tree

4 files changed

+43
-43
lines changed

4 files changed

+43
-43
lines changed

docker-compose.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: "3.9"
2-
31
services:
42
app:
53
container_name: php-db-locker-app

src/LockId/PostgresLockId.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(
3333
}
3434

3535
public static function fromLockId(
36-
LockId $lockId
36+
LockId $lockId,
3737
): self {
3838
$humanReadableValue = (string)$lockId;
3939

src/Locker/PostgresAdvisoryLocker.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919

2020
final class PostgresAdvisoryLocker
2121
{
22-
public function acquireLock(
22+
public function tryAcquireLock(
2323
PDO $dbConnection,
2424
PostgresLockId $postgresLockId,
2525
): bool {
26+
// TODO: Need to cleanup humanReadableValue?
2627
$statement = $dbConnection->prepare(
2728
<<<SQL
28-
SELECT pg_try_advisory_lock(:lock_id);
29+
SELECT pg_try_advisory_lock(:lock_id); -- $postgresLockId->humanReadableValue
2930
SQL
3031
);
3132
$statement->execute(
@@ -37,7 +38,7 @@ public function acquireLock(
3738
return $statement->fetchColumn(0);
3839
}
3940

40-
public function acquireLockWithinTransaction(
41+
public function tryAcquireLockWithinTransaction(
4142
PDO $dbConnection,
4243
PostgresLockId $postgresLockId,
4344
): bool {
@@ -49,9 +50,10 @@ public function acquireLockWithinTransaction(
4950
);
5051
}
5152

53+
// TODO: Need to cleanup humanReadableValue?
5254
$statement = $dbConnection->prepare(
5355
<<<SQL
54-
SELECT pg_try_advisory_xact_lock(:lock_id);
56+
SELECT pg_try_advisory_xact_lock(:lock_id); -- $postgresLockId->humanReadableValue
5557
SQL
5658
);
5759
$statement->execute(
@@ -68,7 +70,7 @@ public function releaseLock(
6870
PostgresLockId $postgresLockId,
6971
): bool {
7072
$statement = $dbConnection->prepare(
71-
<<<SQL
73+
<<<'SQL'
7274
SELECT pg_advisory_unlock(:lock_id);
7375
SQL
7476
);
@@ -85,7 +87,7 @@ public function releaseAllLocks(
8587
PDO $dbConnection,
8688
): void {
8789
$statement = $dbConnection->prepare(
88-
<<<SQL
90+
<<<'SQL'
8991
SELECT pg_advisory_unlock_all();
9092
SQL
9193
);

test/Integration/Locker/PostgresAdvisoryLockerTest.php

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function test_it_can_acquire_lock(): void
3030
$dbConnection = $this->initPostgresPdoConnection();
3131
$postgresLockId = $this->initPostgresLockId('test');
3232

33-
$isLockAcquired = $locker->acquireLock($dbConnection, $postgresLockId);
33+
$isLockAcquired = $locker->tryAcquireLock($dbConnection, $postgresLockId);
3434

3535
$this->assertTrue($isLockAcquired);
3636
$this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId);
@@ -43,7 +43,7 @@ public function test_it_can_acquire_lock_with_smallest_lock_id(): void
4343
$dbConnection = $this->initPostgresPdoConnection();
4444
$postgresLockId = new PostgresLockId(self::DB_INT64_VALUE_MIN);
4545

46-
$isLockAcquired = $locker->acquireLock($dbConnection, $postgresLockId);
46+
$isLockAcquired = $locker->tryAcquireLock($dbConnection, $postgresLockId);
4747

4848
$this->assertTrue($isLockAcquired);
4949
$this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId);
@@ -56,7 +56,7 @@ public function test_it_can_acquire_lock_with_biggest_lock_id(): void
5656
$dbConnection = $this->initPostgresPdoConnection();
5757
$postgresLockId = new PostgresLockId(self::DB_INT64_VALUE_MAX);
5858

59-
$isLockAcquired = $locker->acquireLock($dbConnection, $postgresLockId);
59+
$isLockAcquired = $locker->tryAcquireLock($dbConnection, $postgresLockId);
6060

6161
$this->assertTrue($isLockAcquired);
6262
$this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId);
@@ -69,8 +69,8 @@ public function test_it_can_acquire_lock_in_same_connection_only_once(): void
6969
$dbConnection = $this->initPostgresPdoConnection();
7070
$postgresLockId = $this->initPostgresLockId('test');
7171

72-
$isLockAcquired1 = $locker->acquireLock($dbConnection, $postgresLockId);
73-
$isLockAcquired2 = $locker->acquireLock($dbConnection, $postgresLockId);
72+
$isLockAcquired1 = $locker->tryAcquireLock($dbConnection, $postgresLockId);
73+
$isLockAcquired2 = $locker->tryAcquireLock($dbConnection, $postgresLockId);
7474

7575
$this->assertTrue($isLockAcquired1);
7676
$this->assertTrue($isLockAcquired2);
@@ -85,8 +85,8 @@ public function test_it_can_acquire_multiple_locks_in_one_connection(): void
8585
$postgresLockId1 = $this->initPostgresLockId('test1');
8686
$postgresLockId2 = $this->initPostgresLockId('test2');
8787

88-
$isLock1Acquired = $locker->acquireLock($dbConnection, $postgresLockId1);
89-
$isLock2Acquired = $locker->acquireLock($dbConnection, $postgresLockId2);
88+
$isLock1Acquired = $locker->tryAcquireLock($dbConnection, $postgresLockId1);
89+
$isLock2Acquired = $locker->tryAcquireLock($dbConnection, $postgresLockId2);
9090

9191
$this->assertTrue($isLock1Acquired);
9292
$this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId1);
@@ -101,9 +101,9 @@ public function test_it_cannot_acquire_same_lock_in_two_connections(): void
101101
$dbConnection1 = $this->initPostgresPdoConnection();
102102
$dbConnection2 = $this->initPostgresPdoConnection();
103103
$postgresLockId = $this->initPostgresLockId('test');
104-
$locker->acquireLock($dbConnection1, $postgresLockId);
104+
$locker->tryAcquireLock($dbConnection1, $postgresLockId);
105105

106-
$isLockAcquired = $locker->acquireLock($dbConnection2, $postgresLockId);
106+
$isLockAcquired = $locker->tryAcquireLock($dbConnection2, $postgresLockId);
107107

108108
$this->assertFalse($isLockAcquired);
109109
$this->assertPgAdvisoryLocksCount(1);
@@ -115,7 +115,7 @@ public function test_it_can_release_lock(): void
115115
$locker = $this->initLocker();
116116
$dbConnection = $this->initPostgresPdoConnection();
117117
$postgresLockId = $this->initPostgresLockId('test');
118-
$locker->acquireLock($dbConnection, $postgresLockId);
118+
$locker->tryAcquireLock($dbConnection, $postgresLockId);
119119

120120
$isLockReleased = $locker->releaseLock($dbConnection, $postgresLockId);
121121

@@ -128,8 +128,8 @@ public function test_it_can_release_lock_twice_if_acquired_twice(): void
128128
$locker = $this->initLocker();
129129
$dbConnection = $this->initPostgresPdoConnection();
130130
$postgresLockId = $this->initPostgresLockId('test');
131-
$locker->acquireLock($dbConnection, $postgresLockId);
132-
$locker->acquireLock($dbConnection, $postgresLockId);
131+
$locker->tryAcquireLock($dbConnection, $postgresLockId);
132+
$locker->tryAcquireLock($dbConnection, $postgresLockId);
133133

134134
$isLockReleased1 = $locker->releaseLock($dbConnection, $postgresLockId);
135135
$isLockReleased2 = $locker->releaseLock($dbConnection, $postgresLockId);
@@ -145,10 +145,10 @@ public function test_it_can_acquire_lock_in_second_connection_after_release(): v
145145
$dbConnection1 = $this->initPostgresPdoConnection();
146146
$dbConnection2 = $this->initPostgresPdoConnection();
147147
$postgresLockId = $this->initPostgresLockId('test');
148-
$locker->acquireLock($dbConnection1, $postgresLockId);
148+
$locker->tryAcquireLock($dbConnection1, $postgresLockId);
149149
$locker->releaseLock($dbConnection1, $postgresLockId);
150150

151-
$isLockAcquired = $locker->acquireLock($dbConnection2, $postgresLockId);
151+
$isLockAcquired = $locker->tryAcquireLock($dbConnection2, $postgresLockId);
152152

153153
$this->assertTrue($isLockAcquired);
154154
$this->assertPgAdvisoryLockExistsInConnection($dbConnection2, $postgresLockId);
@@ -161,11 +161,11 @@ public function test_it_cannot_acquire_lock_in_second_connection_after_one_relea
161161
$dbConnection1 = $this->initPostgresPdoConnection();
162162
$dbConnection2 = $this->initPostgresPdoConnection();
163163
$postgresLockId = $this->initPostgresLockId('test');
164-
$locker->acquireLock($dbConnection1, $postgresLockId);
165-
$locker->acquireLock($dbConnection1, $postgresLockId);
164+
$locker->tryAcquireLock($dbConnection1, $postgresLockId);
165+
$locker->tryAcquireLock($dbConnection1, $postgresLockId);
166166

167167
$isLockReleased = $locker->releaseLock($dbConnection1, $postgresLockId);
168-
$isLockAcquired = $locker->acquireLock($dbConnection2, $postgresLockId);
168+
$isLockAcquired = $locker->tryAcquireLock($dbConnection2, $postgresLockId);
169169

170170
$this->assertTrue($isLockReleased);
171171
$this->assertFalse($isLockAcquired);
@@ -192,7 +192,7 @@ public function test_it_cannot_release_lock_if_acquired_in_other_connection(): v
192192
$dbConnection1 = $this->initPostgresPdoConnection();
193193
$dbConnection2 = $this->initPostgresPdoConnection();
194194
$postgresLockId = $this->initPostgresLockId('test');
195-
$locker->acquireLock($dbConnection1, $postgresLockId);
195+
$locker->tryAcquireLock($dbConnection1, $postgresLockId);
196196

197197
$isLockReleased = $locker->releaseLock($dbConnection2, $postgresLockId);
198198

@@ -207,8 +207,8 @@ public function test_it_can_release_all_locks_in_connection(): void
207207
$dbConnection = $this->initPostgresPdoConnection();
208208
$postgresLockId1 = $this->initPostgresLockId('test');
209209
$postgresLockId2 = $this->initPostgresLockId('test2');
210-
$locker->acquireLock($dbConnection, $postgresLockId1);
211-
$locker->acquireLock($dbConnection, $postgresLockId2);
210+
$locker->tryAcquireLock($dbConnection, $postgresLockId1);
211+
$locker->tryAcquireLock($dbConnection, $postgresLockId2);
212212

213213
$locker->releaseAllLocks($dbConnection);
214214

@@ -234,10 +234,10 @@ public function test_it_can_release_all_locks_in_connection_but_keeps_other_lock
234234
$postgresLockId2 = $this->initPostgresLockId('test2');
235235
$postgresLockId3 = $this->initPostgresLockId('test3');
236236
$postgresLockId4 = $this->initPostgresLockId('test4');
237-
$locker->acquireLock($dbConnection1, $postgresLockId1);
238-
$locker->acquireLock($dbConnection1, $postgresLockId2);
239-
$locker->acquireLock($dbConnection2, $postgresLockId3);
240-
$locker->acquireLock($dbConnection2, $postgresLockId4);
237+
$locker->tryAcquireLock($dbConnection1, $postgresLockId1);
238+
$locker->tryAcquireLock($dbConnection1, $postgresLockId2);
239+
$locker->tryAcquireLock($dbConnection2, $postgresLockId3);
240+
$locker->tryAcquireLock($dbConnection2, $postgresLockId4);
241241

242242
$locker->releaseAllLocks($dbConnection1);
243243

@@ -253,7 +253,7 @@ public function test_it_can_acquire_lock_within_transaction(): void
253253
$postgresLockId = $this->initPostgresLockId('test');
254254
$dbConnection->beginTransaction();
255255

256-
$isLockAcquired = $locker->acquireLockWithinTransaction($dbConnection, $postgresLockId);
256+
$isLockAcquired = $locker->tryAcquireLockWithinTransaction($dbConnection, $postgresLockId);
257257

258258
$this->assertTrue($isLockAcquired);
259259
$this->assertPgAdvisoryLocksCount(1);
@@ -271,7 +271,7 @@ public function test_it_cannot_acquire_lock_within_transaction_not_in_transactio
271271
$dbConnection = $this->initPostgresPdoConnection();
272272
$postgresLockId = $this->initPostgresLockId('test');
273273

274-
$locker->acquireLockWithinTransaction($dbConnection, $postgresLockId);
274+
$locker->tryAcquireLockWithinTransaction($dbConnection, $postgresLockId);
275275
}
276276

277277
public function test_it_cannot_acquire_lock_in_second_connection_if_taken_within_transaction(): void
@@ -281,9 +281,9 @@ public function test_it_cannot_acquire_lock_in_second_connection_if_taken_within
281281
$dbConnection2 = $this->initPostgresPdoConnection();
282282
$postgresLockId = $this->initPostgresLockId('test');
283283
$dbConnection1->beginTransaction();
284-
$locker->acquireLockWithinTransaction($dbConnection1, $postgresLockId);
284+
$locker->tryAcquireLockWithinTransaction($dbConnection1, $postgresLockId);
285285

286-
$isLockAcquired = $locker->acquireLock($dbConnection2, $postgresLockId);
286+
$isLockAcquired = $locker->tryAcquireLock($dbConnection2, $postgresLockId);
287287

288288
$this->assertFalse($isLockAcquired);
289289
$this->assertPgAdvisoryLocksCount(1);
@@ -296,7 +296,7 @@ public function test_it_can_auto_release_lock_acquired_within_transaction_on_com
296296
$dbConnection = $this->initPostgresPdoConnection();
297297
$postgresLockId = $this->initPostgresLockId('test');
298298
$dbConnection->beginTransaction();
299-
$locker->acquireLockWithinTransaction($dbConnection, $postgresLockId);
299+
$locker->tryAcquireLockWithinTransaction($dbConnection, $postgresLockId);
300300

301301
$dbConnection->commit();
302302

@@ -310,7 +310,7 @@ public function test_it_can_auto_release_lock_acquired_within_transaction_on_rol
310310
$dbConnection = $this->initPostgresPdoConnection();
311311
$postgresLockId = $this->initPostgresLockId('test');
312312
$dbConnection->beginTransaction();
313-
$locker->acquireLockWithinTransaction($dbConnection, $postgresLockId);
313+
$locker->tryAcquireLockWithinTransaction($dbConnection, $postgresLockId);
314314

315315
$dbConnection->rollBack();
316316

@@ -324,7 +324,7 @@ public function test_it_can_auto_release_lock_acquired_within_transaction_on_con
324324
$dbConnection = $this->initPostgresPdoConnection();
325325
$postgresLockId = $this->initPostgresLockId('test');
326326
$dbConnection->beginTransaction();
327-
$locker->acquireLockWithinTransaction($dbConnection, $postgresLockId);
327+
$locker->tryAcquireLockWithinTransaction($dbConnection, $postgresLockId);
328328

329329
$dbConnection = null;
330330

@@ -337,7 +337,7 @@ public function test_it_cannot_release_lock_acquired_within_transaction(): void
337337
$dbConnection = $this->initPostgresPdoConnection();
338338
$postgresLockId = $this->initPostgresLockId('test');
339339
$dbConnection->beginTransaction();
340-
$locker->acquireLockWithinTransaction($dbConnection, $postgresLockId);
340+
$locker->tryAcquireLockWithinTransaction($dbConnection, $postgresLockId);
341341

342342
$isLockReleased = $locker->releaseLock($dbConnection, $postgresLockId);
343343

@@ -352,9 +352,9 @@ public function test_it_cannot_release_all_locks_acquired_within_transaction():
352352
$dbConnection = $this->initPostgresPdoConnection();
353353
$postgresLockId1 = $this->initPostgresLockId('test');
354354
$postgresLockId2 = $this->initPostgresLockId('test2');
355-
$locker->acquireLock($dbConnection, $postgresLockId1);
355+
$locker->tryAcquireLock($dbConnection, $postgresLockId1);
356356
$dbConnection->beginTransaction();
357-
$locker->acquireLockWithinTransaction($dbConnection, $postgresLockId2);
357+
$locker->tryAcquireLockWithinTransaction($dbConnection, $postgresLockId2);
358358

359359
$locker->releaseAllLocks($dbConnection);
360360

0 commit comments

Comments
 (0)