SearchLuceneTables.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Repair;
  25. use OCP\Migration\IOutput;
  26. use OCP\Migration\IRepairStep;
  27. class SearchLuceneTables implements IRepairStep {
  28. public function getName() {
  29. return 'Repair duplicate entries in oc_lucene_status';
  30. }
  31. /**
  32. * Fix duplicate entries in oc_lucene_status
  33. *
  34. * search_lucene prior to v0.5.0 did not have a primary key on the lucene_status table. Newer versions do, which
  35. * causes the migration check to fail because it tries to insert duplicate rows into the new schema.
  36. *
  37. * FIXME Currently, apps don't have a way of repairing anything before the migration check:
  38. * @link https://github.com/owncloud/core/issues/10980
  39. *
  40. * As a result this repair step needs to live in the core repo, although it belongs into search_lucene:
  41. * @link https://github.com/owncloud/core/issues/10205#issuecomment-54957557
  42. *
  43. * It will completely remove any rows that make a file id have more than one status:
  44. * fileid | status fileid | status
  45. * --------+-------- will become --------+--------
  46. * 2 | E 3 | E
  47. * 2 | I
  48. * 3 | E
  49. *
  50. * search_lucene will then reindex the fileids without a status when the next indexing job is executed
  51. */
  52. public function run(IOutput $out) {
  53. $connection = \OC::$server->getDatabaseConnection();
  54. if ($connection->tableExists('lucene_status')) {
  55. $out->info('removing duplicate entries from lucene_status');
  56. $query = $connection->prepare('
  57. DELETE FROM `*PREFIX*lucene_status`
  58. WHERE `fileid` IN (
  59. SELECT `fileid`
  60. FROM (
  61. SELECT `fileid`
  62. FROM `*PREFIX*lucene_status`
  63. GROUP BY `fileid`
  64. HAVING count(`fileid`) > 1
  65. ) AS `mysqlerr1093hack`
  66. )');
  67. $query->execute();
  68. } else {
  69. $out->info('lucene_status table does not exist -> nothing to do');
  70. }
  71. }
  72. }