Passed
Push — master ( 565d4a...492cc7 )
by Michael
07:40
created

Migrate   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A moveDoColumns() 0 13 4
A changePrefix() 0 5 3
A convertIPAddresses() 0 11 4
A preSyncActions() 0 9 1
A __construct() 0 7 1
1
<?php namespace XoopsModules\Newbb\Common;
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * Class Migrate synchronize existing tables with target schema
15
 *
16
 * @category  Migrate
17
 * @package   Newbb
18
 * @author    Richard Griffith <[email protected]>
19
 * @copyright 2016 XOOPS Project (https://xoops.org)
20
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
21
 * @link      https://xoops.org
22
 */
23
class Migrate extends \Xmf\Database\Migrate
24
{
25
    private $renameTables;
26
27
    /**
28
     * Migrate constructor.
29
     * @throws \RuntimeException
30
     * @throws \InvalidArgumentException
31
     */
32
    public function __construct()
33
    {   require_once  dirname(dirname(__DIR__)) . '/include/config.php';
34
        $config = getConfig();
35
        $this->renameTables            = $config->renameTables;
36
37
        $moduleDirName = basename(dirname(dirname(__DIR__)));
38
        parent::__construct($moduleDirName);
39
    }
40
41
    /**
42
     * change table prefix if needed
43
     */
44
    private function changePrefix()
45
    {
46
        foreach ($this->renameTables as $oldName => $newName) {
47
            if ($this->tableHandler->useTable($oldName)) {
48
                $this->tableHandler->renameTable($oldName, $newName);
49
            }
50
        }
51
    }
52
53
    /**
54
     * Change integer IPv4 column to varchar IPv6 capable
55
     *
56
     * @param string $tableName  table to convert
57
     * @param string $columnName column with IP address
58
     *
59
     * @return void
60
     */
61
    private function convertIPAddresses($tableName, $columnName)
62
    {
63
        if ($this->tableHandler->useTable($tableName)) {
64
            $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
65
            if (false !== strpos($attributes, ' int(')) {
66
                if (false === strpos($attributes, 'unsigned')) {
67
                    $this->tableHandler->alterColumn($tableName, $columnName, " bigint(16) NOT NULL  DEFAULT '0' ");
68
                    $this->tableHandler->update($tableName, [$columnName => "4294967296 + $columnName"], "WHERE $columnName < 0", false);
69
                }
70
                $this->tableHandler->alterColumn($tableName, $columnName, " varchar(45)  NOT NULL  DEFAULT '' ");
71
                $this->tableHandler->update($tableName, [$columnName => "INET_NTOA($columnName)"], '', false);
72
            }
73
        }
74
    }
75
76
    /**
77
     * Move do* columns from newbb_posts to newbb_posts_text table
78
     *
79
     * @return void
80
     */
81
    private function moveDoColumns()
82
    {
83
        $tableName    = 'newbb_posts_text';
84
        $srcTableName = 'newbb_posts';
85
        if (false !== $this->tableHandler->useTable($tableName)
86
            && false !== $this->tableHandler->useTable($srcTableName)) {
87
            $attributes = $this->tableHandler->getColumnAttributes($tableName, 'dohtml');
88
            if (false === $attributes) {
89
                $this->synchronizeTable($tableName);
90
                $updateTable = $GLOBALS['xoopsDB']->prefix($tableName);
91
                $joinTable   = $GLOBALS['xoopsDB']->prefix($srcTableName);
92
                $sql         = "UPDATE `$updateTable` t1 INNER JOIN `$joinTable` t2 ON t1.post_id = t2.post_id \n" . "SET t1.dohtml = t2.dohtml,  t1.dosmiley = t2.dosmiley, t1.doxcode = t2.doxcode\n" . '  , t1.doimage = t2.doimage, t1.dobr = t2.dobr';
93
                $this->tableHandler->addToQueue($sql);
94
            }
95
        }
96
    }
97
98
    /**
99
     * Perform any upfront actions before synchronizing the schema
100
     *
101
     * Some typical uses include
102
     *   table and column renames
103
     *   data conversions
104
     *
105
     * @return void
106
     */
107
    protected function preSyncActions()
108
    {
109
        // change 'bb' table prefix to 'newbb'
110
        $this->changePrefix();
111
        // columns dohtml, dosmiley, doxcode, doimage and dobr moved between tables as some point
112
        $this->moveDoColumns();
113
        // Convert IP address columns from int to readable varchar(45) for IPv6
114
        $this->convertIPAddresses('newbb_posts', 'poster_ip');
115
        $this->convertIPAddresses('newbb_report', 'reporter_ip');
116
    }
117
}
118