1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Tdmdownloads\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
|
|
|
private $renameColumns; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Migrate constructor. |
31
|
|
|
* @throws \RuntimeException |
32
|
|
|
* @throws \InvalidArgumentException |
33
|
|
|
*/ |
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
$class = __NAMESPACE__ . '\\' . 'Configurator'; |
37
|
|
|
|
38
|
|
|
if (!\class_exists($class)) { |
39
|
|
|
throw new \RuntimeException("Class '$class' not found"); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$configurator = new $class(); |
43
|
|
|
|
44
|
|
|
$this->renameTables = $configurator->renameTables; |
45
|
|
|
|
46
|
|
|
$moduleDirName = \basename(dirname(__DIR__, 2)); |
47
|
|
|
|
48
|
|
|
parent::__construct($moduleDirName); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* rename table if needed |
54
|
|
|
*/ |
55
|
|
|
private function renameTable() |
56
|
|
|
{ |
57
|
|
|
foreach ($this->renameTables as $oldName => $newName) { |
58
|
|
|
if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) { |
59
|
|
|
$this->tableHandler->renameTable($oldName, $newName); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* rename columns if needed |
66
|
|
|
*/ |
67
|
|
|
private function renameColumns() |
68
|
|
|
{ |
69
|
|
|
foreach ($this->renameColumns as $tableName) { |
70
|
|
|
if ($this->tableHandler->useTable($tableName)) { |
71
|
|
|
$oldName = $tableName['from']; |
72
|
|
|
$newName = $tableName['to']; |
73
|
|
|
$attributes = $this->tableHandler->getColumnAttributes($tableName, $oldName); |
74
|
|
|
if (false !== \strpos($attributes, ' int(')) { |
|
|
|
|
75
|
|
|
$this->tableHandler->alterColumn($tableName, $oldName, $attributes, $newName); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Perform any upfront actions before synchronizing the schema |
83
|
|
|
* |
84
|
|
|
* Some typical uses include |
85
|
|
|
* table and column renames |
86
|
|
|
* data conversions |
87
|
|
|
*/ |
88
|
|
|
protected function preSyncActions() |
89
|
|
|
{ |
90
|
|
|
// rename table |
91
|
|
|
if ($this->renameTables && \is_array($this->renameTables)) { |
92
|
|
|
$this->renameTable(); |
93
|
|
|
} |
94
|
|
|
// rename column |
95
|
|
|
if ($this->renameColumns && \is_array($this->renameColumns)) { |
96
|
|
|
$this->renameColumns(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|