Skip to content

Commit

Permalink
Add IndexType and IndexMethod classes (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Feb 10, 2025
1 parent 8d44f35 commit edefcb3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Enh #371: Remove `ColumnInterface` (@Tigrov)
- Enh #372: Rename `ColumnSchemaInterface` to `ColumnInterface` (@Tigrov)
- Enh #373: Replace `DbArrayHelper::getColumn()` with `array_column()` (@Tigrov)
- New #374: Add `IndexType` and `IndexMethod` classes (@Tigrov)

## 1.2.0 March 21, 2024

Expand Down
20 changes: 20 additions & 0 deletions src/IndexMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mysql;

/**
* Defines the available index methods for {@see DDLQueryBuilder::createIndex()} method.
*/
final class IndexMethod
{
/**
* Define the method of the index as `BTREE`.
*/
public const BTREE = 'BTREE';
/**
* Define the method of the index as `HASH`.
*/
public const HASH = 'HASH';
}
24 changes: 24 additions & 0 deletions src/IndexType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mysql;

/**
* Defines the available index types for {@see DDLQueryBuilder::createIndex()} method.
*/
final class IndexType
{
/**
* Define the type of the index as `UNIQUE`.
*/
public const UNIQUE = 'UNIQUE';
/**
* Define the type of the index as `FULLTEXT`.
*/
public const FULLTEXT = 'FULLTEXT';
/**
* Define the type of the index as `SPATIAL`.
*/
public const SPATIAL = 'SPATIAL';
}
8 changes: 8 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Yiisoft\Db\Mysql\Tests;

use PHPUnit\Framework\Attributes\DataProviderExternal;
use Throwable;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Mysql\Connection;
use Yiisoft\Db\Mysql\Dsn;
use Yiisoft\Db\Mysql\Driver;
use Yiisoft\Db\Mysql\Tests\Provider\CommandProvider;
use Yiisoft\Db\Mysql\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Common\CommonCommandTest;
use Yiisoft\Db\Tests\Support\DbHelper;
Expand Down Expand Up @@ -149,4 +151,10 @@ public function testShowDatabases(): void
$this->assertSame('mysql:host=127.0.0.1;port=3306', $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);
}
}
16 changes: 16 additions & 0 deletions tests/Provider/CommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,23 @@

namespace Yiisoft\Db\Mysql\Tests\Provider;

use Yiisoft\Db\Mysql\Column\ColumnBuilder;
use Yiisoft\Db\Mysql\IndexMethod;
use Yiisoft\Db\Mysql\IndexType;

final class CommandProvider extends \Yiisoft\Db\Tests\Provider\CommandProvider
{
protected static string $driverName = 'mysql';

public static function createIndex(): array
{
return [
...parent::createIndex(),
[['col1' => ColumnBuilder::integer()], ['col1'], IndexType::UNIQUE, null],
[['col1' => ColumnBuilder::text()], ['col1'], IndexType::FULLTEXT, null],
[['col1' => 'point NOT NULL'], ['col1'], IndexType::SPATIAL, null],
[['col1' => ColumnBuilder::integer()], ['col1'], null, IndexMethod::BTREE],
[['col1' => ColumnBuilder::integer()], ['col1'], null, IndexMethod::HASH],
];
}
}

0 comments on commit edefcb3

Please sign in to comment.