Passed
Push — master ( cdfad9...e39d65 )
by Joas
12:09 queued 10s
created

Version20000Date20201109081918::changeSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
nc 1
nop 3
dl 0
loc 26
rs 9.6666
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020 Joas Schilling <[email protected]>
7
 *
8
 * @author Joas Schilling <[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
namespace OC\Core\Migrations;
28
29
use Closure;
30
use Doctrine\DBAL\Types\Type;
31
use OCP\DB\ISchemaWrapper;
32
use OCP\IDBConnection;
33
use OCP\Migration\IOutput;
34
use OCP\Migration\SimpleMigrationStep;
35
36
class Version20000Date20201109081918 extends SimpleMigrationStep {
37
38
	/** @var IDBConnection */
39
	protected $connection;
40
41
	public function __construct(IDBConnection $connection) {
42
		$this->connection = $connection;
43
	}
44
45
	/**
46
	 * @param IOutput $output
47
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
48
	 * @param array $options
49
	 * @return null|ISchemaWrapper
50
	 */
51
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
52
		/** @var ISchemaWrapper $schema */
53
		$schema = $schemaClosure();
54
55
		$table = $schema->createTable('storages_credentials');
56
		$table->addColumn('id', Type::BIGINT, [
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::BIGINT has been deprecated: Use {@see Types::BIGINT} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

56
		$table->addColumn('id', /** @scrutinizer ignore-deprecated */ Type::BIGINT, [

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
57
			'autoincrement' => true,
58
			'notnull' => true,
59
			'length' => 64,
60
		]);
61
		$table->addColumn('user', Type::STRING, [
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::STRING has been deprecated: Use {@see Types::STRING} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

61
		$table->addColumn('user', /** @scrutinizer ignore-deprecated */ Type::STRING, [

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
62
			'notnull' => false,
63
			'length' => 64,
64
		]);
65
		$table->addColumn('identifier', Type::STRING, [
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::STRING has been deprecated: Use {@see Types::STRING} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

65
		$table->addColumn('identifier', /** @scrutinizer ignore-deprecated */ Type::STRING, [

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
66
			'notnull' => true,
67
			'length' => 64,
68
		]);
69
		$table->addColumn('credentials', Type::TEXT, [
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::TEXT has been deprecated: Use {@see Types::TEXT} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

69
		$table->addColumn('credentials', /** @scrutinizer ignore-deprecated */ Type::TEXT, [

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
70
			'notnull' => false,
71
		]);
72
		$table->setPrimaryKey(['id']);
73
		$table->addUniqueIndex(['user', 'identifier'], 'stocred_ui');
74
		$table->addIndex(['user'], 'stocred_user');
75
76
		return $schema;
77
	}
78
79
	/**
80
	 * {@inheritDoc}
81
	 *
82
	 * @since 13.0.0
83
	 */
84
	public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void {
85
		if (!$this->connection->tableExists('credentials')) {
86
			return;
87
		}
88
89
		$query = $this->connection->getQueryBuilder();
90
		$query->select('*')
91
			->from('credentials');
92
93
		$insert = $this->connection->getQueryBuilder();
94
		$insert->insert('storages_credentials')
95
			->setValue('user', $insert->createNamedParameter('user'))
96
			->setValue('identifier', $insert->createNamedParameter('identifier'))
97
			->setValue('credentials', $insert->createNamedParameter('credentials'));
98
99
		$result = $query->execute();
100
		while ($row = $result->fetch()) {
101
			$insert->setParameter('user', (string) $row['user'])
102
				->setParameter('identifier', (string) $row['identifier'])
103
				->setParameter('credentials', (string) $row['credentials']);
104
			$insert->execute();
105
		}
106
		$result->closeCursor();
107
	}
108
}
109