-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add searchindex helper and console command (#82)
* add searchindex helper and console command * add postgres support for searchindex optimization and unit test * skip searchindex tests depending on db driver
- Loading branch information
Showing
7 changed files
with
331 additions
and
183 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
layout: default | ||
title: Helpers | ||
nav_order: 6 | ||
--- | ||
|
||
# Helpers | ||
|
||
## Search Index | ||
We include a helper for common tasks related to Craft's searchindex table which can become unweildy when large amounts of data are in the system. To access the helper from anywhere in the application you can grab it with `use viget\base\helpers\SearchIndex;` | ||
|
||
For example you can optimize that table: | ||
```php | ||
use viget\base\helpers\SearchIndex; | ||
|
||
... | ||
|
||
try { | ||
SearchIndex::optimize(); | ||
} catch (\Exception $e) { | ||
// Handle the error | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
layout: default | ||
title: Testing | ||
nav_order: 6 | ||
nav_order: 7 | ||
--- | ||
|
||
# Testing | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace viget\base\console\controllers; | ||
|
||
use craft\console\Controller; | ||
use viget\base\helpers\SearchIndex; | ||
use yii\console\ExitCode; | ||
|
||
class SearchIndexController extends Controller | ||
{ | ||
/** | ||
* Optimizes Craft's searchindex table (can take a long time) | ||
* @return int | ||
*/ | ||
public function actionOptimize(): int | ||
{ | ||
$tableName = SearchIndex::$tableName; | ||
|
||
try { | ||
SearchIndex::optimize(); | ||
$this->stdout("Table {$tableName} optimized successfully.\n"); | ||
return ExitCode::OK; | ||
} catch (\Exception $e) { | ||
$this->stderr("Failed to optimize table {$tableName}: {$e->getMessage()}\n"); | ||
return ExitCode::UNSPECIFIED_ERROR; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace viget\base\helpers; | ||
|
||
use Craft; | ||
use craft\db\Table; | ||
|
||
class SearchIndex | ||
{ | ||
public static $tableName = Table::SEARCHINDEX; | ||
|
||
/** | ||
* Optimizes Craft's searchindex table (can take a long time) | ||
* @return int number of rows affected by the execution. | ||
* @throws Exception execution failed | unsupported driver | ||
* @see \yii\db\Command::execute() | ||
*/ | ||
public static function optimize() | ||
{ | ||
$db = Craft::$app->getDb(); | ||
$driverName = $db->driverName; | ||
$tableName = self::$tableName; | ||
|
||
if ($driverName === 'mysql') { | ||
return $db->createCommand("OPTIMIZE TABLE {$tableName}")->execute(); | ||
} elseif ($driverName === 'pgsql') { | ||
return $db->createCommand("VACUUM ANALYZE {$tableName}")->execute(); | ||
} else { | ||
throw new \Exception('Unsupported database driver'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace unit\helpers; | ||
|
||
use Codeception\Test\Unit; | ||
use Craft; | ||
use craft\db\Table; | ||
use viget\base\helpers\SearchIndex; | ||
|
||
class SearchIndexHelperTest extends Unit | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
Craft::$app->getDb()->createCommand()->truncateTable(Table::SEARCHINDEX)->execute(); | ||
} | ||
|
||
public function testOptimizeForMysql() | ||
{ | ||
if (Craft::$app->getDb()->driverName !== 'mysql') { | ||
$this->markTestSkipped('Skip MySQL testing when driver is not mysql'); | ||
} | ||
|
||
$result = SearchIndex::optimize(); | ||
|
||
// Assert: Verify that the result is an integer (number of rows affected) | ||
$this->assertIsInt($result); | ||
} | ||
|
||
public function testOptimizeForPostgres() | ||
{ | ||
if (Craft::$app->getDb()->driverName !== 'pgsql') { | ||
$this->markTestSkipped('Skip PostgreSQL testing when driver is not pgsql'); | ||
} | ||
|
||
$result = SearchIndex::optimize(); | ||
|
||
// Assert: Verify that the result is an integer (number of rows affected) | ||
$this->assertIsInt($result); | ||
} | ||
|
||
public function testUnsupportedDriverThrowsException() | ||
{ | ||
Craft::$app->getDb()->driverName = 'unsupported_driver'; | ||
|
||
// Assert: Expect an exception to be thrown | ||
$this->expectException(\Exception::class); | ||
|
||
SearchIndex::optimize(); | ||
} | ||
} |