1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace XoopsModules\Xoopsheadline\Common; |
6
|
|
|
|
7
|
|
|
/* |
8
|
|
|
You may not change or alter any portion of this comment or credits |
9
|
|
|
of supporting developers from this source code or any supporting source code |
10
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
11
|
|
|
|
12
|
|
|
This program is distributed in the hope that it will be useful, |
13
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
use Xmf\Database\Tables; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Migrate synchronize existing tables with target schema |
21
|
|
|
* |
22
|
|
|
* @category Migrate |
23
|
|
|
* @author Richard Griffith <[email protected]> |
24
|
|
|
* @copyright 2016 XOOPS Project (https://xoops.org) |
25
|
|
|
* @license GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html) |
26
|
|
|
* @link https://xoops.org |
27
|
|
|
*/ |
28
|
|
|
class Migrate extends \Xmf\Database\Migrate |
29
|
|
|
{ |
30
|
|
|
private $moduleDirName; |
31
|
|
|
private $renameColumns; |
32
|
|
|
private $renameTables; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Migrate constructor. |
36
|
|
|
* @throws \RuntimeException |
37
|
|
|
* @throws \InvalidArgumentException |
38
|
|
|
*/ |
39
|
|
|
public function __construct() |
40
|
|
|
{ |
41
|
|
|
$class = __NAMESPACE__ . '\\' . 'Configurator'; |
42
|
|
|
if (!\class_exists($class)) { |
43
|
|
|
throw new \RuntimeException("Class '$class' not found"); |
44
|
|
|
} |
45
|
|
|
$configurator = new $class(); |
46
|
|
|
$this->renameTables = $configurator->renameTables; |
47
|
|
|
$this->renameColumns = $configurator->renameColumns; |
48
|
|
|
|
49
|
|
|
$this->moduleDirName = \basename(\dirname(__DIR__, 2)); |
50
|
|
|
parent::__construct($this->moduleDirName); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* rename table if needed |
55
|
|
|
*/ |
56
|
|
|
private function renameTable(): void |
57
|
|
|
{ |
58
|
|
|
foreach ($this->renameTables as $oldName => $newName) { |
59
|
|
|
if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) { |
60
|
|
|
$this->tableHandler->renameTable($oldName, $newName); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* rename columns if needed |
67
|
|
|
*/ |
68
|
|
|
private function renameColumns(): void |
69
|
|
|
{ |
70
|
|
|
$tables = new Tables(); |
71
|
|
|
foreach ($this->renameColumns as $table) { |
72
|
|
|
$tableName = $table['tablename']; |
73
|
|
|
$tableExists = $tables->useTable($tableName); |
74
|
|
|
if ($tableExists) { |
75
|
|
|
$oldName = $table['from']; |
76
|
|
|
$newName = $table['to']; |
77
|
|
|
$tableDetails = $tables->dumpTables(); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$attributes = $tables->getColumnAttributes($tableName, $oldName); |
80
|
|
|
// if (false !== \strpos($attributes, ' int(')) { |
81
|
|
|
if (false !== $attributes) { |
82
|
|
|
$tables->alterColumn($tableName, $oldName, $attributes, $newName); |
83
|
|
|
|
84
|
|
|
// $tables->getTableIndexes() update($tableName, [$newName => "($newName)"], '', false); |
85
|
|
|
// |
86
|
|
|
// $tables->dropIndex($name, $table) |
87
|
|
|
// $tables->addIndex($name, $table, $column, $unique = false) |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Perform any upfront actions before synchronizing the schema |
96
|
|
|
* |
97
|
|
|
* Some typical uses include |
98
|
|
|
* table and column renames |
99
|
|
|
* data conversions |
100
|
|
|
*/ |
101
|
|
|
protected function preSyncActions(): void |
102
|
|
|
{ |
103
|
|
|
// rename table |
104
|
|
|
if ($this->renameTables && \is_array($this->renameTables)) { |
105
|
|
|
$this->renameTable(); |
106
|
|
|
} |
107
|
|
|
// rename column |
108
|
|
|
if ($this->renameColumns && \is_array($this->renameColumns)) { |
109
|
|
|
$this->renameColumns(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|