Migrate::moveDoColumns()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
nc 3
nop 0
dl 0
loc 13
rs 9.9
c 1
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Wfdownloads;
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
 * @package   Newbb
20
 * @author    Richard Griffith <[email protected]>
21
 * @copyright 2016 XOOPS Project (https://xoops.org)
22
 * @license   GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
23
 * @link      https://xoops.org
24
 */
25
class Migrate extends \Xmf\Database\Migrate
26
{
27
    //    private $renameTables = [
28
    //        'bb_archive'     => 'newbb_archive',
29
    //        'bb_categories'  => 'newbb_categories',
30
    //        'bb_votedata'    => 'newbb_votedata',
31
    //        'bb_forums'      => 'newbb_forums',
32
    //        'bb_posts'       => 'newbb_posts',
33
    //        'bb_posts_text'  => 'newbb_posts_text',
34
    //        'bb_topics'      => 'newbb_topics',
35
    //        'bb_online'      => 'newbb_online',
36
    //        'bb_digest'      => 'newbb_digest',
37
    //        'bb_report'      => 'newbb_report',
38
    //        'bb_attachments' => 'newbb_attachments',
39
    //        'bb_moderates'   => 'newbb_moderates',
40
    //        'bb_reads_forum' => 'newbb_reads_forum',
41
    //        'bb_reads_topic' => 'newbb_reads_topic',
42
    //        'bb_type'        => 'newbb_type',
43
    //        'bb_type_forum'  => 'newbb_type_forum',
44
    //        'bb_stats'       => 'newbb_stats',
45
    //        'bb_user_stats'  => 'newbb_user_stats',
46
    //    ];
47
48
    /**
49
     * Migrate constructor.
50
     * @throws \RuntimeException
51
     * @throws \InvalidArgumentException
52
     */
53
    public function __construct()
54
    {
55
        $moduleDirName = \basename(\dirname(__DIR__));
56
        parent::__construct($moduleDirName);
57
    }
58
59
    /**
60
     * change table prefix if needed
61
     */
62
    private function changePrefix()
63
    {
64
        foreach ($this->renameTables as $oldName => $newName) {
0 ignored issues
show
Bug Best Practice introduced by
The property renameTables does not exist on XoopsModules\Wfdownloads\Migrate. Did you maybe forget to declare it?
Loading history...
65
            if ($this->tableHandler->useTable($oldName)) {
66
                $this->tableHandler->renameTable($oldName, $newName);
67
            }
68
        }
69
    }
70
71
    /**
72
     * Change integer IPv4 column to varchar IPv6 capable
73
     *
74
     * @param string $tableName  table to convert
75
     * @param string $columnName column with IP address
76
     */
77
    private function convertIPAddresses($tableName, $columnName)
78
    {
79
        if ($this->tableHandler->useTable($tableName)) {
80
            $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
81
            if (false !== mb_strpos($attributes, ' int(')) {
82
                if (false === mb_strpos($attributes, 'unsigned')) {
83
                    $this->tableHandler->alterColumn($tableName, $columnName, " bigint(16) NOT NULL  DEFAULT '0' ");
84
                    $this->tableHandler->update($tableName, [$columnName => "4294967296 + $columnName"], "WHERE $columnName < 0", false);
85
                }
86
                $this->tableHandler->alterColumn($tableName, $columnName, " varchar(45)  NOT NULL  DEFAULT '' ");
87
                $this->tableHandler->update($tableName, [$columnName => "INET_NTOA($columnName)"], '', false);
88
            }
89
        }
90
    }
91
92
    /**
93
     * Move do* columns from newbb_posts to newbb_posts_text table
94
     */
95
    private function moveDoColumns()
96
    {
97
        $tableName    = 'newbb_posts_text';
98
        $srcTableName = 'newbb_posts';
99
        if ($this->tableHandler->useTable($tableName)
100
            && $this->tableHandler->useTable($srcTableName)) {
101
            $attributes = $this->tableHandler->getColumnAttributes($tableName, 'dohtml');
102
            if (false === $attributes) {
103
                $this->synchronizeTable($tableName);
104
                $updateTable = $GLOBALS['xoopsDB']->prefix($tableName);
105
                $joinTable   = $GLOBALS['xoopsDB']->prefix($srcTableName);
106
                $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';
107
                $this->tableHandler->addToQueue($sql);
108
            }
109
        }
110
    }
111
112
    /**
113
     * Perform any upfront actions before synchronizing the schema
114
     *
115
     * Some typical uses include
116
     *   table and column renames
117
     *   data conversions
118
     */
119
    protected function preSyncActions()
120
    {
121
        //add the table if it's missing:
122
        $this->addMissingTable('wfdownloads_ip_log');
123
124
        // or Convert IP address columns from varchar(20) to readable varchar(45) for IPv6
125
        $this->convertIPAddresses('wfdownloads_ip_log', 'ip_address');
126
127
        // change 'bb' table prefix to 'newbb'
128
        $this->changePrefix();
129
        // columns dohtml, dosmiley, doxcode, doimage and dobr moved between tables as some point
130
        $this->moveDoColumns();
131
    }
132
}
133