Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
user_notifications_table_remove_duplicates
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 3
20
0.00% covered (danger)
0.00%
0 / 1
 depends_on
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 update_data
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 remove_duplicates
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\db\migration\data\v32x;
15
16class user_notifications_table_remove_duplicates extends \phpbb\db\migration\migration
17{
18    public static function depends_on()
19    {
20        return array(
21            '\phpbb\db\migration\data\v32x\user_notifications_table_temp_index',
22        );
23    }
24
25    public function update_data()
26    {
27        return array(
28            array('custom', array(array($this, 'remove_duplicates'))),
29        );
30    }
31
32    public function remove_duplicates()
33    {
34        $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->table_prefix . 'user_notifications');
35
36        $sql = "SELECT item_type, item_id, user_id, method, MAX(notify) AS notify
37                FROM {$this->table_prefix}user_notifications
38                GROUP BY item_type, item_id, user_id, method
39                HAVING COUNT(item_type) > 1";
40
41        $result = $this->sql_query($sql);
42        while ($row = $this->db->sql_fetchrow($result))
43        {
44            // Delete the duplicate entries
45            $this->sql_query("DELETE FROM {$this->table_prefix}user_notifications
46                                WHERE user_id = {$row['user_id']}
47                                  AND item_type = '{$row['item_type']}'
48                                  AND method = '{$row['method']}'");
49
50            // And re-insert as a single one
51            $insert_buffer->insert($row);
52        }
53        $this->db->sql_freeresult($result);
54    }
55}