Passed
Push — master ( 762245...66eec6 )
by Michael
02:15
created

Migrate::moveDoColumns()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 13
rs 9.9
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php namespace XoopsModules\Extcal\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
use XoopsModules\Extcal;
14
15
/**
16
 * Class Migrate synchronize existing tables with target schema
17
 *
18
 * @category  Migrate
19
 * @author    Richard Griffith <[email protected]>
20
 * @copyright 2016 XOOPS Project (https://xoops.org)
21
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22
 * @link      https://xoops.org
23
 */
24
class Migrate extends \Xmf\Database\Migrate
0 ignored issues
show
Bug introduced by
The type Xmf\Database\Migrate was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
{
26
    private $renameTables;
27
28
    /**
29
     * Migrate constructor.
30
     * @throws \RuntimeException
31
     * @throws \InvalidArgumentException
32
     */
33
    public function __construct(Extcal\Common\Configurator  $configurator)
34
    {   require_once  dirname(dirname(__DIR__)) . '/include/config.php';
35
//        $config = getConfig();
36
        $this->renameTables            = $configurator->renameTables;
37
38
        $moduleDirName = basename(dirname(dirname(__DIR__)));
39
        parent::__construct($moduleDirName);
40
    }
41
42
    /**
43
     * change table prefix if needed
44
     */
45
    private function changePrefix()
46
    {
47
        foreach ($this->renameTables as $oldName => $newName) {
48
            if ($this->tableHandler->useTable($oldName)) {
49
                $this->tableHandler->renameTable($oldName, $newName);
50
            }
51
        }
52
    }
53
54
    /**
55
     * Change integer IPv4 column to varchar IPv6 capable
56
     *
57
     * @param string $tableName  table to convert
58
     * @param string $columnName column with IP address
59
     *
60
     * @return void
61
     */
62
    private function convertIPAddresses($tableName, $columnName)
63
    {
64
        if ($this->tableHandler->useTable($tableName)) {
65
            $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
66
            if (false !== strpos($attributes, ' int(')) {
67
                if (false === strpos($attributes, 'unsigned')) {
68
                    $this->tableHandler->alterColumn($tableName, $columnName, " bigint(16) NOT NULL  DEFAULT '0' ");
69
                    $this->tableHandler->update($tableName, [$columnName => "4294967296 + $columnName"], "WHERE $columnName < 0", false);
70
                }
71
                $this->tableHandler->alterColumn($tableName, $columnName, " varchar(45)  NOT NULL  DEFAULT '' ");
72
                $this->tableHandler->update($tableName, [$columnName => "INET_NTOA($columnName)"], '', false);
73
            }
74
        }
75
    }
76
77
    /**
78
     * Move do* columns from tdmmoney_posts to tdmmoney_posts_text table
79
     *
80
     * @return void
81
     */
82
    private function moveDoColumns()
83
    {
84
        $tableName    = 'tdmmoney_posts_text';
85
        $srcTableName = 'tdmmoney_posts';
86
        if (false !== $this->tableHandler->useTable($tableName)
87
            && false !== $this->tableHandler->useTable($srcTableName)) {
88
            $attributes = $this->tableHandler->getColumnAttributes($tableName, 'dohtml');
89
            if (false === $attributes) {
90
                $this->synchronizeTable($tableName);
91
                $updateTable = $GLOBALS['xoopsDB']->prefix($tableName);
92
                $joinTable   = $GLOBALS['xoopsDB']->prefix($srcTableName);
93
                $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';
94
                $this->tableHandler->addToQueue($sql);
95
            }
96
        }
97
    }
98
99
    /**
100
     * Perform any upfront actions before synchronizing the schema
101
     *
102
     * Some typical uses include
103
     *   table and column renames
104
     *   data conversions
105
     *
106
     * @return void
107
     */
108
    protected function preSyncActions()
109
    {
110
        // change 'bb' table prefix to 'tdmmoney'
111
        $this->changePrefix();
112
        // columns dohtml, dosmiley, doxcode, doimage and dobr moved between tables as some point
113
        $this->moveDoColumns();
114
        // Convert IP address columns from int to readable varchar(45) for IPv6
115
        $this->convertIPAddresses('tdmmoney_posts', 'poster_ip');
116
        $this->convertIPAddresses('tdmmoney_report', 'reporter_ip');
117
    }
118
}
119