Skip to content

Commit 2673a49

Browse files
Matt-Wozavsej
andauthored
PCBC-968 Support for maxTTL value of -1 for collection "no expiry" (#144)
* Add maxTTL check and tests * Update core and package.xml * update core to latest * fix bug with null expiry --------- Co-authored-by: Sergey Avseyev <sergey.avseyev@gmail.com>
1 parent 4f92388 commit 2673a49

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

Couchbase/Management/CreateCollectionSettings.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,29 @@
2020

2121
namespace Couchbase\Management;
2222

23+
use Couchbase\Exception\InvalidArgumentException;
24+
2325
class CreateCollectionSettings
2426
{
2527
private ?int $maxExpiry;
2628
private ?bool $history;
2729

30+
31+
/**
32+
* @throws InvalidArgumentException
33+
*/
2834
public function __construct(int $maxExpiry = null, bool $history = null)
2935
{
36+
if ($maxExpiry && $maxExpiry < -1) {
37+
throw new InvalidArgumentException("Collection max expiry must be greater than or equal to -1.");
38+
}
3039
$this->maxExpiry = $maxExpiry;
3140
$this->history = $history;
3241
}
3342

43+
/**
44+
* @throws InvalidArgumentException
45+
*/
3446
public static function build(int $maxExpiry = null, bool $history = null): CreateCollectionSettings
3547
{
3648
return new CreateCollectionSettings($maxExpiry, $history);
@@ -52,6 +64,12 @@ public function history(): ?bool
5264
return $this->history;
5365
}
5466

67+
/**
68+
* @param CreateCollectionSettings|null $settings
69+
* @return array
70+
*
71+
* @internal
72+
*/
5573
public static function export(?CreateCollectionSettings $settings): array
5674
{
5775
if ($settings == null) {

Couchbase/Management/UpdateCollectionSettings.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,28 @@
2020

2121
namespace Couchbase\Management;
2222

23+
use Couchbase\Exception\InvalidArgumentException;
24+
2325
class UpdateCollectionSettings
2426
{
2527
private ?int $maxExpiry;
2628
private ?bool $history;
2729

30+
/**
31+
* @throws InvalidArgumentException
32+
*/
2833
public function __construct(int $maxExpiry = null, bool $history = null)
2934
{
35+
if ($maxExpiry && $maxExpiry < -1) {
36+
throw new InvalidArgumentException("Collection max expiry must be greater than or equal to -1.");
37+
}
3038
$this->maxExpiry = $maxExpiry;
3139
$this->history = $history;
3240
}
3341

42+
/**
43+
* @throws InvalidArgumentException
44+
*/
3445
public static function build(int $maxExpiry = null, bool $history = null): UpdateCollectionSettings
3546
{
3647
return new UpdateCollectionSettings($maxExpiry, $history);

tests/CollectionManagerTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use Couchbase\Exception\CollectionExistsException;
44
use Couchbase\Exception\CollectionNotFoundException;
55
use Couchbase\Exception\CouchbaseException;
6+
use Couchbase\Exception\FeatureNotAvailableException;
7+
use Couchbase\Exception\InvalidArgumentException;
68
use Couchbase\Exception\ScopeExistsException;
79
use Couchbase\Exception\ScopeNotFoundException;
810
use Couchbase\Management\BucketSettings;
@@ -293,6 +295,57 @@ public function testCollectionHistory(): void
293295
$bucketManager->dropBucket($bucketName);
294296
}
295297

298+
public function testCreateCollectionNoExpiry()
299+
{
300+
$this->skipIfCaves();
301+
$this->skipIfProtostellar();
302+
$this->skipIfUnsupported($this->version()->supportsCollectionMaxTTLNoExpiry());
303+
304+
$collectionName = $this->uniqueId("collection");
305+
$scopeName = $this->uniqueId("scope");
306+
$this->manager->createScope($scopeName);
307+
$this->manager->createCollection($scopeName, $collectionName, CreateCollectionSettings::build(-1, false));
308+
309+
$selectedScope = $this->getScope($scopeName);
310+
311+
$found = false;
312+
foreach ($selectedScope->collections() as $collection) {
313+
if ($collection->name() == $collectionName) {
314+
$found = true;
315+
$foundCollection = $collection;
316+
}
317+
}
318+
$this->assertTrue($found);
319+
320+
$this->assertEquals(-1, $foundCollection->maxExpiry());
321+
}
322+
323+
public function testCreateCollectionNoExpiryNotSupported()
324+
{
325+
$this->skipIfCaves();
326+
$this->skipIfProtostellar();
327+
$this->skipIfUnsupported(!$this->version()->supportsCollectionMaxTTLNoExpiry());
328+
329+
$collectionName = $this->uniqueId("collection");
330+
$scopeName = $this->uniqueId("scope");
331+
$this->manager->createScope($scopeName);
332+
333+
$this->expectException(FeatureNotAvailableException::class);
334+
$this->manager->createCollection($scopeName, $collectionName, CreateCollectionSettings::build(-1, false));
335+
}
336+
337+
public function testCreateCollectionInvalidExpiry()
338+
{
339+
$this->expectException(InvalidArgumentException::class);
340+
CreateCollectionSettings::build(-5, false);
341+
}
342+
343+
public function testCreateCollectionZeroExpiry(): void
344+
{
345+
$settings = CreateCollectionSettings::build(0);
346+
$this->assertNotNull($settings);
347+
}
348+
296349
/**
297350
* @throws ScopeNotFoundException
298351
*/

tests/Helpers/ServerVersion.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ public function supportsDocNotLockedException(): bool
279279
return ($this->major == 7 && $this->minor >= 6) || $this->major > 7;
280280
}
281281

282+
public function supportsCollectionMaxTTLNoExpiry(): bool
283+
{
284+
return ($this->major == 7 && $this->minor >= 6) || $this->major > 7;
285+
}
286+
282287
/**
283288
* @return int
284289
*/

0 commit comments

Comments
 (0)