Skip to content

Commit

Permalink
Add missing indices (#1278)
Browse files Browse the repository at this point in the history
* Add missing indices

* A workaround for MySQL

* bump minor version

Co-authored-by: ildyria <[email protected]>
  • Loading branch information
nagmat84 and ildyria authored Apr 16, 2022
1 parent 0199212 commit af768af
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
67 changes: 67 additions & 0 deletions database/migrations/2022_04_16_170724_add_missing_indices.php
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 database/migrations/2022_04_16_174503_bump_version040501.php
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']);
}
}
2 changes: 1 addition & 1 deletion version.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.5.0
4.5.1

0 comments on commit af768af

Please sign in to comment.