From 80bfc7678453ff00c19ba2ab2becde0eed9d99eb Mon Sep 17 00:00:00 2001 From: Anton Komarev Date: Fri, 25 Jul 2025 13:19:23 +0300 Subject: [PATCH 1/3] Code style --- .gitattributes | 11 ++--- docker-compose.yml => compose.yml | 0 src/LockId/LockId.php | 5 --- src/LockId/PostgresLockId.php | 6 +-- src/Locker/PostgresAdvisoryLocker.php | 30 ++++++------- .../AbstractIntegrationTestCase.php | 42 +++++++++---------- .../Locker/PostgresAdvisoryLockerTest.php | 2 +- test/Unit/LockId/LockIdTest.php | 25 ++++++++++- 8 files changed, 69 insertions(+), 52 deletions(-) rename docker-compose.yml => compose.yml (100%) diff --git a/.gitattributes b/.gitattributes index 52f4b01..07ddab3 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,11 +1,12 @@ * text=auto -/.docker export-ignore -/.github export-ignore -/test export-ignore +/.docker/ export-ignore +/.github/ export-ignore +/test/ export-ignore + /.env export-ignore /.gitattributes export-ignore /.gitignore export-ignore -/CODE_OF_CONDUCT.md export-ignore -/CONTRIBUTING.md export-ignore +/compose.yml export-ignore +/Makefile export-ignore /phpunit.xml.dist export-ignore diff --git a/docker-compose.yml b/compose.yml similarity index 100% rename from docker-compose.yml rename to compose.yml diff --git a/src/LockId/LockId.php b/src/LockId/LockId.php index 03e2421..6f51925 100644 --- a/src/LockId/LockId.php +++ b/src/LockId/LockId.php @@ -27,11 +27,6 @@ public function __construct( } public function __toString(): string - { - return $this->compileId(); - } - - private function compileId(): string { return $this->value !== '' ? $this->key . ':' . $this->value diff --git a/src/LockId/PostgresLockId.php b/src/LockId/PostgresLockId.php index 9a0ab99..090ceb9 100644 --- a/src/LockId/PostgresLockId.php +++ b/src/LockId/PostgresLockId.php @@ -35,11 +35,11 @@ public function __construct( public static function fromLockId( LockId $lockId, ): self { - $humanReadableValue = (string)$lockId; + $lockStringId = (string)$lockId; return new self( - self::generateIdFromString($humanReadableValue), - $humanReadableValue + id: self::generateIdFromString($lockStringId), + humanReadableValue: $lockStringId, ); } diff --git a/src/Locker/PostgresAdvisoryLocker.php b/src/Locker/PostgresAdvisoryLocker.php index 74f4790..d57fab3 100644 --- a/src/Locker/PostgresAdvisoryLocker.php +++ b/src/Locker/PostgresAdvisoryLocker.php @@ -23,16 +23,16 @@ public function tryAcquireLock( PDO $dbConnection, PostgresLockId $postgresLockId, ): bool { - // TODO: Need to cleanup humanReadableValue? + // TODO: Need to sanitize humanReadableValue? $statement = $dbConnection->prepare( <<humanReadableValue - SQL + SELECT PG_TRY_ADVISORY_LOCK(:lock_id); -- $postgresLockId->humanReadableValue + SQL, ); $statement->execute( [ 'lock_id' => $postgresLockId->id, - ] + ], ); return $statement->fetchColumn(0); @@ -46,20 +46,20 @@ public function tryAcquireLockWithinTransaction( $lockId = $postgresLockId->humanReadableValue; throw new LogicException( - "Transaction-level advisory lock `$lockId` cannot be acquired outside of transaction" + "Transaction-level advisory lock `$lockId` cannot be acquired outside of transaction", ); } - // TODO: Need to cleanup humanReadableValue? + // TODO: Need to sanitize humanReadableValue? $statement = $dbConnection->prepare( <<humanReadableValue - SQL + SELECT PG_ADVISORY_XACT_LOCK(:lock_id); -- $postgresLockId->humanReadableValue + SQL, ); $statement->execute( [ 'lock_id' => $postgresLockId->id, - ] + ], ); return $statement->fetchColumn(0); @@ -70,14 +70,14 @@ public function releaseLock( PostgresLockId $postgresLockId, ): bool { $statement = $dbConnection->prepare( - <<<'SQL' - SELECT pg_advisory_unlock(:lock_id); - SQL + <<humanReadableValue + SQL, ); $statement->execute( [ 'lock_id' => $postgresLockId->id, - ] + ], ); return $statement->fetchColumn(0); @@ -88,8 +88,8 @@ public function releaseAllLocks( ): void { $statement = $dbConnection->prepare( <<<'SQL' - SELECT pg_advisory_unlock_all(); - SQL + SELECT PG_ADVISORY_UNLOCK_ALL(); + SQL, ); $statement->execute(); } diff --git a/test/Integration/AbstractIntegrationTestCase.php b/test/Integration/AbstractIntegrationTestCase.php index f714465..ca48e8d 100644 --- a/test/Integration/AbstractIntegrationTestCase.php +++ b/test/Integration/AbstractIntegrationTestCase.php @@ -56,7 +56,7 @@ protected function assertPgAdvisoryLockExistsInConnection( $this->assertTrue( $row !== null, - "Lock id `$lockIdString` does not exists" + "Lock id `$lockIdString` does not exists", ); } @@ -70,7 +70,7 @@ protected function assertPgAdvisoryLockMissingInConnection( $this->assertTrue( $row === null, - "Lock id `$lockIdString` is present" + "Lock id `$lockIdString` is present", ); } @@ -83,7 +83,7 @@ protected function assertPgAdvisoryLocksCount( $this->assertSame( $expectedCount, $rowsCount, - "Failed asserting that advisory locks actual count $rowsCount matches expected count $expectedCount." + "Failed asserting that advisory locks actual count $rowsCount matches expected count $expectedCount.", ); } @@ -98,14 +98,14 @@ private function findPostgresAdvisoryLockInConnection( $statement = $dbConnection->prepare( <<<'SQL' - SELECT * - FROM pg_locks - WHERE locktype = 'advisory' - AND classid = :lock_catalog_id - AND objid = :lock_object_id - AND pid = :connection_pid - AND mode = :mode - SQL + SELECT * + FROM pg_locks + WHERE locktype = 'advisory' + AND classid = :lock_catalog_id + AND objid = :lock_object_id + AND pid = :connection_pid + AND mode = :mode + SQL, ); $statement->execute( [ @@ -113,7 +113,7 @@ private function findPostgresAdvisoryLockInConnection( 'lock_object_id' => $lockObjectId, 'connection_pid' => $dbConnection->pgsqlGetPid(), 'mode' => self::MODE_EXCLUSIVE, - ] + ], ); $result = $statement->fetchObject(); @@ -131,16 +131,16 @@ private function findAllPostgresAdvisoryLocks(): array $statement = $dbConnection->prepare( <<<'SQL' - SELECT * - FROM pg_locks - WHERE locktype = 'advisory' - AND mode = :mode - SQL + SELECT * + FROM pg_locks + WHERE locktype = 'advisory' + AND mode = :mode + SQL, ); $statement->execute( [ 'mode' => self::MODE_EXCLUSIVE, - ] + ], ); return $statement->fetchAll(PDO::FETCH_OBJ); @@ -150,10 +150,10 @@ private function closeAllPostgresPdoConnections(): void { $this->initPostgresPdoConnection()->query( <<<'SQL' - SELECT pg_terminate_backend(pid) + SELECT PG_TERMINATE_BACKEND(pid) FROM pg_stat_activity - WHERE pid <> pg_backend_pid() - SQL + WHERE pid <> PG_BACKEND_PID() + SQL, ); } } diff --git a/test/Integration/Locker/PostgresAdvisoryLockerTest.php b/test/Integration/Locker/PostgresAdvisoryLockerTest.php index 50e9c20..f7628c1 100644 --- a/test/Integration/Locker/PostgresAdvisoryLockerTest.php +++ b/test/Integration/Locker/PostgresAdvisoryLockerTest.php @@ -264,7 +264,7 @@ public function test_it_cannot_acquire_lock_within_transaction_not_in_transactio { $this->expectException(LogicException::class); $this->expectExceptionMessage( - "Transaction-level advisory lock `test` cannot be acquired outside of transaction" + 'Transaction-level advisory lock `test` cannot be acquired outside of transaction', ); $locker = $this->initLocker(); diff --git a/test/Unit/LockId/LockIdTest.php b/test/Unit/LockId/LockIdTest.php index 57529cf..dfc028b 100644 --- a/test/Unit/LockId/LockIdTest.php +++ b/test/Unit/LockId/LockIdTest.php @@ -14,10 +14,10 @@ namespace Cog\Test\DbLocker\Unit\LockId; use Cog\DbLocker\LockId\LockId; +use Cog\Test\DbLocker\Unit\AbstractUnitTestCase; use InvalidArgumentException; -use PHPUnit\Framework\TestCase; -final class LockIdTest extends TestCase +final class LockIdTest extends AbstractUnitTestCase { public function test_it_can_create_lock_id(): void { @@ -26,6 +26,20 @@ public function test_it_can_create_lock_id(): void $this->assertSame('test', (string)$lockId); } + public function test_it_can_create_lock_id_with_space_key(): void + { + $lockId = new LockId(' '); + + $this->assertSame(' ', (string)$lockId); + } + + public function test_it_can_create_lock_id_with_spaced_key(): void + { + $lockId = new LockId(' test '); + + $this->assertSame(' test ', (string)$lockId); + } + public function test_it_can_create_lock_id_with_value(): void { $lockId = new LockId('test', '1'); @@ -33,6 +47,13 @@ public function test_it_can_create_lock_id_with_value(): void $this->assertSame('test:1', (string)$lockId); } + public function test_it_can_create_lock_id_with_value_and_spaced_key(): void + { + $lockId = new LockId(' test ', '1'); + + $this->assertSame(' test :1', (string)$lockId); + } + public function test_it_cannot_create_lock_id_with_empty_key(): void { $this->expectException(InvalidArgumentException::class); From f6c24af8b7c1e26cd833e7f835e6c1ef686adc90 Mon Sep 17 00:00:00 2001 From: Anton Komarev Date: Fri, 25 Jul 2025 17:20:03 +0300 Subject: [PATCH 2/3] Code style --- .github/workflows/php.yml | 2 +- Makefile | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index c711907..c27dd19 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -11,7 +11,7 @@ jobs: uses: actions/checkout@v2 - name: Docker compose up - run: docker-compose up -d + run: docker compose up -d - name: Validate composer.json and composer.lock run: composer validate diff --git a/Makefile b/Makefile index fd76b44..0852bcf 100644 --- a/Makefile +++ b/Makefile @@ -8,10 +8,10 @@ setup-dev: ## Setup project for development make composer-install start: ## Start application silently - docker-compose up -d + docker compose up -d stop: ## Stop application - docker-compose down + docker compose down restart: ## Restart the application make stop From b86806e1070846aa9a99357b7d7744cc5a23ba97 Mon Sep 17 00:00:00 2001 From: Anton Komarev Date: Fri, 25 Jul 2025 17:20:53 +0300 Subject: [PATCH 3/3] Code style --- src/Locker/PostgresAdvisoryLocker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Locker/PostgresAdvisoryLocker.php b/src/Locker/PostgresAdvisoryLocker.php index d57fab3..1062109 100644 --- a/src/Locker/PostgresAdvisoryLocker.php +++ b/src/Locker/PostgresAdvisoryLocker.php @@ -53,7 +53,7 @@ public function tryAcquireLockWithinTransaction( // TODO: Need to sanitize humanReadableValue? $statement = $dbConnection->prepare( <<humanReadableValue + SELECT PG_TRY_ADVISORY_XACT_LOCK(:lock_id); -- $postgresLockId->humanReadableValue SQL, ); $statement->execute(