Migrate   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A preSyncActions() 0 9 5
A __construct() 0 12 2
A renameTable() 0 5 4
A renameColumns() 0 15 4
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xoopsheadline\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 Xmf\Database\Tables;
16
17
/**
18
 * Class Migrate synchronize existing tables with target schema
19
 *
20
 * @category  Migrate
21
 * @author    Richard Griffith <[email protected]>
22
 * @copyright 2016 XOOPS Project (https://xoops.org)
23
 * @license   GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
24
 * @link      https://xoops.org
25
 */
26
class Migrate extends \Xmf\Database\Migrate
27
{
28
    private $moduleDirName;
29
    private $renameColumns;
30
    private $renameTables;
31
32
    /**
33
     * Migrate constructor.
34
     * @throws \InvalidArgumentException
35
     * @throws \RuntimeException
36
     */
37
    public function __construct()
38
    {
39
        $class = __NAMESPACE__ . '\\' . 'Configurator';
40
        if (!\class_exists($class)) {
41
            throw new \RuntimeException("Class '$class' not found");
42
        }
43
        $configurator        = new $class();
44
        $this->renameTables  = $configurator->renameTables;
45
        $this->renameColumns = $configurator->renameColumns;
46
47
        $this->moduleDirName = \basename(\dirname(__DIR__, 2));
48
        parent::__construct($this->moduleDirName);
49
    }
50
51
    /**
52
     * rename table if needed
53
     */
54
    private function renameTable(): void
55
    {
56
        foreach ($this->renameTables as $oldName => $newName) {
57
            if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
58
                $this->tableHandler->renameTable($oldName, $newName);
59
            }
60
        }
61
    }
62
63
    /**
64
     * rename columns if needed
65
     */
66
    private function renameColumns(): void
67
    {
68
        $tables = new Tables();
69
        foreach ($this->renameColumns as $table) {
70
            $tableName   = $table['tablename'];
71
            $tableExists = $tables->useTable($tableName);
72
            if ($tableExists) {
73
                $oldName = $table['from'];
74
                $newName = $table['to'];
75
                //                $tableDetails = $tables->dumpTables();
76
77
                $attributes = $tables->getColumnAttributes($tableName, $oldName);
78
                //                if (false !== \strpos($attributes, ' int(')) {
79
                if (false !== $attributes) {
80
                    $tables->alterColumn($tableName, $oldName, $attributes, $newName);
81
82
                    //                    $tables->getTableIndexes()  update($tableName, [$newName => "($newName)"], '', false);
83
                    //
84
                    //                    $tables->dropIndex($name, $table)
85
                    //                    $tables->addIndex($name, $table, $column, $unique = false)
86
                }
87
            }
88
        }
89
    }
90
91
    /**
92
     * Perform any upfront actions before synchronizing the schema
93
     *
94
     * Some typical uses include
95
     *   table and column renames
96
     *   data conversions
97
     */
98
    protected function preSyncActions(): void
99
    {
100
        // rename table
101
        if ($this->renameTables && \is_array($this->renameTables)) {
102
            $this->renameTable();
103
        }
104
        // rename column
105
        if ($this->renameColumns && \is_array($this->renameColumns)) {
106
            $this->renameColumns();
107
        }
108
    }
109
}
110