Migrate::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Xoopstube\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\Xoopstube\Common;
16
//use  \Xmf\Database\Migrate;
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
     * @param \XoopsModules\Xoopstube\Common\Configurator|null $configurator
34
     */
35
    public function __construct(Configurator $configurator = null)
36
    {
37
        if (null !== $configurator) {
38
            $this->renameTables = $configurator->renameTables;
39
40
            $moduleDirName = \basename(\dirname(__DIR__, 2));
41
            parent::__construct($moduleDirName);
42
        }
43
    }
44
45
    /**
46
     * change table prefix if needed
47
     */
48
    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...
49
    {
50
        foreach ($this->renameTables as $oldName => $newName) {
51
            if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
52
                $this->tableHandler->renameTable($oldName, $newName);
53
            }
54
        }
55
    }
56
57
    /**
58
     * Change integer IPv4 column to varchar IPv6 capable
59
     *
60
     * @param string $tableName  table to convert
61
     * @param string $columnName column with IP address
62
     */
63
    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...
64
    {
65
        if ($this->tableHandler->useTable($tableName)) {
66
            $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
67
            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

67
            if (false !== mb_strpos(/** @scrutinizer ignore-type */ $attributes, ' int(')) {
Loading history...
68
                if (false === mb_strpos($attributes, 'unsigned')) {
69
                    $this->tableHandler->alterColumn($tableName, $columnName, " bigint(16) NOT NULL  DEFAULT '0' ");
70
                    $this->tableHandler->update($tableName, [$columnName => "4294967296 + $columnName"], "WHERE $columnName < 0", false);
71
                }
72
                $this->tableHandler->alterColumn($tableName, $columnName, " varchar(45)  NOT NULL  DEFAULT '' ");
73
                $this->tableHandler->update($tableName, [$columnName => "INET_NTOA($columnName)"], '', false);
74
            }
75
        }
76
    }
77
78
    /**
79
     * Move do* columns from newbb_posts to newbb_posts_text table
80
     */
81
    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...
82
    {
83
//        $tableName    = 'newbb_posts_text';
84
//        $srcTableName = 'newbb_posts';
85
//        if ($this->tableHandler->useTable($tableName)
86
//            && $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
    protected function preSyncActions()
106
    {
107
        // change 'bb' table prefix to 'newbb'
108
//        $this->changePrefix();
109
        // columns dohtml, dosmiley, doxcode, doimage and dobr moved between tables as some point
110
//        $this->moveDoColumns();
111
        // Convert IP address columns from int to readable varchar(45) for IPv6
112
//        $this->convertIPAddresses('newbb_posts', 'poster_ip');
113
//        $this->convertIPAddresses('newbb_report', 'reporter_ip');
114
    }
115
}
116