PopulateNewlyIntroducedDatabaseFields.php 1.6 KB

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