forked from XoopsModules25x/modulebuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMigrate.php
More file actions
115 lines (104 loc) · 4.38 KB
/
Migrate.php
File metadata and controls
115 lines (104 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php declare(strict_types=1);
namespace XoopsModules\Modulebuilder\Common;
/*
You may not change or alter any portion of this comment or credits
of supporting developers from this source code or any supporting source code
which is considered copyrighted (c) material of the original comment or credit authors.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
use XoopsModules\Modulebuilder\Common;
/**
* Class Migrate synchronize existing tables with target schema
*
* @category Migrate
* @author Richard Griffith <richard@geekwright.com>
* @copyright 2016 XOOPS Project (https://xoops.org)
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
* @link https://xoops.org
*/
class Migrate extends \Xmf\Database\Migrate
{
private $renameTables;
/**
* @param \XoopsModules\Modulebuilder\Common\Configurator|null $configurator
*/
public function __construct(Common\Configurator $configurator = null)
{
if (null !== $configurator) {
$this->renameTables = $configurator->renameTables;
$moduleDirName = \basename(\dirname(__DIR__, 2));
parent::__construct($moduleDirName);
}
}
/**
* change table prefix if needed
*/
private function changePrefix(): void
{
foreach ($this->renameTables as $oldName => $newName) {
if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
$this->tableHandler->renameTable($oldName, $newName);
}
}
}
/**
* Change integer IPv4 column to varchar IPv6 capable
*
* @param string $tableName table to convert
* @param string $columnName column with IP address
*/
private function convertIPAddresses(string $tableName, string $columnName): void
{
if ($this->tableHandler->useTable($tableName)) {
$attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
if (false !== \mb_strpos($attributes, ' int(')) {
if (false === \mb_strpos($attributes, 'unsigned')) {
$this->tableHandler->alterColumn($tableName, $columnName, " bigint(16) NOT NULL DEFAULT '0' ");
$this->tableHandler->update($tableName, [$columnName => "4294967296 + $columnName"], "WHERE $columnName < 0", false);
}
$this->tableHandler->alterColumn($tableName, $columnName, " varchar(45) NOT NULL DEFAULT '' ");
$this->tableHandler->update($tableName, [$columnName => "INET_NTOA($columnName)"], '', false);
}
}
}
/**
* Move do* columns from newbb_posts to newbb_posts_text table
*/
private function moveDoColumns(): void
{
$tableName = 'newbb_posts_text';
$srcTableName = 'newbb_posts';
if ($this->tableHandler->useTable($tableName)
&& $this->tableHandler->useTable($srcTableName)) {
$attributes = $this->tableHandler->getColumnAttributes($tableName, 'dohtml');
if (false === $attributes) {
$this->synchronizeTable($tableName);
$updateTable = $GLOBALS['xoopsDB']->prefix($tableName);
$joinTable = $GLOBALS['xoopsDB']->prefix($srcTableName);
$sql = "UPDATE `$updateTable` t1 INNER JOIN `$joinTable` t2 ON t1.post_id = t2.post_id \n" . "SET t1.dohtml = t2.dohtml, t1.dosmiley = t2.dosmiley, t1.doxcode = t2.doxcode\n" . ' , t1.doimage = t2.doimage, t1.dobr = t2.dobr';
$this->tableHandler->addToQueue($sql);
}
}
}
/**
* Perform any upfront actions before synchronizing the schema
*
* Some typical uses include
* table and column renames
* data conversions
*/
protected function preSyncActions(): void
{
/*
// change 'bb' table prefix to 'newbb'
$this->changePrefix();
// columns dohtml, dosmiley, doxcode, doimage and dobr moved between tables as some point
$this->moveDoColumns();
// Convert IP address columns from int to readable varchar(45) for IPv6
$this->convertIPAddresses('newbb_posts', 'poster_ip');
$this->convertIPAddresses('newbb_report', 'reporter_ip');
*/
}
}