diff --git a/core/Migrations/Version28000Date20231004103301.php b/core/Migrations/Version28000Date20231004103301.php index f1ce479d0faa5..f3046e0f414ec 100644 --- a/core/Migrations/Version28000Date20231004103301.php +++ b/core/Migrations/Version28000Date20231004103301.php @@ -28,11 +28,16 @@ use Closure; use OCP\DB\ISchemaWrapper; use OCP\DB\Types; +use OCP\IConfig; use OCP\Migration\IOutput; use OCP\Migration\SimpleMigrationStep; // Create new tables for the Metadata API (files_metadata and files_metadata_index). class Version28000Date20231004103301 extends SimpleMigrationStep { + + public function __construct(private IConfig $config) { + } + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { /** @var ISchemaWrapper $schema */ $schema = $schemaClosure(); @@ -94,6 +99,8 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt return null; } + $this->config->setAppValue('core', 'files_metadata_installed', '1'); + return $schema; } } diff --git a/lib/private/FilesMetadata/FilesMetadataManager.php b/lib/private/FilesMetadata/FilesMetadataManager.php index c651fead0cea5..013c85af604b6 100644 --- a/lib/private/FilesMetadata/FilesMetadataManager.php +++ b/lib/private/FilesMetadata/FilesMetadataManager.php @@ -52,6 +52,7 @@ use OCP\FilesMetadata\Model\IFilesMetadata; use OCP\FilesMetadata\Model\IMetadataValueWrapper; use OCP\IConfig; +use OCP\IDBConnection; use Psr\Log\LoggerInterface; /** @@ -60,6 +61,7 @@ */ class FilesMetadataManager implements IFilesMetadataManager { public const CONFIG_KEY = 'files_metadata'; + public const MIGRATION_DONE = 'files_metadata_installed'; private const JSON_MAXSIZE = 100000; private ?IFilesMetadata $all = null; @@ -241,10 +243,10 @@ public function getMetadataQuery( string $fileTableAlias, string $fileIdField ): ?IMetadataQuery { - // we don't want to join metadata table if never filled - if ($this->config->getAppValue('core', self::CONFIG_KEY, '') === '') { + if (!$this->metadataInitiated()) { return null; } + return new MetadataQuery($qb, $this->getKnownMetadata(), $fileTableAlias, $fileIdField); } @@ -320,4 +322,26 @@ public static function loadListeners(IEventDispatcher $eventDispatcher): void { $eventDispatcher->addServiceListener(NodeWrittenEvent::class, MetadataUpdate::class); $eventDispatcher->addServiceListener(CacheEntryRemovedEvent::class, MetadataDelete::class); } + + /** + * Will confirm that tables were created and store an app value to cache the result. + * Can be removed in 29 as this is to avoid strange situation when Nextcloud files were + * replaced but the upgrade was not triggered yet. + * + * @return bool + */ + private function metadataInitiated(): bool { + if ($this->config->getAppValue('core', self::MIGRATION_DONE, '0') === '1') { + return true; + } + + $dbConnection = \OCP\Server::get(IDBConnection::class); + if ($dbConnection->tableExists(MetadataRequestService::TABLE_METADATA)) { + $this->config->setAppValue('core', self::MIGRATION_DONE, '1'); + + return true; + } + + return false; + } }