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
|
|
|
class Version0104Date20200205104800 extends SimpleMigrationStep { |
33
|
|
|
|
34
|
|
|
/** @var IDBConnection */ |
35
|
|
|
protected $connection; |
36
|
|
|
|
37
|
|
|
/** @var IConfig */ |
38
|
|
|
protected $config; |
39
|
|
|
|
40
|
|
|
/** @var array */ |
41
|
|
|
protected $childTables = [ |
42
|
|
|
'polls_comments', |
43
|
|
|
'polls_log', |
44
|
|
|
'polls_notif', |
45
|
|
|
'polls_options', |
46
|
|
|
'polls_share', |
47
|
|
|
'polls_votes', |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
public function __construct(IDBConnection $connection, IConfig $config) { |
51
|
|
|
$this->connection = $connection; |
52
|
|
|
$this->config = $config; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { |
60
|
|
|
// delete all orphaned entries by selecting all rows |
61
|
|
|
// those poll_ids are not present in the polls table |
62
|
|
|
// |
63
|
|
|
// we have to use a raw query, because NOT EXISTS is not |
64
|
|
|
// part of doctrine's expression builder |
65
|
|
|
// |
66
|
|
|
// get table prefix, as we are running a raw query |
67
|
|
|
$prefix = $this->config->getSystemValue('dbtableprefix', 'oc_'); |
68
|
|
|
// check for orphaned entries in all tables referencing |
69
|
|
|
// the main polls table |
70
|
|
|
foreach ($this->childTables as $tbl) { |
71
|
|
|
$child = "$prefix$tbl"; |
72
|
|
|
$query = "DELETE |
73
|
|
|
FROM $child |
74
|
|
|
WHERE NOT EXISTS ( |
75
|
|
|
SELECT NULL |
76
|
|
|
FROM {$prefix}polls_polls polls |
77
|
|
|
WHERE polls.id = {$child}.poll_id |
78
|
|
|
)"; |
79
|
|
|
$stmt = $this->connection->prepare($query); |
80
|
|
|
$stmt->execute(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
85
|
|
|
// add an on delete fk contraint to all tables referencing the main polls table |
86
|
|
|
/** @var ISchemaWrapper $schema */ |
87
|
|
|
$schema = $schemaClosure(); |
88
|
|
|
|
89
|
|
|
$eventTable = $schema->getTable('polls_polls'); |
90
|
|
|
foreach ($this->childTables as $tbl) { |
91
|
|
|
$table = $schema->getTable($tbl); |
92
|
|
|
|
93
|
|
|
$table->addForeignKeyConstraint($eventTable, ['poll_id'], ['id'], ['onDelete' => 'CASCADE']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $schema; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|