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

Version0017Date20200202112901::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 Version0017Date20200202112901 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_mb_tmp');
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
90
//			$table->addColumn(
91
//				'instance', 'string', [
92
//							  'notnull' => false,
93
//							  'length'  => 255,
94
//						  ]
95
//			);
96
//			$table->setPrimaryKey(['circle_id', 'user_id', 'user_type', 'instance']);
97
98
		$table->addColumn(
99
			'user_type', 'smallint', [
100
						   'notnull' => true,
101
						   'length'  => 1,
102
						   'default' => 1,
103
					   ]
104
		);
105
		$table->addColumn(
106
			'level', 'smallint', [
107
					   'notnull' => true,
108
					   'length'  => 1,
109
				   ]
110
		);
111
		$table->addColumn(
112
			'status', 'string', [
113
						'notnull' => false,
114
						'length'  => 15,
115
					]
116
		);
117
		$table->addColumn(
118
			'note', 'string', [
119
					  'notnull' => false,
120
					  'length'  => 255,
121
				  ]
122
		);
123
		$table->addColumn(
124
			'joined', 'datetime', [
125
						'notnull' => false,
126
					]
127
		);
128
		$table->addColumn(
129
			'member_id', Type::STRING, [
130
						   'notnull' => false,
131
						   'length'  => 15,
132
					   ]
133
		);
134
		$table->addColumn(
135
			'contact_meta', 'string', [
136
							  'notnull' => false,
137
							  'length'  => 1000,
138
						  ]
139
		);
140
		$table->addColumn(
141
			'contact_checked', Type::SMALLINT, [
142
								 'notnull' => false,
143
								 'length'  => 1,
144
							 ]
145
		);
146
		$table->addColumn(
147
			'contact_id', 'string', [
148
							'notnull' => false,
149
							'length'  => 127,
150
						]
151
		);
152
153
		return $schema;
154
	}
155
156
	/**
157
	 * @param IOutput $output
158
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
159
	 * @param array $options
160
	 */
161
	public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
162
		$this->copyTable('circles_members', 'circles_mb_tmp');
163
	}
164
165
166 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...
167
		$connection = \OC::$server->getDatabaseConnection();
168
		$qb = $connection->getQueryBuilder();
169
170
		$qb->select('*')
171
		   ->from($orig);
172
173
		$result = $qb->execute();
174
		while ($row = $result->fetch()) {
175
176
			$copy = $connection->getQueryBuilder();
177
			$copy->insert($dest);
178
			$ak = array_keys($row);
179
			foreach ($ak as $k) {
180
				if ($row[$k] !== null) {
181
					$copy->setValue($k, $copy->createNamedParameter($row[$k]));
182
				}
183
			}
184
			$copy->execute();
185
		}
186
187
	}
188
189
}
190