Migrate::moveDoColumns()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 13
rs 9.9
cc 4
nc 3
nop 0
1
<?php
2
3
namespace XoopsModules\Groupmanager\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 RuntimeException;
16
17
18
/**
19
 * Class Migrate synchronize existing tables with target schema
20
 *
21
 * @category  Migrate
22
 * @author    Richard Griffith <[email protected]>
23
 * @copyright 2016 XOOPS Project (https://xoops.org)
24
 * @license   GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
25
 * @link      https://xoops.org
26
 */
27
class Migrate extends \Xmf\Database\Migrate
28
{
29
    private $renameTables;
30
31
    /**
32
     * Migrate constructor.
33
     * @throws \RuntimeException
34
     * @throws \InvalidArgumentException
35
     */
36
    public function __construct()
37
    {
38
        $class = __NAMESPACE__ . '\\' . 'Configurator';
39
        if (!\class_exists($class)) {
40
            throw new RuntimeException("Class '$class' not found");
41
        }
42
        $configurator       = new $class();
43
        $this->renameTables = $configurator->renameTables;
44
45
        $moduleDirName = \basename(\dirname(__DIR__, 2));
46
        parent::__construct($moduleDirName);
47
    }
48
49
    /**
50
     * change table prefix if needed
51
     */
52
    private function changePrefix()
0 ignored issues
show
Unused Code introduced by
The method changePrefix() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
53
    {
54
        foreach ($this->renameTables as $oldName => $newName) {
55
            if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
56
                $this->tableHandler->renameTable($oldName, $newName);
57
            }
58
        }
59
    }
60
61
    /**
62
     * Change integer IPv4 column to varchar IPv6 capable
63
     *
64
     * @param string $tableName  table to convert
65
     * @param string $columnName column with IP address
66
     */
67
    private function convertIPAddresses($tableName, $columnName)
0 ignored issues
show
Unused Code introduced by
The method convertIPAddresses() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
68
    {
69
        if ($this->tableHandler->useTable($tableName)) {
70
            $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
71
            if (false !== mb_strpos($attributes, ' int(')) {
0 ignored issues
show
Bug introduced by
It seems like $attributes can also be of type boolean; however, parameter $haystack of mb_strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
            if (false !== mb_strpos(/** @scrutinizer ignore-type */ $attributes, ' int(')) {
Loading history...
72
                if (false === mb_strpos($attributes, 'unsigned')) {
73
                    $this->tableHandler->alterColumn($tableName, $columnName, " bigint(16) NOT NULL  DEFAULT '0' ");
74
                    $this->tableHandler->update($tableName, [$columnName => "4294967296 + $columnName"], "WHERE $columnName < 0", false);
75
                }
76
                $this->tableHandler->alterColumn($tableName, $columnName, " varchar(45)  NOT NULL  DEFAULT '' ");
77
                $this->tableHandler->update($tableName, [$columnName => "INET_NTOA($columnName)"], '', false);
78
            }
79
        }
80
    }
81
82
    /**
83
     * Move do* columns from newbb_posts to newbb_posts_text table
84
     */
85
    private function moveDoColumns()
0 ignored issues
show
Unused Code introduced by
The method moveDoColumns() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
86
    {
87
        $tableName    = 'newbb_posts_text';
88
        $srcTableName = 'newbb_posts';
89
        if ($this->tableHandler->useTable($tableName)
90
            && $this->tableHandler->useTable($srcTableName)) {
91
            $attributes = $this->tableHandler->getColumnAttributes($tableName, 'dohtml');
92
            if (false === $attributes) {
93
                $this->synchronizeTable($tableName);
94
                $updateTable = $GLOBALS['xoopsDB']->prefix($tableName);
95
                $joinTable   = $GLOBALS['xoopsDB']->prefix($srcTableName);
96
                $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';
97
                $this->tableHandler->addToQueue($sql);
98
            }
99
        }
100
    }
101
102
    /**
103
     * Perform any upfront actions before synchronizing the schema
104
     *
105
     * Some typical uses include
106
     *   table and column renames
107
     *   data conversions
108
     */
109
    protected function preSyncActions()
110
    {
111
        /*
112
        // change 'bb' table prefix to 'newbb'
113
        $this->changePrefix();
114
        // columns dohtml, dosmiley, doxcode, doimage and dobr moved between tables as some point
115
        $this->moveDoColumns();
116
        // Convert IP address columns from int to readable varchar(45) for IPv6
117
        $this->convertIPAddresses('newbb_posts', 'poster_ip');
118
        $this->convertIPAddresses('newbb_report', 'reporter_ip');
119
        */
120
    }
121
}
122