Passed
Push — master ( 2c7a51...0c7b86 )
by René
04:13
created

Version0106Date20201031080745   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 15
eloc 57
c 4
b 1
f 1
dl 0
loc 117
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A postSchemaChange() 0 9 4
B changeSchema() 0 39 6
A preSchemaChange() 0 31 4
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\IConfig;
28
use OCP\IDBConnection;
29
use OCP\Migration\SimpleMigrationStep;
30
use OCP\Migration\IOutput;
31
32
/**
33
 * Installation class for the polls app.
34
 * Initial db creation
35
 */
36
class Version0106Date20201031080745 extends SimpleMigrationStep {
37
38
	/** @var IDBConnection */
39
	protected $connection;
40
41
	/** @var IConfig */
42
	protected $config;
43
44
	/**
45
	 * @param IDBConnection $connection
46
	 * @param IConfig $config
47
	 */
48
	public function __construct(IDBConnection $connection, IConfig $config) {
49
		$this->connection = $connection;
50
		$this->config = $config;
51
	}
52
53
	/**
54
	 * @param IOutput $output
55
	 * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
56
	 * @param array $options
57
	 * @return null
58
	 * @since 13.0.0
59
	 */
60
	public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
61
		$schema = $schemaClosure();
62
63
		if ($schema->hasTable('polls_preferences')) {
64
65
			// remove preferences with empty user_id from oc_polls_preferences
66
			$query = $this->connection->getQueryBuilder();
67
			$query->delete('polls_preferences')
68
				->where('user_id = :userId')
69
				->setParameter('userId', '');
70
			$query->execute();
71
72
			// remove duplicate preferences from oc_polls_preferences
73
			// preserve the last user setting in the db
74
			$query = $this->connection->getQueryBuilder();
75
			$query->select('id', 'user_id')
76
				->from('polls_preferences');
77
			$users = $query->execute();
78
79
			$delete = $this->connection->getQueryBuilder();
80
			$delete->delete('polls_preferences')
81
				->where('id = :id');
82
83
			$userskeep = [];
84
85
			while ($row = $users->fetch()) {
86
				if (in_array($row['user_id'], $userskeep)) {
87
					$delete->setParameter('id', $row['id']);
88
					$delete->execute();
89
				} else {
90
					$userskeep[] = $row['user_id'];
91
				}
92
			}
93
		}
94
	}
95
96
	/**
97
	 * @param IOutput $output
98
	 * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
99
	 * @param array $options
100
	 * @return null|ISchemaWrapper
101
	 * @since 13.0.0
102
	 */
103
	public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
104
		/** @var ISchemaWrapper $schema */
105
		$schema = $schemaClosure();
106
		if ($schema->hasTable('polls_share')) {
107
			$table = $schema->getTable('polls_share');
108
109
			if (!$table->hasColumn('display_name')) {
110
				$table->addColumn('display_name', 'string', [
111
					'notnull' => false,
112
					'length' => 64,
113
					'default' => ''
114
				]);
115
			}
116
117
			if (!$table->hasColumn('email_address')) {
118
				$table->addColumn('email_address', 'string', [
119
					'notnull' => false,
120
					'length' => 254,
121
					'default' => ''
122
				]);
123
			}
124
		}
125
126
		if ($schema->hasTable('polls_preferences')) {
127
			$table = $schema->getTable('polls_preferences');
128
			$table->changeColumn('user_id', [
129
				'default' => ''
130
			]);
131
			$table->changeColumn('preferences', [
132
				'notnull' => false
133
			]);
134
			try {
135
				$table->addUniqueIndex(['user_id']);
136
			} catch (\Exception $e) {
137
				//catch silently, index is already present
138
			}
139
		}
140
141
		return $schema;
142
	}
143
144
	public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
145
		$schema = $schemaClosure();
146
		if ($schema->hasTable('polls_share')) {
147
			$table = $schema->getTable('polls_share');
148
			if ($table->hasColumn('email_address') && $table->hasColumn('user_email')) {
149
				$query = $this->connection->getQueryBuilder();
150
				$query->update('polls_share')
151
					->set('email_address', 'user_email');
152
				$query->execute();
153
			}
154
		}
155
	}
156
}
157