diff --git a/CHANGELOG.md b/CHANGELOG.md index 23bbba5..9ae088b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ - Enh #299: Add `ColumnDefinitionParser` class (@Tigrov) - Enh #299: Convert database types to lower case (@Tigrov) - Enh #300: Replace `DbArrayHelper::getColumn()` with `array_column()` (@Tigrov) +- New #301: Add `IndexType` class (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/src/IndexType.php b/src/IndexType.php new file mode 100644 index 0000000..88d12b1 --- /dev/null +++ b/src/IndexType.php @@ -0,0 +1,28 @@ + $index) { $columnNames = array_column($index, 'column_name'); - if ($columnNames[0] === null) { - $columnNames[0] = ''; + if ($columnNames === [null]) { + $columnNames = []; } $result[] = (new IndexConstraint()) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 89a1374..76b4011 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Oracle\Tests; +use PHPUnit\Framework\Attributes\DataProviderExternal; use ReflectionException; use Throwable; use Yiisoft\Db\Constant\ColumnType; @@ -13,9 +14,12 @@ use Yiisoft\Db\Exception\InvalidCallException; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; +use Yiisoft\Db\Oracle\Column\ColumnBuilder; use Yiisoft\Db\Oracle\Connection; use Yiisoft\Db\Oracle\Dsn; use Yiisoft\Db\Oracle\Driver; +use Yiisoft\Db\Oracle\IndexType; +use Yiisoft\Db\Oracle\Tests\Provider\CommandProvider; use Yiisoft\Db\Oracle\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Tests\Common\CommonCommandTest; @@ -26,6 +30,7 @@ use function is_resource; use function str_pad; use function stream_get_contents; +use function version_compare; /** * @group oracle @@ -636,4 +641,47 @@ public function testShowDatabases(): void $this->assertSame('oci:dbname=localhost:1521', $db->getDriver()->getDsn()); $this->assertSame(['YIITEST'], $command->showDatabases()); } + + #[DataProviderExternal(CommandProvider::class, 'createIndex')] + public function testCreateIndex(array $columns, array $indexColumns, string|null $indexType, string|null $indexMethod): void + { + parent::testCreateIndex($columns, $indexColumns, $indexType, $indexMethod); + } + + public function testCreateSearchIndex() + { + $db = $this->getConnection(); + + if (version_compare($db->getServerInfo()->getVersion(), '21', '<')) { + $this->markTestSkipped('Search index is supported since Oracle 21'); + } + + $command = $db->createCommand(); + $schema = $db->getSchema(); + + $tableName = 'test_create_index'; + $indexName = 'test_index_name'; + + if ($schema->getTableSchema($tableName) !== null) { + $command->dropTable($tableName)->execute(); + } + + $command->createTable($tableName, ['col1' => ColumnBuilder::text()])->execute(); + $command->createIndex($tableName, $indexName, ['col1'], IndexType::SEARCH)->execute(); + + $this->assertCount(2, $schema->getTableIndexes($tableName)); + + $index = $schema->getTableIndexes($tableName)[0]; + + $this->assertSame(['col1'], $index->getColumnNames()); + $this->assertFalse($index->isUnique()); + $this->assertFalse($index->isPrimary()); + + $sysIndex = $schema->getTableIndexes($tableName)[1]; + $this->assertSame([], $sysIndex->getColumnNames()); + $this->assertTrue($sysIndex->isUnique()); + $this->assertFalse($sysIndex->isPrimary()); + + $db->close(); + } } diff --git a/tests/Provider/CommandProvider.php b/tests/Provider/CommandProvider.php index 248f854..a5685c1 100644 --- a/tests/Provider/CommandProvider.php +++ b/tests/Provider/CommandProvider.php @@ -7,6 +7,8 @@ use JsonException; use PDO; use Yiisoft\Db\Command\Param; +use Yiisoft\Db\Oracle\Column\ColumnBuilder; +use Yiisoft\Db\Oracle\IndexType; use Yiisoft\Db\Oracle\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Support\DbHelper; @@ -92,4 +94,13 @@ public static function insertVarbinary(): array ['simple string', 'simple string'], ]; } + + public static function createIndex(): array + { + return [ + ...parent::createIndex(), + [['col1' => ColumnBuilder::integer()], ['col1'], IndexType::UNIQUE, null], + [['col1' => ColumnBuilder::integer()], ['col1'], IndexType::BITMAP, null], + ]; + } }