memories/lib/Db/AddMissingIndices.php

74 lines
2.5 KiB
PHP
Raw Normal View History

2022-10-26 17:06:45 +00:00
<?php
declare(strict_types=1);
2022-10-26 17:06:45 +00:00
namespace OCA\Memories\Db;
use OC\DB\SchemaWrapper;
use OCP\Migration\IOutput;
2022-10-26 17:06:45 +00:00
class AddMissingIndices
{
/**
* Add missing indices to the database schema.
*/
public static function run(IOutput $output): SchemaWrapper
2022-10-26 17:06:45 +00:00
{
$connection = \OC::$server->get(\OC\DB\Connection::class);
$schema = new SchemaWrapper($connection);
2022-10-26 17:06:45 +00:00
// Should migrate at end
$ops = [];
2022-10-26 17:06:45 +00:00
// Speed up CTE lookup for subdirectories
if ($schema->hasTable('filecache')) {
$table = $schema->getTable('filecache');
if (!$table->hasIndex('memories_parent_mimetype')) {
$table->addIndex(['parent', 'mimetype'], 'memories_parent_mimetype');
$ops[] = 'filecache::memories_parent_mimetype';
}
}
2022-10-26 17:06:45 +00:00
// Add index on systemtag_object_mapping to speed up the query
if ($schema->hasTable('systemtag_object_mapping')) {
$table = $schema->getTable('systemtag_object_mapping');
if (!$table->hasIndex('memories_type_tagid')) {
$table->addIndex(['objecttype', 'systemtagid'], 'memories_type_tagid');
$ops[] = 'systemtag_object_mapping::memories_type_tagid';
2022-10-26 17:06:45 +00:00
}
}
// Add index on recognize detections for file id to speed up joins
if ($schema->hasTable('recognize_face_detections')) {
$table = $schema->getTable('recognize_face_detections');
// Starting at some version, recognize ships an index on file_id
// In that case, we *remove* the memories index if it exists
$hasOwn = $table->hasIndex('recognize_facedet_file');
$hasOur = $table->hasIndex('memories_file_id');
if (!$hasOwn && !$hasOur) {
// Add our index because none exists
2022-10-26 17:06:45 +00:00
$table->addIndex(['file_id'], 'memories_file_id');
$ops[] = 'recognize_face_detections::memories_file_id';
} elseif ($hasOwn && $hasOur) {
// Remove our index because recognize has one
$table->dropIndex('memories_file_id');
$ops[] = '-recognize_face_detections::memories_file_id';
2022-10-26 17:06:45 +00:00
}
}
// Migrate
if (\count($ops) > 0) {
$output->info('Updating external table schema: '.implode(', ', $ops));
2022-10-26 17:06:45 +00:00
$connection->migrateToSchema($schema->getWrappedSchema());
} else {
$output->info('External table schema seem up to date');
2022-10-26 17:06:45 +00:00
}
return $schema;
}
}