Completed
Push — master ( 32ee8c...e48760 )
by Maxence
01:38
created

Version0017Date20200202112903::copyTable()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 9.568
c 0
b 0
f 0
cc 4
nc 4
nop 2
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2019
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
declare(strict_types=1);
28
29
namespace OCA\Circles\Migration;
30
31
use Closure;
32
use Doctrine\DBAL\Schema\SchemaException;
33
use Doctrine\DBAL\Types\Type;
34
use OCP\DB\ISchemaWrapper;
35
use OCP\IDBConnection;
36
use OCP\Migration\IOutput;
37
use OCP\Migration\SimpleMigrationStep;
38
39
/**
40
 * Auto-generated migration step: Please modify to your needs!
41
 */
42
class Version0017Date20200202112903 extends SimpleMigrationStep {
43
44
45
	/** @var IDBConnection */
46
	private $connection;
47
48
49
	/**
50
	 * @param IDBConnection $connection
51
	 */
52
	public function __construct(IDBConnection $connection) {
53
		$this->connection = $connection;
54
	}
55
56
	/**
57
	 * @param IOutput $output
58
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
59
	 * @param array $options
60
	 */
61
	public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
62
	}
63
64
	/**
65
	 * @param IOutput $output
66
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
67
	 * @param array $options
68
	 *
69
	 * @return null|ISchemaWrapper
70
	 * @throws SchemaException
71
	 */
72
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
73
		/** @var ISchemaWrapper $schema */
74
		$schema = $schemaClosure();
75
76
		$table = $schema->createTable('circles_members');
77
		$table->addColumn(
78
			'circle_id', 'string', [
79
						   'notnull' => true,
80
						   'length'  => 64,
81
					   ]
82
		);
83
		$table->addColumn(
84
			'user_id', 'string', [
85
						 'notnull' => true,
86
						 'length'  => 128,
87
					 ]
88
		);
89
		$table->addColumn(
90
			'instance', 'string', [
91
						  'notnull' => false,
92
						  'length'  => 255,
93
					  ]
94
		);
95
		$table->addColumn(
96
			'user_type', 'smallint', [
97
						   'notnull' => true,
98
						   'length'  => 1,
99
						   'default' => 1,
100
					   ]
101
		);
102
		$table->addColumn(
103
			'level', 'smallint', [
104
					   'notnull' => true,
105
					   'length'  => 1,
106
				   ]
107
		);
108
		$table->addColumn(
109
			'status', 'string', [
110
						'notnull' => false,
111
						'length'  => 15,
112
					]
113
		);
114
		$table->addColumn(
115
			'note', 'string', [
116
					  'notnull' => false,
117
					  'length'  => 255,
118
				  ]
119
		);
120
		$table->addColumn(
121
			'joined', 'datetime', [
122
						'notnull' => false,
123
					]
124
		);
125
		$table->addColumn(
126
			'member_id', Type::STRING, [
127
						   'notnull' => false,
128
						   'length'  => 15,
129
					   ]
130
		);
131
		$table->addColumn(
132
			'contact_meta', 'string', [
133
							  'notnull' => false,
134
							  'length'  => 1000,
135
						  ]
136
		);
137
		$table->addColumn(
138
			'contact_checked', Type::SMALLINT, [
139
								 'notnull' => false,
140
								 'length'  => 1,
141
							 ]
142
		);
143
		$table->addColumn(
144
			'contact_id', 'string', [
145
							'notnull' => false,
146
							'length'  => 127,
147
						]
148
		);
149
150
		$table->setPrimaryKey(['circle_id', 'user_id', 'user_type', 'contact_id', 'instance']);
151
152
		return $schema;
153
	}
154
155
	/**
156
	 * @param IOutput $output
157
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
158
	 * @param array $options
159
	 */
160
	public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
161
		$this->copyTable('circles_mb_tmp', 'circles_members');
162
163
		$this->updateMemberId();
164
		$this->updateTokens();
165
	}
166
167
168
	/**
169
	 *
170
	 */
171
	private function updateMemberId() {
172
		$qb = $this->connection->getQueryBuilder();
173
		$expr = $qb->expr();
174
175
		$orX = $expr->orX();
176
		$orX->add($expr->eq('member_id', $qb->createNamedParameter('')));
177
		$orX->add($expr->isNull('member_id'));
178
179
		$qb->select('circle_id', 'user_id', 'user_type')
180
		   ->from('circles_members')
181
		   ->where($orX);
182
183
		$result = $qb->execute();
184
		while ($row = $result->fetch()) {
185
			$uniqueId = substr(bin2hex(openssl_random_pseudo_bytes(24)), 0, 15);
186
187
			$update = $this->connection->getQueryBuilder();
188
			$expru = $update->expr();
189
			$update->update('circles_members')
190
				   ->set('member_id', $update->createNamedParameter($uniqueId))
191
				   ->where($expru->eq('circle_id', $update->createNamedParameter($row['circle_id'])))
192
				   ->andWhere($expru->eq('user_id', $update->createNamedParameter($row['user_id'])))
193
				   ->andWhere($expru->eq('user_type', $update->createNamedParameter($row['user_type'])));
194
195
			$update->execute();
196
		}
197
	}
198
199
200
	/**
201
	 *
202
	 */
203
	private function updateTokens() {
204
		$qb = $this->connection->getQueryBuilder();
205
		$expr = $qb->expr();
206
		$orX = $expr->orX();
207
		$orX->add($expr->eq('member_id', $qb->createNamedParameter('')));
208
		$orX->add($expr->isNull('member_id'));
209
		$qb->select('user_id', 'circle_id')
210
		   ->from('circles_tokens')
211
		   ->where($orX);
212
213
		$result = $qb->execute();
214
		while ($row = $result->fetch()) {
215
			$qbm = $this->connection->getQueryBuilder();
216
			$exprm = $qbm->expr();
217
218
			$qbm->select('member_id')
219
				->from('circles_members')
220
				->where($exprm->eq('circle_id', $qbm->createNamedParameter($row['circle_id'])))
221
				->andWhere($exprm->eq('user_id', $qbm->createNamedParameter($row['user_id'])))
222
				->andWhere($exprm->neq('user_type', $qbm->createNamedParameter('1')));
223
224
			$resultm = $qbm->execute();
225
			$member = $resultm->fetch();
226
227
			$update = $this->connection->getQueryBuilder();
228
			$expru = $update->expr();
229
			$update->update('circles_tokens')
230
				   ->set('member_id', $update->createNamedParameter($member['member_id']))
231
				   ->where($expru->eq('circle_id', $update->createNamedParameter($row['circle_id'])))
232
				   ->andWhere($expru->eq('user_id', $update->createNamedParameter($row['user_id'])));
233
234
			$update->execute();
235
		}
236
	}
237
238
239 View Code Duplication
	protected function copyTable($orig, $dest) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
240
		$connection = \OC::$server->getDatabaseConnection();
241
		$qb = $connection->getQueryBuilder();
242
243
		$qb->select('*')
244
		   ->from($orig);
245
246
		$result = $qb->execute();
247
		while ($row = $result->fetch()) {
248
249
			$copy = $connection->getQueryBuilder();
250
			$copy->insert($dest);
251
			$ak = array_keys($row);
252
			foreach ($ak as $k) {
253
				if ($row[$k] !== null) {
254
					$copy->setValue($k, $copy->createNamedParameter($row[$k]));
255
				}
256
			}
257
			$copy->execute();
258
		}
259
260
	}
261
262
263
}
264