Passed
Pull Request — master (#823)
by René
04:05
created

Version0104Date20200205104800::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
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\DB\QueryBuilder\IQueryBuilder;
28
use OCP\IConfig;
29
use OCP\IDBConnection;
30
use OCP\Migration\SimpleMigrationStep;
31
use OCP\Migration\IOutput;
32
33
/**
34
 * Installation class for the polls app.
35
 * Initial db creation
36
 */
37
class Version0104Date20200205104800 extends SimpleMigrationStep {
38
39
	/** @var IDBConnection */
40
	protected $connection;
41
42
	/** @var IConfig */
43
	protected $config;
44
45
    /** @var array */
46
    protected $childTables = [
47
        'polls_comments',
48
        'polls_log',
49
        'polls_notif',
50
        'polls_options',
51
        'polls_share',
52
        'polls_votes',
53
    ];
54
55
	/**
56
	 * @param IDBConnection $connection
57
	 * @param IConfig $config
58
	 */
59
	public function __construct(IDBConnection $connection, IConfig $config) {
60
		$this->connection = $connection;
61
		$this->config = $config;
62
	}
63
64
65
    /**
66
     * @param IOutput $output
67
     * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
68
     * @param array $options
69
     * @return null
70
     * @since 13.0.0
71
     */
72
    public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
73
        // delete all orphaned entries by selecting all rows
74
        // those poll_ids are not present in the polls table
75
        //
76
        // we have to use a raw query, because NOT EXISTS is not
77
        // part of doctrine's expression builder
78
        //
79
        // get table prefix, as we are running a raw query
80
        $prefix = $this->config->getSystemValue('dbtableprefix', 'oc_');
81
        // check for orphaned entries in all tables referencing
82
        // the main polls table
83
        foreach($this->childTables as $tbl) {
84
            $child = "$prefix$tbl";
85
            $query = "DELETE
86
                FROM $child
87
                WHERE NOT EXISTS (
88
                    SELECT NULL
89
                    FROM {$prefix}polls_polls polls
90
                    WHERE polls.id = {$child}.poll_id
91
                )";
92
            $stmt = $this->connection->prepare($query);
93
            $stmt->execute();
94
        }
95
    }
96
97
	/**
98
	 * @param IOutput $output
99
	 * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
100
	 * @param array $options
101
	 * @return null|ISchemaWrapper
102
	 * @since 13.0.0
103
	 */
104
	public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
105
        // add an on delete fk contraint to all tables referencing the main polls table
106
		/** @var ISchemaWrapper $schema */
107
		$schema = $schemaClosure();
108
109
        $eventTable = $schema->getTable('polls_polls');
110
        foreach($this->childTables as $tbl) {
111
            $table = $schema->getTable($tbl);
112
113
            $table->addForeignKeyConstraint($eventTable, ['poll_id'], ['id'], ['onDelete' => 'CASCADE']);
114
        }
115
116
		return $schema;
117
	}
118
}
119