UUIDFixInsert.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. public function __construct(
  15. protected IConfig $config,
  16. protected UserMapping $userMapper,
  17. protected GroupMapping $groupMapper,
  18. protected IJobList $jobList,
  19. ) {
  20. }
  21. /**
  22. * Returns the step's name
  23. *
  24. * @return string
  25. * @since 9.1.0
  26. */
  27. public function getName() {
  28. return 'Insert UUIDFix background job for user and group in batches';
  29. }
  30. /**
  31. * Run repair step.
  32. * Must throw exception on error.
  33. *
  34. * @param IOutput $output
  35. * @throws \Exception in case of failure
  36. * @since 9.1.0
  37. */
  38. public function run(IOutput $output) {
  39. $installedVersion = $this->config->getAppValue('user_ldap', 'installed_version', '1.2.1');
  40. if (version_compare($installedVersion, '1.2.1') !== -1) {
  41. return;
  42. }
  43. foreach ([$this->userMapper, $this->groupMapper] as $mapper) {
  44. $offset = 0;
  45. $batchSize = 50;
  46. $jobClass = $mapper instanceof UserMapping ? UUIDFixUser::class : UUIDFixGroup::class;
  47. do {
  48. $retry = false;
  49. $records = $mapper->getList($offset, $batchSize);
  50. if (count($records) === 0) {
  51. continue;
  52. }
  53. try {
  54. $this->jobList->add($jobClass, ['records' => $records]);
  55. $offset += $batchSize;
  56. } catch (\InvalidArgumentException $e) {
  57. if (str_contains($e->getMessage(), 'Background job arguments can\'t exceed 4000')) {
  58. $batchSize = (int)floor(count($records) * 0.8);
  59. $retry = true;
  60. }
  61. }
  62. } while (count($records) === $batchSize || $retry);
  63. }
  64. }
  65. }