Skip to content

Commit 5c3508e

Browse files
Merge pull request #73 from utopia-php/feat-get-limit-methods-on-database
feat: get limit methods on database
2 parents 4617340 + 1469661 commit 5c3508e

4 files changed

Lines changed: 41 additions & 27 deletions

File tree

src/Database/Database.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,4 +1176,27 @@ public function getId(): string
11761176
{
11771177
return \uniqid();
11781178
}
1179+
1180+
/**
1181+
* Get adapter attribute limit, accounting for internal metadata
1182+
* Returns 0 to indicate no limit
1183+
*
1184+
* @return int
1185+
*/
1186+
public function getAttributeLimit()
1187+
{
1188+
// If negative, return 0
1189+
// -1 ==> virtual columns count as total, so treat as buffer
1190+
return \max($this->adapter->getAttributeLimit() - $this->adapter->getNumberOfDefaultAttributes() - 1, 0);
1191+
}
1192+
1193+
/**
1194+
* Get adapter index limit
1195+
*
1196+
* @return int
1197+
*/
1198+
public function getIndexLimit()
1199+
{
1200+
return $this->adapter->getIndexLimit() - $this->adapter->getNumberOfDefaultIndexes();
1201+
}
11791202
}

tests/Database/Adapter/MariaDBTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ static function getAdapterRowLimit(): int
3939
return MariaDB::getRowLimit();
4040
}
4141

42-
/**
43-
*
44-
* @return int
45-
*/
46-
static function getUsedIndexes(): int
47-
{
48-
return MariaDB::getNumberOfDefaultIndexes();
49-
}
50-
5142
/**
5243
* @return Adapter
5344
*/

tests/Database/Adapter/MongoDBTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ static function getAdapterRowLimit(): int
3939
return MongoDB::getRowLimit();
4040
}
4141

42-
/**
43-
*
44-
* @return int
45-
*/
46-
static function getUsedIndexes(): int
47-
{
48-
return MongoDB::getNumberOfDefaultIndexes();
49-
}
50-
5142
/**
5243
* @return Adapter
5344
*/

tests/Database/Base.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ abstract static protected function getAdapterName(): string;
3030
*/
3131
abstract static protected function getAdapterRowLimit(): int;
3232

33-
/**
34-
* @return int
35-
*/
36-
abstract static protected function getUsedIndexes(): int;
37-
3833
public function setUp(): void
3934
{
4035
Authorization::setRole('role:all');
@@ -1270,10 +1265,10 @@ public function testWritePermissionsUpdateFailure(Document $document)
12701265

12711266
public function testExceptionAttributeLimit()
12721267
{
1273-
if (static::getAdapterName() === 'mariadb' || static::getAdapterName() === 'mysql') {
1268+
if ($this->getDatabase()->getAttributeLimit() > 0) {
12741269
// load the collection up to the limit
12751270
$attributes = [];
1276-
for ($i=0; $i < 1012; $i++) {
1271+
for ($i=0; $i < $this->getDatabase()->getAttributeLimit(); $i++) {
12771272
$attributes[] = new Document([
12781273
'$id' => "test{$i}",
12791274
'type' => Database::VAR_INTEGER,
@@ -1300,7 +1295,7 @@ public function testExceptionAttributeLimit()
13001295
*/
13011296
public function testCheckAttributeCountLimit()
13021297
{
1303-
if (static::getAdapterName() === 'mariadb' || static::getAdapterName() === 'mysql') {
1298+
if ($this->getDatabase()->getAttributeLimit() > 0) {
13041299
$collection = static::getDatabase()->getCollection('attributeLimit');
13051300

13061301
// create same attribute in testExceptionAttributeLimit
@@ -1464,7 +1459,7 @@ public function testExceptionIndexLimit()
14641459
// MariaDB, MySQL, and MongoDB create 3 indexes per new collection
14651460
// MongoDB create 4 indexes per new collection
14661461
// Add up to the limit, then check if the next index throws IndexLimitException
1467-
for ($i=0; $i < (64 - static::getUsedIndexes()); $i++) {
1462+
for ($i=0; $i < ($this->getDatabase()->getIndexLimit()); $i++) {
14681463
$this->assertEquals(true, static::getDatabase()->createIndex('indexLimit', "index{$i}", Database::INDEX_KEY, ["test{$i}"], [16]));
14691464
}
14701465
$this->expectException(LimitException::class);
@@ -1543,4 +1538,18 @@ public function testUniqueIndexDuplicateUpdate()
15431538

15441539
static::getDatabase()->updateDocument('movies', $document->getId(), $document->setAttribute('name', 'Frozen'));
15451540
}
1541+
1542+
public function testGetAttributeLimit()
1543+
{
1544+
if (static::getAdapterName() === 'mariadb' || static::getAdapterName() === 'mysql') {
1545+
$this->assertEquals(1012, $this->getDatabase()->getAttributeLimit());
1546+
} else {
1547+
$this->assertEquals(0, $this->getDatabase()->getAttributeLimit());
1548+
}
1549+
}
1550+
1551+
public function testGetIndexLimit()
1552+
{
1553+
$this->assertEquals(61, $this->getDatabase()->getIndexLimit());
1554+
}
15461555
}

0 commit comments

Comments
 (0)