PopulateNewlyIntroducedDatabaseFields.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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\WorkflowEngine\Migration;
  8. use OCP\DB\IResult;
  9. use OCP\IDBConnection;
  10. use OCP\Migration\IOutput;
  11. use OCP\Migration\IRepairStep;
  12. use OCP\WorkflowEngine\IManager;
  13. class PopulateNewlyIntroducedDatabaseFields implements IRepairStep {
  14. /** @var IDBConnection */
  15. private $dbc;
  16. public function __construct(IDBConnection $dbc) {
  17. $this->dbc = $dbc;
  18. }
  19. public function getName() {
  20. return 'Populating added database structures for workflows';
  21. }
  22. public function run(IOutput $output) {
  23. $result = $this->getIdsWithoutScope();
  24. $this->populateScopeTable($result);
  25. $result->closeCursor();
  26. }
  27. protected function populateScopeTable(IResult $ids): void {
  28. $qb = $this->dbc->getQueryBuilder();
  29. $insertQuery = $qb->insert('flow_operations_scope');
  30. while (($id = $ids->fetchOne()) !== false) {
  31. $insertQuery->values(['operation_id' => $qb->createNamedParameter($id), 'type' => IManager::SCOPE_ADMIN]);
  32. $insertQuery->execute();
  33. }
  34. }
  35. protected function getIdsWithoutScope(): IResult {
  36. $qb = $this->dbc->getQueryBuilder();
  37. $selectQuery = $qb->select('o.id')
  38. ->from('flow_operations', 'o')
  39. ->leftJoin('o', 'flow_operations_scope', 's', $qb->expr()->eq('o.id', 's.operation_id'))
  40. ->where($qb->expr()->isNull('s.operation_id'));
  41. // The left join operation is not necessary, usually, but it's a safe-guard
  42. // in case the repair step is executed multiple times for whatever reason.
  43. /** @var IResult $result */
  44. $result = $selectQuery->execute();
  45. return $result;
  46. }
  47. }