Migrate::preSyncActions()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

65
            if (false !== mb_strpos(/** @scrutinizer ignore-type */ $attributes, ' int(')) {
Loading history...
66
                if (false === mb_strpos($attributes, 'unsigned')) {
67
                    $this->tableHandler->alterColumn($tableName, $columnName, " bigint(16) NOT NULL  DEFAULT '0' ");
68
                    $this->tableHandler->update($tableName, [$columnName => "4294967296 + $columnName"], "WHERE $columnName < 0", false);
69
                }
70
                $this->tableHandler->alterColumn($tableName, $columnName, " varchar(45)  NOT NULL  DEFAULT '' ");
71
                $this->tableHandler->update($tableName, [$columnName => "INET_NTOA($columnName)"], '', false);
72
            }
73
        }
74
    }
75
76
    /**
77
     * Move columns to another table
78
     */
79
    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...
80
    {
81
        //for an example, see newbb 5.0
82
    }
83
84
    /**
85
     * Perform any upfront actions before synchronizing the schema
86
     *
87
     * Some typical uses include
88
     *   table and column renames
89
     *   data conversions
90
     */
91
    protected function preSyncActions()
92
    {
93
        // change table prefix
94
        if ($this->renameTables && \is_array($this->renameTables)) {
95
            $this->changePrefix();
96
        }
97
        //        // columns dohtml, dosmiley, doxcode, doimage and dobr moved between tables as some point
98
        //        $this->moveDoColumns();
99
        //        // Convert IP address columns from int to readable varchar(45) for IPv6
100
        //        $this->convertIPAddresses('extgallery_posts', 'poster_ip');
101
        //        $this->convertIPAddresses('extgallery_report', 'reporter_ip');
102
    }
103
}
104