Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add indexes for system file table #3

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

use October\Rain\Database\Schema\Blueprint;
use October\Rain\Database\Updates\Migration;

/**
* Class DbUpdateSystemFilesTableAddIndexes.
*/
class DbUpdateSystemFilesTableAddIndexes extends Migration
{
/**
* @var string
*/
const TABLE_NAME = 'system_files';

/**
* @var array
*/
private $tableIndexes = [
'field' => 'system_files_field_index',
'attachment_id' => 'system_files_attachment_id_index',
'attachment_type' => 'system_files_attachment_type_index',
];

/**
* Apply migration.
*
* return void
*/
public function up(): void
{
if (!Schema::hasColumns(self::TABLE_NAME, array_keys($this->tableIndexes))) {
return;
}

/**
* Drop indexes.
*/
Schema::table(self::TABLE_NAME, function(Blueprint $table) {
$this->dropIndexes($table);
});

/**
* Create indexes.
*/
Schema::table(self::TABLE_NAME, function(Blueprint $table) {
$this->createIndexes($table);
});
}

/**
* Rollback migration.
*
* @return void
*/
public function down(): void
{
if (!Schema::hasColumns(self::TABLE_NAME, array_keys($this->tableIndexes))) {
return;
}

/**
* Drop indexes.
*/
Schema::table(self::TABLE_NAME, function(Blueprint $table) {

/**
* @var array
*/
$listIndexes = $this->getListTableIndexes();

if (array_key_exists('system_files_attachments_index', $listIndexes)) {
$table->dropIndex('system_files_attachments_index');
}
});
}

/**
* Delete list of indexes relative to $tableIndexes variable.
*
* @param \October\Rain\Database\Schema\Blueprint
*
* @return void
*/
private function dropIndexes(Blueprint $table): void
{
/**
* @var array
*/
$listIndexes = $this->getListTableIndexes();

foreach ($this->tableIndexes as $index) {
if (array_key_exists($index, $listIndexes)) {
$table->dropIndex($index);
}
}
}

/**
* Create list of indexes relative to $tableIndexes variable.
*
* @param \October\Rain\Database\Schema\Blueprint
*
* @return void
*/
private function createIndexes(Blueprint $table): void
{
/**
* @var array
*/
$listIndexes = $this->getListTableIndexes();

if (!array_key_exists('system_files_attachments_index', $listIndexes)) {
$table->index([
'field',
'attachment_id',
'attachment_type',
], 'system_files_attachments_index');
}

foreach ($this->tableIndexes as $column => $index) {
if (!array_key_exists($index, $listIndexes)) {
$table->index($column, $index);
}
}
}

/**
* @return array
*/
private function getListTableIndexes(): array
{
/**
* @var \Doctrine\DBAL\Schema\MySqlSchemaManager
*/
$mysqlManager = \Schema::getConnection()->getDoctrineSchemaManager();

/**
* @var array
*/
return $mysqlManager->listTableIndexes(self::TABLE_NAME);
}
}