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

Version11300Date20201120141438::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * @copyright Copyright (c) 2020 Julius Härtl <[email protected]>
7
 *
8
 * @author Julius Härtl <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
28
namespace OCA\Files_Sharing\Migration;
29
30
use Closure;
31
use Doctrine\DBAL\Types\Type;
32
use Doctrine\DBAL\Types\Types;
33
use OCP\DB\ISchemaWrapper;
34
use OCP\IDBConnection;
35
use OCP\Migration\IOutput;
36
use OCP\Migration\SimpleMigrationStep;
37
38
class Version11300Date20201120141438 extends SimpleMigrationStep {
39
40
	/** @var IDBConnection */
41
	private $connection;
42
43
	public function __construct(IDBConnection $connection) {
44
		$this->connection = $connection;
45
	}
46
47
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
48
		/** @var ISchemaWrapper $schema */
49
		$schema = $schemaClosure();
50
51
		if (!$schema->hasTable('share_external')) {
52
			$table = $schema->createTable('share_external');
53
			$table->addColumn('id', Types::BIGINT, [
54
				'autoincrement' => true,
55
				'notnull' => true,
56
			]);
57
			$table->addColumn('parent', Types::BIGINT, [
58
				'notnull' => false,
59
				'default' => -1,
60
			]);
61
			$table->addColumn('share_type', Types::INTEGER, [
62
				'notnull' => false,
63
				'length' => 4,
64
			]);
65
			$table->addColumn('remote', Types::STRING, [
66
				'notnull' => true,
67
				'length' => 512,
68
			]);
69
			$table->addColumn('remote_id', Types::STRING, [
70
				'notnull' => false,
71
				'length' => 255,
72
				'default' => '',
73
			]);
74
			$table->addColumn('share_token', Types::STRING, [
75
				'notnull' => true,
76
				'length' => 64,
77
			]);
78
			$table->addColumn('password', Types::STRING, [
79
				'notnull' => false,
80
				'length' => 64,
81
			]);
82
			$table->addColumn('name', Types::STRING, [
83
				'notnull' => true,
84
				'length' => 64,
85
			]);
86
			$table->addColumn('owner', Types::STRING, [
87
				'notnull' => true,
88
				'length' => 64,
89
			]);
90
			$table->addColumn('user', Types::STRING, [
91
				'notnull' => true,
92
				'length' => 64,
93
			]);
94
			$table->addColumn('mountpoint', Types::STRING, [
95
				'notnull' => true,
96
				'length' => 4000,
97
			]);
98
			$table->addColumn('mountpoint_hash', Types::STRING, [
99
				'notnull' => true,
100
				'length' => 32,
101
			]);
102
			$table->addColumn('accepted', Types::INTEGER, [
103
				'notnull' => true,
104
				'length' => 4,
105
				'default' => 0,
106
			]);
107
			$table->setPrimaryKey(['id']);
108
			$table->addIndex(['user'], 'sh_external_user');
109
			$table->addUniqueIndex(['user', 'mountpoint_hash'], 'sh_external_mp');
110
		} else {
111
			$table = $schema->getTable('share_external');
112
			$remoteIdColumn = $table->getColumn('remote_id');
113
			if ($remoteIdColumn && $remoteIdColumn->getType()->getName() !== Types::STRING) {
114
				$remoteIdColumn->setNotnull(false);
115
				$remoteIdColumn->setType(Type::getType(Types::STRING));
116
				$remoteIdColumn->setOptions(['length' => 255]);
117
				$remoteIdColumn->setDefault('');
118
			}
119
		}
120
121
		return $schema;
122
	}
123
124
	public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
125
		$qb = $this->connection->getQueryBuilder();
126
		$qb->update('share_external')
127
			->set('remote_id', $qb->createNamedParameter(''))
128
			->where($qb->expr()->eq('remote_id', $qb->createNamedParameter('-1')));
129
		$qb->execute();
130
	}
131
}
132