Passed
Push — master ( 3749b7...a0444b )
by Julius
13:05 queued 12s
created

Version1011Date20201120125158::changeSchema()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
nc 3
nop 3
dl 0
loc 17
rs 9.9
c 1
b 0
f 0
1
<?php
2
/*
3
 * @copyright Copyright (c) 2020 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
declare(strict_types=1);
25
26
namespace OCA\FederatedFileSharing\Migration;
27
28
use Closure;
29
use Doctrine\DBAL\Types\Type;
30
use Doctrine\DBAL\Types\Types;
31
use OCP\DB\ISchemaWrapper;
32
use OCP\IDBConnection;
33
use OCP\Migration\IOutput;
34
use OCP\Migration\SimpleMigrationStep;
35
36
class Version1011Date20201120125158 extends SimpleMigrationStep {
37
38
	/** @var IDBConnection */
39
	private $connection;
40
41
	public function __construct(IDBConnection $connection) {
42
		$this->connection = $connection;
43
	}
44
45
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
46
		/** @var ISchemaWrapper $schema */
47
		$schema = $schemaClosure();
48
49
		if ($schema->hasTable('federated_reshares')) {
50
			$table = $schema->getTable('federated_reshares');
51
			$remoteIdColumn = $table->getColumn('remote_id');
52
			if ($remoteIdColumn && $remoteIdColumn->getType()->getName() !== Types::STRING) {
53
				$remoteIdColumn->setNotnull(false);
54
				$remoteIdColumn->setType(Type::getType(Types::STRING));
55
				$remoteIdColumn->setOptions(['length' => 255]);
56
				$remoteIdColumn->setDefault('');
57
				return $schema;
58
			}
59
		}
60
61
		return null;
62
	}
63
64
	public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
65
		$qb = $this->connection->getQueryBuilder();
66
		$qb->update('federated_reshares')
67
			->set('remote_id', $qb->createNamedParameter(''))
68
			->where($qb->expr()->eq('remote_id', $qb->createNamedParameter('-1')));
69
		$qb->execute();
70
	}
71
}
72