2022-10-26 17:06:45 +00:00
|
|
|
<?php
|
|
|
|
|
2023-10-15 02:20:21 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-10-26 17:06:45 +00:00
|
|
|
namespace OCA\Memories\Db;
|
|
|
|
|
|
|
|
use OC\DB\SchemaWrapper;
|
2023-10-06 22:42:47 +00:00
|
|
|
use OCP\Migration\IOutput;
|
2022-10-26 17:06:45 +00:00
|
|
|
|
|
|
|
class AddMissingIndices
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Add missing indices to the database schema.
|
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
public static function run(IOutput $output): SchemaWrapper
|
2022-10-26 17:06:45 +00:00
|
|
|
{
|
2023-04-14 02:43:13 +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
|
2023-10-06 22:42:47 +00:00
|
|
|
$ops = [];
|
2022-10-26 17:06:45 +00:00
|
|
|
|
2023-10-06 22:29:16 +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');
|
2023-10-06 22:42:47 +00:00
|
|
|
$ops[] = 'filecache::memories_parent_mimetype';
|
2023-10-06 22:29:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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');
|
2023-10-06 22:42:47 +00:00
|
|
|
$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');
|
|
|
|
|
2023-10-06 22:42:47 +00:00
|
|
|
// 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');
|
2023-10-06 22:42:47 +00:00
|
|
|
$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
|
2023-10-14 09:07:18 +00:00
|
|
|
if (\count($ops) > 0) {
|
2023-10-06 22:42:47 +00:00
|
|
|
$output->info('Updating external table schema: '.implode(', ', $ops));
|
2022-10-26 17:06:45 +00:00
|
|
|
$connection->migrateToSchema($schema->getWrappedSchema());
|
2023-10-06 22:42:47 +00:00
|
|
|
} else {
|
|
|
|
$output->info('External table schema seem up to date');
|
2022-10-26 17:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $schema;
|
|
|
|
}
|
|
|
|
}
|