Migrate   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 84
rs 10
c 1
b 0
f 0
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A moveDoColumns() 0 13 4
A __construct() 0 11 2
A convertIPAddresses() 0 11 4
A preSyncActions() 0 2 1
A changePrefix() 0 5 4
1
<?php
2
3
namespace XoopsModules\Xfguestbook\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
/**
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 (https://www.gnu.org/licenses/gpl-2.0.html)
22
 * @link      https://xoops.org
23
 */
24
class Migrate extends \Xmf\Database\Migrate
25
{
26
    private $renameTables;
27
28
    /**
29
     * Migrate constructor.
30
     * @throws \RuntimeException
31
     * @throws \InvalidArgumentException
32
     */
33
    public function __construct()
34
    {
35
        $class = __NAMESPACE__ . '\\' . 'Configurator';
36
        if (!\class_exists($class)) {
37
            throw new \RuntimeException("Class '$class' not found");
38
        }
39
        $configurator       = new $class();
40
        $this->renameTables = $configurator->renameTables;
41
42
        $moduleDirName = \basename(dirname(__DIR__, 2));
43
        parent::__construct($moduleDirName);
44
    }
45
46
    /**
47
     * change table prefix if needed
48
     */
49
    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...
50
    {
51
        foreach ($this->renameTables as $oldName => $newName) {
52
            if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
53
                $this->tableHandler->renameTable($oldName, $newName);
54
            }
55
        }
56
    }
57
58
    /**
59
     * Change integer IPv4 column to varchar IPv6 capable
60
     *
61
     * @param string $tableName  table to convert
62
     * @param string $columnName column with IP address
63
     */
64
    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...
65
    {
66
        if ($this->tableHandler->useTable($tableName)) {
67
            $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
68
            if (false !== mb_strpos($attributes, ' int(')) {
69
                if (false === mb_strpos($attributes, 'unsigned')) {
70
                    $this->tableHandler->alterColumn($tableName, $columnName, " bigint(16) NOT NULL  DEFAULT '0' ");
71
                    $this->tableHandler->update($tableName, [$columnName => "4294967296 + $columnName"], "WHERE $columnName < 0", false);
72
                }
73
                $this->tableHandler->alterColumn($tableName, $columnName, " varchar(45)  NOT NULL  DEFAULT '' ");
74
                $this->tableHandler->update($tableName, [$columnName => "INET_NTOA($columnName)"], '', false);
75
            }
76
        }
77
    }
78
79
    /**
80
     * Move do* columns from newbb_posts to newbb_posts_text table
81
     */
82
    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...
83
    {
84
        $tableName    = 'newbb_posts_text';
85
        $srcTableName = 'newbb_posts';
86
        if ($this->tableHandler->useTable($tableName)
87
            && $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
    protected function preSyncActions()
107
    {
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
}
119