|
10 | 10 | use Utopia\Database\Exception\Duplicate as DuplicateException; |
11 | 11 | use Utopia\Database\Exception\Limit as LimitException; |
12 | 12 | use Utopia\Database\Exception\Query as QueryException; |
| 13 | +use Utopia\Database\Exception\Unique as UniqueException; |
13 | 14 | use Utopia\Database\Helpers\ID; |
14 | 15 | use Utopia\Database\Helpers\Permission; |
15 | 16 | use Utopia\Database\Helpers\Role; |
@@ -511,6 +512,46 @@ public function testCreateIndexOnSequence(): void |
511 | 512 | $database->deleteCollection(__FUNCTION__); |
512 | 513 | } |
513 | 514 |
|
| 515 | + public function testCreateUniqueIndexOverDuplicates(): void |
| 516 | + { |
| 517 | + /** @var Database $database */ |
| 518 | + $database = $this->getDatabase(); |
| 519 | + |
| 520 | + if (!$database->getAdapter()->getSupportForUniqueIndex()) { |
| 521 | + $this->expectNotToPerformAssertions(); |
| 522 | + return; |
| 523 | + } |
| 524 | + |
| 525 | + $database->createCollection(__FUNCTION__); |
| 526 | + $database->createAttribute(__FUNCTION__, 'name', Database::VAR_STRING, 128, false); |
| 527 | + $database->createAttribute(__FUNCTION__, 'age', Database::VAR_INTEGER, 0, false); |
| 528 | + |
| 529 | + foreach (['first', 'second'] as $id) { |
| 530 | + $database->createDocument(__FUNCTION__, new Document([ |
| 531 | + '$id' => $id, |
| 532 | + '$permissions' => [ |
| 533 | + Permission::read(Role::any()), |
| 534 | + ], |
| 535 | + 'name' => 'chester', |
| 536 | + 'age' => 7, |
| 537 | + ])); |
| 538 | + } |
| 539 | + |
| 540 | + foreach (['name', 'age'] as $attribute) { |
| 541 | + try { |
| 542 | + $database->createIndex(__FUNCTION__, "unique_{$attribute}", Database::INDEX_UNIQUE, [$attribute]); |
| 543 | + $this->fail('Failed to throw exception'); |
| 544 | + } catch (Exception $e) { |
| 545 | + $this->assertInstanceOf(UniqueException::class, $e); |
| 546 | + } |
| 547 | + |
| 548 | + $indexes = $database->getCollection(__FUNCTION__)->getAttribute('indexes'); |
| 549 | + $this->assertNotContains("unique_{$attribute}", \array_map(fn ($index) => $index->getId(), $indexes)); |
| 550 | + } |
| 551 | + |
| 552 | + $database->deleteCollection(__FUNCTION__); |
| 553 | + } |
| 554 | + |
514 | 555 | public function testIndexLengthZero(): void |
515 | 556 | { |
516 | 557 | /** @var Database $database */ |
|
0 commit comments