Passed
Push — master ( 82b95d...638320 )
by Michael
04:10 queued 11s
created

Migrate::renameColumn()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 3
1
<?php
2
3
namespace XoopsModules\Extcal\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
     * @param \XoopsModules\Extcal\Common\Configurator $configurator
31
     */
32
    public function __construct(\XoopsModules\Extcal\Common\Configurator $configurator)
33
    {
34
        $class = __NAMESPACE__ . '\\' . 'Configurator';
35
        if (!\class_exists($class)) {
36
            throw new \RuntimeException("Class '$class' not found");
37
        }
38
        $configurator       = new $class();
39
        $this->renameTables = $configurator->renameTables;
40
41
        $moduleDirName = \basename(\dirname(\dirname(__DIR__)));
42
        parent::__construct($moduleDirName);
43
    }
44
45
    /**
46
     * rename table if needed
47
     */
48
    private function renameTable()
49
    {
50
        foreach ($this->renameTables as $oldName => $newName) {
51
            if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
52
                $this->tableHandler->renameTable($oldName, $newName);
53
            }
54
        }
55
    }
56
57
    private function renameColumn($tableName, $columnName, $newName)
58
    {
59
        if ($this->tableHandler->useTable($tableName)) {
60
            $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
61
            if (false !== \strpos($attributes, ' int(')) {
62
                $this->tableHandler->alterColumn($tableName, $columnName, $attributes, $newName);
63
            }
64
        }
65
    }
66
67
    /**
68
     * Perform any upfront actions before synchronizing the schema
69
     *
70
     * Some typical uses include
71
     *   table and column renames
72
     *   data conversions
73
     */
74
    protected function preSyncActions()
75
    {
76
        // rename table
77
        if ($this->renameTables && \is_array($this->renameTables)) {
78
            $this->renameTable();
79
        }
80
                $this->renameColumn('extcal_event', 'event_etablissement','event_location');
81
    }
82
}
83