Passed
Push — master ( 077fbd...836ec9 )
by Michael
04:09
created

Migrate::renameColumns()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 1
b 0
f 1
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
//use XoopsModules\Extcal\Common;
25
26
class Migrate extends \Xmf\Database\Migrate
27
{
28
    private $renameTables;
29
30
    /**
31
     * Migrate constructor.
32
     * @param Configurator $configurator
33
     * @throws \RuntimeException
34
     * @throws \InvalidArgumentException
35
     */
36
    public function __construct(Configurator $configurator = null)
37
    {
38
        if (null !== $configurator) {
39
            $this->renameTables = $configurator->renameTables;
40
41
            $moduleDirName = basename(dirname(dirname(__DIR__)));
42
            parent::__construct($moduleDirName);
43
        }
44
    }
45
46
    /**
47
     * change table prefix if needed
48
     */
49
    private function changePrefix()
50
    {
51
        foreach ($this->renameTables as $oldName => $newName) {
52
            if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
53
                $this->tableHandler->renameTable($oldName, $newName);
54
            }
55
        }
56
    }
57
58
    private function renameColumns($tableName, $columnName, $newName)
59
    {
60
        if ($this->tableHandler->useTable($tableName)) {
61
            $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
62
            if (false !== strpos($attributes, ' int(')) {
63
                $this->tableHandler->alterColumn($tableName, $columnName, $attributes, $newName);
64
            }
65
        }
66
    }
67
68
    /**
69
     * Perform any upfront actions before synchronizing the schema
70
     *
71
     * Some typical uses include
72
     *   table and column renames
73
     *   data conversions
74
     */
75
    protected function preSyncActions()
76
    {
77
        // change table prefix
78
        if ($this->renameTables && is_array($this->renameTables)) {
79
            $this->changePrefix();
80
        }
81
        $this->renameColumns('extcal_event', 'event_etablissement', 'event_location');
82
    }
83
84
}
85