RegisterBuildReminderIndexBackgroundJob.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Migration;
  8. use OCA\DAV\BackgroundJob\BuildReminderIndexBackgroundJob;
  9. use OCP\BackgroundJob\IJobList;
  10. use OCP\IConfig;
  11. use OCP\IDBConnection;
  12. use OCP\Migration\IOutput;
  13. use OCP\Migration\IRepairStep;
  14. /**
  15. * Class RegisterBuildReminderIndexBackgroundJob
  16. *
  17. * @package OCA\DAV\Migration
  18. */
  19. class RegisterBuildReminderIndexBackgroundJob implements IRepairStep {
  20. /** @var string */
  21. private const CONFIG_KEY = 'buildCalendarReminderIndex';
  22. /**
  23. * @param IDBConnection $db
  24. * @param IJobList $jobList
  25. * @param IConfig $config
  26. */
  27. public function __construct(
  28. private IDBConnection $db,
  29. private IJobList $jobList,
  30. private IConfig $config,
  31. ) {
  32. }
  33. /**
  34. * @return string
  35. */
  36. public function getName() {
  37. return 'Registering building of calendar reminder index as background job';
  38. }
  39. /**
  40. * @param IOutput $output
  41. */
  42. public function run(IOutput $output) {
  43. // only run once
  44. if ($this->config->getAppValue('dav', self::CONFIG_KEY) === 'yes') {
  45. $output->info('Repair step already executed');
  46. return;
  47. }
  48. $query = $this->db->getQueryBuilder();
  49. $query->select($query->createFunction('MAX(' . $query->getColumnName('id') . ')'))
  50. ->from('calendarobjects');
  51. $result = $query->executeQuery();
  52. $maxId = (int)$result->fetchOne();
  53. $result->closeCursor();
  54. $output->info('Add background job');
  55. $this->jobList->add(BuildReminderIndexBackgroundJob::class, [
  56. 'offset' => 0,
  57. 'stopAt' => $maxId
  58. ]);
  59. // if all were done, no need to redo the repair during next upgrade
  60. $this->config->setAppValue('dav', self::CONFIG_KEY, 'yes');
  61. }
  62. }