UUIDFixInsert.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\User_LDAP\Migration;
  7. use OCA\User_LDAP\Mapping\GroupMapping;
  8. use OCA\User_LDAP\Mapping\UserMapping;
  9. use OCP\BackgroundJob\IJobList;
  10. use OCP\IConfig;
  11. use OCP\Migration\IOutput;
  12. use OCP\Migration\IRepairStep;
  13. class UUIDFixInsert implements IRepairStep {
  14. /** @var IConfig */
  15. protected $config;
  16. /** @var UserMapping */
  17. protected $userMapper;
  18. /** @var GroupMapping */
  19. protected $groupMapper;
  20. /** @var IJobList */
  21. protected $jobList;
  22. public function __construct(IConfig $config, UserMapping $userMapper, GroupMapping $groupMapper, IJobList $jobList) {
  23. $this->config = $config;
  24. $this->userMapper = $userMapper;
  25. $this->groupMapper = $groupMapper;
  26. $this->jobList = $jobList;
  27. }
  28. /**
  29. * Returns the step's name
  30. *
  31. * @return string
  32. * @since 9.1.0
  33. */
  34. public function getName() {
  35. return 'Insert UUIDFix background job for user and group in batches';
  36. }
  37. /**
  38. * Run repair step.
  39. * Must throw exception on error.
  40. *
  41. * @param IOutput $output
  42. * @throws \Exception in case of failure
  43. * @since 9.1.0
  44. */
  45. public function run(IOutput $output) {
  46. $installedVersion = $this->config->getAppValue('user_ldap', 'installed_version', '1.2.1');
  47. if (version_compare($installedVersion, '1.2.1') !== -1) {
  48. return;
  49. }
  50. foreach ([$this->userMapper, $this->groupMapper] as $mapper) {
  51. $offset = 0;
  52. $batchSize = 50;
  53. $jobClass = $mapper instanceof UserMapping ? UUIDFixUser::class : UUIDFixGroup::class;
  54. do {
  55. $retry = false;
  56. $records = $mapper->getList($offset, $batchSize);
  57. if (count($records) === 0) {
  58. continue;
  59. }
  60. try {
  61. $this->jobList->add($jobClass, ['records' => $records]);
  62. $offset += $batchSize;
  63. } catch (\InvalidArgumentException $e) {
  64. if (str_contains($e->getMessage(), 'Background job arguments can\'t exceed 4000')) {
  65. $batchSize = (int)floor(count($records) * 0.8);
  66. $retry = true;
  67. }
  68. }
  69. } while (count($records) === $batchSize || $retry);
  70. }
  71. }
  72. }