-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbEncodingConversor.php
255 lines (216 loc) · 8.89 KB
/
DbEncodingConversor.php
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env php
<?php
/**
* Encoding conversion (MySQL).
* Fix collation and encoding of a specific database/tables/columns; also, fixes issues with indices.
*
* @see https://mathiasbynens.be/notes/mysql-utf8mb4
* @see https://medium.com/@alexBerg/my-war-with-mysql-or-how-did-i-switch-to-full-utf8mb4-73b257083ac8
* @see https://medium.com/tensult/converting-rds-mysql-file-format-from-antelope-to-barracuda-ba8a60b2c1ec
*/
class DbEncodingConversor {
/**
* @type int
*/
private static $_maxLength = 191;
/**
* @type PDO
*/
private $_PDOObject;
/**
* @type string
*/
private $_database;
/**
* @type string[]
*/
private $_indices = [];
/**
* @param string $host
* @param string $database
* @return void
*/
public function __construct (string $host, string $database) {
if (!defined('PDO::ATTR_DRIVER_NAME')) {
echo "PDO is not installed in your system...\n";
exit;
}
$password = self::_getPassword('Enter MySQL root password: ');
$dns = sprintf('%s:dbname=%s;host=%s;charset=%s', 'mysql', $database, $host, 'utf8mb4');
try {
$this->_PDOObject = new PDO($dns, 'root', $password);
$this->_PDOObject ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_PDOObject ->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$this->_PDOObject ->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
} catch (PDOException $e) {
echo "Connection failed: {$e->getMessage()}\n";
exit;
}
$this->_database = $database;
}
public function run() {
$tablesData = $this->_getDbTablesData();
$this->_changeDatabaseEncoding();
$textTypes = ['text','tinytext','mediumtext','longtext', 'varchar','char'];
// First, convert each text column so, in the event that the type of field
// needs to be changed to support current byte limits, index can be adjusted as well.
// To avoid issues during the conversion per columns, turn off foreign keys checking
$indexStmt = $this->_PDOObject->prepare("SET FOREIGN_KEY_CHECKS=0");
$indexStmt->execute();
foreach ($tablesData as $table => $columns) {
foreach ($columns as $column) {
if (!in_array($column['type'], $textTypes)) {
continue;
}
$isNull = $column['nullable'] ? 'NULL' : 'NOT NULL';
// If type is (VAR)CHAR, adjust the byte limit and mark all indices
// associated with it to be updated
if ($column['type'] === 'char' || $column['type'] === 'varchar') {
$this->_verifyColumnStructure($table, $column);
}
// Drop old indices
$this->_dropStoredIndices($table);
$columnStmt = $this->_PDOObject->prepare("
ALTER TABLE `{$this->_database}`.`{$table}`
CHANGE `{$column['column']}` `{$column['column']}` {$column['type']}
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci
{$isNull}");
$columnStmt->execute();
echo "Updated encoding for column `{$table}.{$column['column']}` with type {$column['type']}\n";
// Add updated indices
$this->_addStoredIndices($table);
}
}
foreach (array_keys($tablesData) as $table) {
// This statements allows the table to accept the `Barracuda` file type properly
$oStmt = $this->_PDOObject->prepare("ALTER TABLE `{$this->_database}`.`{$table}` ROW_FORMAT=DYNAMIC");
$oStmt->execute();
$oStmt = $this->_PDOObject->prepare("
ALTER TABLE `{$this->_database}`.`{$table}`
CONVERT TO CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci");
$oStmt->execute();
$oRepairStmt = $this->_PDOObject->prepare("REPAIR TABLE `{$this->_database}`.`{$table}`");
$oRepairStmt->execute();
$oOptimizeStmt = $this->_PDOObject->prepare("OPTIMIZE TABLE `{$this->_database}`.`{$table}`");
$oOptimizeStmt->execute();
echo "Fixed encoding/repaired/optimized `{$table}`\n";
}
// Turn on foreign keys checking once the operation is completed
$indexStmt = $this->_PDOObject->prepare("SET FOREIGN_KEY_CHECKS=1");
$indexStmt->execute();
}
/**
* @param string $sPrompt
* @return string
*/
private static function _getPassword (string $prompt = "Enter Password: ") {
echo $prompt;
system('stty -echo');
$password = trim(fgets(STDIN));
system('stty echo');
echo "\n";
return $password;
}
/**
* @return array
*/
private function _getDbTablesData() {
$query = "
SELECT table_name, column_name, data_type, IF(is_nullable='YES', 1, 0) as nullable,
LENGTH(column_name) as length
FROM `information_schema`.`columns`
WHERE table_schema = '{$this->_database}'";
$tablesData = [];
foreach ($this->_PDOObject->query($query) as $row) {
$tablesData[$row['table_name']][] = [
'column' => $row['column_name'],
'type' => $row['data_type'],
'nullable' => $row['nullable'],
];
}
return $tablesData;
}
/**
*
*/
private function _changeDatabaseEncoding() {
$oStmt = $this->_PDOObject->prepare("ALTER DATABASE `{$this->_database}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
$oStmt->execute();
echo "Altered database charset and collation\n";
}
/**
* @param string $table
* @param array $columnData
*/
private function _verifyColumnStructure(string $table, array &$columnData) {
$length = self::$_maxLength;
$statement = $this->_PDOObject->prepare("
SELECT 1 FROM `{$this->_database}`.`{$table}` WHERE LENGTH({$columnData['column']}) > {$length}");
$statement->execute();
$result = $statement->fetch();
if ($result) {
$columnData['type'] = 'TINYTEXT';
echo "Changed field type for `{$table}.{$columnData['column']}`\n";
$this->_collectColumnIndexRef($table, $columnData['column']);
} else {
$columnData['type'] .= "({$length})";
}
}
/**
* @param string $table
* @param string $column
*/
private function _collectColumnIndexRef(string $table, string $column) {
$length = self::$_maxLength;
$results = $this->_PDOObject->query("SHOW INDEX FROM `{$this->_database}`.`{$table}`");
foreach ($results as $index) {
if ($index['Column_name'] === $column) {
if (!isset($this->_indices[$index['Key_name']])) {
$this->_indices[$index['Key_name']] = [
'name' => $index['Key_name'],
'unique' => $index['Non_unique'] === 0,
'columns' => ["`{$column}`({$length})"],
'deleted' => false,
];
}
}
}
foreach ($results as $index) {
if (isset($this->_indices[$index['Key_name']]) && !$this->_indices[$index['Key_name']]['deleted']) {
if (in_array("`{$index['Column_name']}`({$length})", $this->_indices[$index['Key_name']]['columns'])) {
continue;
}
$this->_indices[$index['Key_name']]['columns'][] = '`' . $index['Column_name'] . '`';
}
}
}
/**
* @param string $table
*/
private function _dropStoredIndices(string $table) {
foreach (array_keys($this->_indices) as $index) {
$indexStmt = $this->_PDOObject->prepare("ALTER TABLE {$table} DROP INDEX {$index}");
$indexStmt->execute();
$this->_indices[$index]['deleted'] = true;
}
}
/**
* @param string $table
*/
private function _addStoredIndices(string $table) {
foreach ($this->_indices as $indexName => $indexData) {
$indexColumns = implode(',', $indexData['columns']);
$isUnique = $indexData['unique'] ? 'UNIQUE' : "";
$indexStmt = $this->_PDOObject->prepare("
ALTER TABLE {$table}
ADD {$isUnique} INDEX `{$indexData['name']}` ({$indexColumns})");
$indexStmt->execute();
unset($this->_indices[$indexName]);
echo "Updated `{$indexData['name']}` index`\n";
}
}
}
$oConversor = new DbEncodingConversor($argv[1], $argv[2]);
$oConversor->run();