Passed
Pull Request — master (#1219)
by René
04:40
created

Version0106Date20201031080745::preSchemaChange()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 34
rs 9.536
cc 4
nc 4
nop 3
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
		$prefix = $this->config->getSystemValue('dbtableprefix', 'oc_');
0 ignored issues
show
Unused Code introduced by
The assignment to $prefix is dead and can be removed.
Loading history...
63
		$userId = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $userId is dead and can be removed.
Loading history...
64
65
		if ($schema->hasTable('polls_preferences')) {
66
67
			// remove preferences with empty user_id from oc_polls_preferences
68
			$query = $this->connection->getQueryBuilder();
69
			$query->delete('polls_preferences')
70
				->where('user_id = :userId')
71
				->setParameter('userId', '');
72
			$query->execute();
73
74
			// remove duplicate preferences from oc_polls_preferences
75
			// preserve the last user setting in the db
76
			$query = $this->connection->getQueryBuilder();
77
			$query->select('id', 'user_id')
78
				->from('polls_preferences');
79
			$users = $query->execute();
80
81
			$delete = $this->connection->getQueryBuilder();
82
			$delete->delete('polls_preferences')
83
				->where('id = :id');
84
85
			$userskeep = [];
86
87
			while ($row = $users->fetch()) {
88
				if (in_array($row['user_id'], $userskeep)) {
89
					$delete->setParameter('id', $row['id']);
90
					$delete->execute();
91
				} else {
92
					$userskeep[] = $row['user_id'];
93
					\OC::$server->getLogger()->alert('save ' . json_encode($row));
94
				}
95
			}
96
		}
97
	}
98
99
	/**
100
	 * @param IOutput $output
101
	 * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
102
	 * @param array $options
103
	 * @return null|ISchemaWrapper
104
	 * @since 13.0.0
105
	 */
106
	public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
107
		/** @var ISchemaWrapper $schema */
108
		$schema = $schemaClosure();
109
		if ($schema->hasTable('polls_share')) {
110
			$table = $schema->getTable('polls_share');
111
112
			if (!$table->hasColumn('display_name')) {
113
				$table->addColumn('display_name', 'string', [
114
					'notnull' => false,
115
					'length' => 64,
116
					'default' => ''
117
				]);
118
			}
119
120
			if (!$table->hasColumn('email_address')) {
121
				$table->addColumn('email_address', 'string', [
122
					'notnull' => false,
123
					'length' => 254,
124
					'default' => ''
125
				]);
126
			}
127
		}
128
129
		if ($schema->hasTable('polls_preferences')) {
130
			$table = $schema->getTable('polls_preferences');
131
			$table->addUniqueIndex(['user_id']);
132
		}
133
134
		return $schema;
135
	}
136
137
	public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
138
		$schema = $schemaClosure();
139
		if ($schema->hasTable('polls_share')) {
140
			$table = $schema->getTable('polls_share');
141
			if ($table->hasColumn('email_address') && $table->hasColumn('user_email')) {
142
				$query = $this->connection->getQueryBuilder();
143
				$query->update('polls_share')
144
					->set('email_address', 'user_email');
145
				$query->execute();
146
			}
147
		}
148
	}
149
}
150