Passed
Push — master ( 2187f8...15d39c )
by Joas
11:45 queued 12s
created

populateScopeTable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2019 Arthur Schiwon <[email protected]>
5
 *
6
 * @author Arthur Schiwon <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\WorkflowEngine\Migration;
26
27
use Doctrine\DBAL\Driver\Statement;
28
use OCA\WorkflowEngine\Entity\File;
29
use OCP\IDBConnection;
30
use OCP\Migration\IOutput;
31
use OCP\Migration\IRepairStep;
32
use OCP\WorkflowEngine\IManager;
33
34
class PopulateNewlyIntroducedDatabaseFields implements IRepairStep {
35
36
	/** @var IDBConnection */
37
	private $dbc;
38
39
	public function __construct(IDBConnection $dbc) {
40
		$this->dbc = $dbc;
41
	}
42
43
	public function getName() {
44
		return 'Populating added database structures for workflows';
45
	}
46
47
	public function run(IOutput $output) {
48
		$result = $this->getIdsWithoutScope();
49
50
		$this->populateScopeTable($result);
51
52
		$result->closeCursor();
53
54
		$this->populateEntityCol();
55
	}
56
57
	protected function populateEntityCol() {
58
		$qb = $this->dbc->getQueryBuilder();
59
60
		$qb->update('flow_operations')
61
			->set('entity', $qb->createNamedParameter(File::class))
62
			->where($qb->expr()->emptyString('entity'))
63
			->execute();
64
65
	}
66
67
	protected function populateScopeTable(Statement $ids): void {
68
		$qb = $this->dbc->getQueryBuilder();
69
70
		$insertQuery = $qb->insert('flow_operations_scope');
71
		while($id = $ids->fetchColumn(0)) {
72
			$insertQuery->values(['operation_id' => $qb->createNamedParameter($id), 'type' => IManager::SCOPE_ADMIN]);
73
			$insertQuery->execute();
74
		}
75
	}
76
77
	protected function getIdsWithoutScope(): Statement {
78
		$qb = $this->dbc->getQueryBuilder();
79
		$selectQuery = $qb->select('o.id')
80
			->from('flow_operations', 'o')
81
			->leftJoin('o', 'flow_operations_scope', 's', $qb->expr()->eq('o.id', 's.operation_id'))
82
			->where($qb->expr()->isNull('s.operation_id'));
83
		// The left join operation is not necessary, usually, but it's a safe-guard
84
		// in case the repair step is executed multiple times for whatever reason.
85
86
		return $selectQuery->execute();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $selectQuery->execute() could return the type integer which is incompatible with the type-hinted return Doctrine\DBAL\Driver\Statement. Consider adding an additional type-check to rule them out.
Loading history...
87
	}
88
89
}
90