Passed
Pull Request — master (#1284)
by René
09:49 queued 04:22
created

Version0107Date20201217071304::removeDuplicates()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 26
c 1
b 0
f 1
dl 0
loc 40
rs 9.504
cc 4
nc 4
nop 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 René Gieling <[email protected]>
4
 *
5
 * @author René Gieling <[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
namespace OCA\Polls\Migration;
25
26
use OCP\DB\ISchemaWrapper;
27
use OCP\Migration\IOutput;
28
use OCP\IDBConnection;
29
use OCP\Migration\SimpleMigrationStep;
30
use Doctrine\DBAL\Schema\SchemaException;
31
32
class Version0107Date20201217071304 extends SimpleMigrationStep {
33
34
	/** @var IDBConnection */
35
	protected $connection;
36
37
	public function __construct(IDBConnection $connection) {
38
		$this->connection = $connection;
39
	}
40
41
	/**
42
	 * @return void
43
	 */
44
	public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
45
		// $schema = $schemaClosure();
46
		//
47
		// if (!$schema->hasTable('polls_share')) {
48
		// 	return;
49
		// }
50
		// $query = $this->connection->getQueryBuilder();
51
		//
52
		// // make sure, all public shares fit to the unique index added in schemaChange(),
53
		// // by copying token to user_id
54
		// $query->update('polls_share')
55
		// 	->set('user_id', 'token')
56
		// 	->where('type = :type')
57
		// 	->setParameter('type', 'public')
58
		// 	->execute();
59
		//
60
		// // remove duplicates from oc_polls_share
61
		// // preserve the first entry
62
		// $query = $this->connection->getQueryBuilder();
63
		// $query->select('id', 'type', 'poll_id', 'user_id')
64
		// 	->from('polls_share');
65
		// $foundEntries = $query->execute();
66
		//
67
		// $delete = $this->connection->getQueryBuilder();
68
		// $delete->delete('polls_share')->where('id = :id');
69
		//
70
		// $entries2Keep = [];
71
		//
72
		// while ($row = $foundEntries->fetch()) {
73
		// 	$currentRecord = [
74
		// 		$row['poll_id'],
75
		// 		$row['type'],
76
		// 		$row['user_id']
77
		// 	];
78
		//
79
		// 	if (in_array($currentRecord, $entries2Keep)) {
80
		// 		$delete->setParameter('id', $row['id']);
81
		// 		$delete->execute();
82
		// 	} else {
83
		// 		$entries2Keep[] = $currentRecord;
84
		// 	}
85
		// }
86
	}
87
88
	public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
89
		$this->removeDuplicates($schemaClosure);
90
91
		/** @var ISchemaWrapper $schema */
92
		$schema = $schemaClosure();
93
94
		if ($schema->hasTable('polls_share')) {
95
			$table = $schema->getTable('polls_share');
96
			$table->changeColumn('user_id', [
97
				'notnull' => true,
98
				'default' => ''
99
			]);
100
101
			try {
102
				$table->addUniqueIndex(['poll_id', 'user_id'], 'UNIQ_shares');
103
			} catch (SchemaException $e) {
104
				//catch silently, index is already present
105
			}
106
		}
107
		return $schema;
108
	}
109
110
	public function removeDuplicates(\Closure $schemaClosure) {
111
		$schema = $schemaClosure();
112
113
		if (!$schema->hasTable('polls_share')) {
114
			return;
115
		}
116
		$query = $this->connection->getQueryBuilder();
117
118
		// make sure, all public shares fit to the unique index added in schemaChange(),
119
		// by copying token to user_id
120
		$query->update('polls_share')
121
			->set('user_id', 'token')
122
			->where('type = :type')
123
			->setParameter('type', 'public')
124
			->execute();
125
126
		// remove duplicates from oc_polls_share
127
		// preserve the first entry
128
		$query = $this->connection->getQueryBuilder();
129
		$query->select('id', 'type', 'poll_id', 'user_id')
130
			->from('polls_share');
131
		$foundEntries = $query->execute();
132
133
		$delete = $this->connection->getQueryBuilder();
134
		$delete->delete('polls_share')->where('id = :id');
135
136
		$entries2Keep = [];
137
138
		while ($row = $foundEntries->fetch()) {
139
			$currentRecord = [
140
				$row['poll_id'],
141
				$row['type'],
142
				$row['user_id']
143
			];
144
145
			if (in_array($currentRecord, $entries2Keep)) {
146
				$delete->setParameter('id', $row['id']);
147
				$delete->execute();
148
			} else {
149
				$entries2Keep[] = $currentRecord;
150
			}
151
		}
152
	}
153
154
155
156
}
157