FixMountStorages.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Repair\NC11;
  7. use OCP\DB\QueryBuilder\IQueryBuilder;
  8. use OCP\IDBConnection;
  9. use OCP\Migration\IOutput;
  10. use OCP\Migration\IRepairStep;
  11. class FixMountStorages implements IRepairStep {
  12. /** @var IDBConnection */
  13. private $db;
  14. /**
  15. * @param IDBConnection $db
  16. */
  17. public function __construct(IDBConnection $db) {
  18. $this->db = $db;
  19. }
  20. /**
  21. * @return string
  22. */
  23. public function getName() {
  24. return 'Fix potential broken mount points';
  25. }
  26. public function run(IOutput $output) {
  27. $query = $this->db->getQueryBuilder();
  28. $query->select('m.id', 'f.storage')
  29. ->from('mounts', 'm')
  30. ->leftJoin('m', 'filecache', 'f', $query->expr()->eq('m.root_id', 'f.fileid'))
  31. ->where($query->expr()->neq('m.storage_id', 'f.storage'));
  32. $update = $this->db->getQueryBuilder();
  33. $update->update('mounts')
  34. ->set('storage_id', $update->createParameter('storage'))
  35. ->where($query->expr()->eq('id', $update->createParameter('mount')));
  36. $result = $query->execute();
  37. $entriesUpdated = 0;
  38. while ($row = $result->fetch()) {
  39. $update->setParameter('storage', $row['storage'], IQueryBuilder::PARAM_INT)
  40. ->setParameter('mount', $row['id'], IQueryBuilder::PARAM_INT);
  41. $update->execute();
  42. $entriesUpdated++;
  43. }
  44. $result->closeCursor();
  45. if ($entriesUpdated > 0) {
  46. $output->info($entriesUpdated . ' mounts updated');
  47. return;
  48. }
  49. $output->info('No mounts updated');
  50. }
  51. }