-
-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add missing indices * A workaround for MySQL * bump minor version Co-authored-by: ildyria <[email protected]>
- Loading branch information
Showing
3 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
database/migrations/2022_04_16_170724_add_missing_indices.php
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,67 @@ | ||
<?php | ||
|
||
use Doctrine\DBAL\Exception as DBALException; | ||
use Doctrine\DBAL\Schema\AbstractSchemaManager; | ||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class AddMissingIndices extends Migration | ||
{ | ||
private AbstractSchemaManager $schemaManager; | ||
private string $driverName; | ||
|
||
/** | ||
* @throws DBALException | ||
*/ | ||
public function __construct() | ||
{ | ||
$connection = Schema::connection(null)->getConnection(); | ||
$this->schemaManager = $connection->getDoctrineSchemaManager(); | ||
$this->driverName = $connection->getDriverName(); | ||
} | ||
|
||
public function up() | ||
{ | ||
// MySQL cannot create indices over unlimited string values | ||
// So we must explicitly define an upper bound on how many characters | ||
// are analyzed for sorting | ||
$descriptionSQL = match ($this->driverName) { | ||
'mysql' => DB::raw('description(128)'), | ||
default => 'description', | ||
}; | ||
|
||
Schema::table('photos', function (Blueprint $table) use ($descriptionSQL) { | ||
// These indices are needed to efficiently retrieve the covers of | ||
// albums acc. to different sorting criteria | ||
// Note, that covers are always sorted acc. to `is_starred` first. | ||
$table->index(['album_id', 'is_starred', 'title']); | ||
$table->index(['album_id', 'is_starred', $descriptionSQL]); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::table('photos', function (Blueprint $table) { | ||
$this->dropIndexIfExists($table, 'photos_album_id_is_starred_title_index'); | ||
$this->dropIndexIfExists($table, 'photos_album_id_is_starred_description_index'); | ||
}); | ||
} | ||
|
||
/** | ||
* A helper function that allows to drop an index if exists. | ||
* | ||
* @param Blueprint $table | ||
* @param string $indexName | ||
* | ||
* @throws DBALException | ||
*/ | ||
private function dropIndexIfExists(Blueprint $table, string $indexName) | ||
{ | ||
$doctrineTable = $this->schemaManager->listTableDetails($table->getTable()); | ||
if ($doctrineTable->hasIndex($indexName)) { | ||
$table->dropIndex($indexName); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
database/migrations/2022_04_16_174503_bump_version040501.php
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,27 @@ | ||
<?php | ||
|
||
use App\Models\Configs; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class BumpVersion040501 extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Configs::where('key', 'version')->update(['value' => '040501']); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Configs::where('key', 'version')->update(['value' => '040500']); | ||
} | ||
} |
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 +1 @@ | ||
4.5.0 | ||
4.5.1 |