Migrate   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 15

5 Methods

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