Passed
Push — master ( 6499a1...73e8f6 )
by Michael
36s queued 12s
created

class/Common/Migrate.php (1 issue)

Labels
Severity
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 Configurator $configurator
34
     * @throws \RuntimeException
35
     * @throws \InvalidArgumentException
36
     */
37
    public function __construct(Configurator $configurator = null)
38
    {
39
        if (null !== $configurator) {
40
            $this->renameTables = $configurator->renameTables;
41
42
            $moduleDirName = \basename(\dirname(__DIR__, 2));
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
            if (false !== mb_strpos($attributes, ' int(')) {
0 ignored issues
show
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

69
            if (false !== mb_strpos(/** @scrutinizer ignore-type */ $attributes, ' int(')) {
Loading history...
70
                if (false === mb_strpos($attributes, 'unsigned')) {
71
                    $this->tableHandler->alterColumn($tableName, $columnName, " bigint(16) NOT NULL  DEFAULT '0' ");
72
                    $this->tableHandler->update($tableName, [$columnName => "4294967296 + $columnName"], "WHERE $columnName < 0", false);
73
                }
74
                $this->tableHandler->alterColumn($tableName, $columnName, " varchar(45)  NOT NULL  DEFAULT '' ");
75
                $this->tableHandler->update($tableName, [$columnName => "INET_NTOA($columnName)"], '', false);
76
            }
77
        }
78
    }
79
80
    /**
81
     * Move do* columns from newbb_posts to newbb_posts_text table
82
     */
83
    private function moveDoColumns()
84
    {
85
//        $tableName    = 'newbb_posts_text';
86
//        $srcTableName = 'newbb_posts';
87
//        if ($this->tableHandler->useTable($tableName)
88
//            && $this->tableHandler->useTable($srcTableName)) {
89
//            $attributes = $this->tableHandler->getColumnAttributes($tableName, 'dohtml');
90
//            if (false === $attributes) {
91
//                $this->synchronizeTable($tableName);
92
//                $updateTable = $GLOBALS['xoopsDB']->prefix($tableName);
93
//                $joinTable   = $GLOBALS['xoopsDB']->prefix($srcTableName);
94
//                $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';
95
//                $this->tableHandler->addToQueue($sql);
96
//            }
97
//        }
98
    }
99
100
    /**
101
     * Perform any upfront actions before synchronizing the schema
102
     *
103
     * Some typical uses include
104
     *   table and column renames
105
     *   data conversions
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