Passed
Push — master ( 0708c5...e4b65d )
by Michael
13:42 queued 13s
created

Migrate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 58
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A preSyncActions() 0 6 1
A __construct() 0 11 2
A changeColumnSize() 0 10 3
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\News\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 RuntimeException;
16
use Xmf\Database\Tables;
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.0 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
     * @throws \InvalidArgumentException
34
     * @throws \RuntimeException
35
     */
36
    public function __construct()
37
    {
38
        $class = __NAMESPACE__ . '\\' . 'Configurator';
39
        if (!\class_exists($class)) {
40
            throw new RuntimeException("Class '$class' not found");
41
        }
42
        $configurator       = new $class();
43
        $this->renameTables = $configurator->renameTables;
44
45
        $moduleDirName = \basename(\dirname(__DIR__, 2));
46
        parent::__construct($moduleDirName);
47
    }
48
49
50
    /**
51
     * Change column size of a field
52
     *
53
     * @param string $tableName  table to convert
54
     * @param string $columnName column to convert
55
     * @param string $attribute new attribute
56
     */
57
    private function changeColumnSize($tableName, $columnName, $attribute)
58
    {
59
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
60
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
61
62
        $tables = new Tables();
63
        if ($tables->useTable($tableName)) {
64
            $tables->alterColumn($tableName, $columnName, $attribute);
65
            if (!$tables->executeQueue()) {
66
                echo '<br>' . constant('CO_' . $moduleDirNameUpper . '_UPGRADEFAILED4') . ' ' . $tables->getLastError();
67
            }
68
        }
69
    }
70
71
72
    /**
73
     * Perform any upfront actions before synchronizing the schema
74
     *
75
     * Some typical uses include
76
     *   table and column renames
77
     *   data conversions
78
     */
79
    protected function preSyncActions(): void
80
    {
81
       // change column size for IP address from varchar(16) to varchar(45) for IPv6
82
        $this->changeColumnSize('news_stories', 'hostname', 'varchar(45) NOT NULL DEFAULT \'\'');
83
        // change column size for Picture from varchar(50) to varchar(255) for SEO
84
        $this->changeColumnSize('news_stories', 'picture', 'varchar(255) NOT NULL DEFAULT \'\'');
85
86
    }
87
}
88