From 659e366c11d8ee7842ca24a3d0af3032be041491 Mon Sep 17 00:00:00 2001 From: Thiery Laverdure Date: Tue, 11 Nov 2025 07:43:55 -0500 Subject: [PATCH] Replace uniqid with uuid for query IDs Switched from using PHP's uniqid() to ramsey/uuid for generating unique query IDs in LitebaseClient. Updated composer.json to require ramsey/uuid and adjusted tests to use UUIDs for query IDs, improving uniqueness and standards compliance. --- composer.json | 3 ++- src/LitebaseClient.php | 11 ++++++----- tests/Unit/HttpTransportTest.php | 3 ++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 4a8652d..cec5398 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,8 @@ "ext-curl": "*", "ext-json": "*", "guzzlehttp/guzzle": "^7.9", - "guzzlehttp/psr7": "^1.7 || ^2.0" + "guzzlehttp/psr7": "^1.7 || ^2.0", + "ramsey/uuid": "^4.9" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.5", diff --git a/src/LitebaseClient.php b/src/LitebaseClient.php index 4e0c9c2..23260b4 100644 --- a/src/LitebaseClient.php +++ b/src/LitebaseClient.php @@ -5,6 +5,7 @@ use Exception; use GuzzleHttp\Client; use Litebase\OpenAPI\Model\StatementParameter; +use Ramsey\Uuid\Uuid; use Throwable; class LitebaseClient @@ -61,7 +62,7 @@ public function beginTransaction(): bool try { $response = $this->transport->send( new Query( - id: uniqid(), + id: Uuid::uuid4()->toString(), statement: 'BEGIN', ) ); @@ -100,7 +101,7 @@ public function commit(): bool try { $this->transport->send( new Query( - id: uniqid(), + id: Uuid::uuid4()->toString(), transactionId: $this->transaction->id, statement: 'COMMIT', ) @@ -141,7 +142,7 @@ public function errorInfo(): array public function exec(array $input): ?QueryResult { // Set a unique id for the request. - $input['id'] = uniqid(); + $input['id'] = Uuid::uuid4()->toString(); if ($this->transaction) { $input['transaction_id'] = $this->transaction->id; @@ -198,7 +199,7 @@ public function rollback(): bool $this->transport->send( new Query( - id: uniqid(), + id: Uuid::uuid4()->toString(), transactionId: $this->transaction->id, statement: 'ROLLBACK', ) @@ -219,7 +220,7 @@ public function withTransport(string $transportType): LitebaseClient $this->transport = new HttpStreamingTransport($this->configuration); break; default: - throw new Exception('Invalid transport type: '.$transportType); + throw new Exception('Invalid transport type: ' . $transportType); } return $this; diff --git a/tests/Unit/HttpTransportTest.php b/tests/Unit/HttpTransportTest.php index b834472..479867e 100644 --- a/tests/Unit/HttpTransportTest.php +++ b/tests/Unit/HttpTransportTest.php @@ -11,6 +11,7 @@ use Litebase\HttpTransport; use Litebase\Query; use Litebase\QueryResult; +use Ramsey\Uuid\Uuid; describe('HttpTransport', function () { $mock = new MockHandler; @@ -65,7 +66,7 @@ $transport->setClient(new ApiClient($configuration, $httpClient)); $query = new Query( - id: 'test-query', + id: Uuid::uuid4()->toString(), statement: 'SELECT 1', parameters: [], );