Skip to content

Commit d900f79

Browse files
authored
Add files via upload
1 parent 82ab9ba commit d900f79

File tree

3 files changed

+111
-42
lines changed

3 files changed

+111
-42
lines changed

src/Differ.php

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
use Camcima\MySqlDiff\Model\Database;
88
use Camcima\MySqlDiff\Model\DatabaseDiff;
99

10+
/**
11+
* Class Differ
12+
*
13+
* @package Camcima\MySqlDiff
14+
*/
1015
class Differ
1116
{
1217
/**
@@ -32,7 +37,8 @@ public function diffDatabases(Database $fromDatabase, Database $toDatabase, arra
3237
}
3338

3439
$toTable = $toDatabase->getTableByName($fromTable->getName());
35-
if ($fromTable->generateCreationScript(true) != $toTable->generateCreationScript(true)) {
40+
41+
if ($fromTable->generateCreationScript(true) !== $toTable->generateCreationScript(true)) {
3642
$changedTable = new ChangedTable($fromTable, $toTable);
3743
$this->diffChangedTable($changedTable);
3844
$databaseDiff->addChangedTable($changedTable);
@@ -89,18 +95,19 @@ private function diffColumns(ChangedTable $changedTable)
8995

9096
// Determine changed columns
9197
$fromColumn = $fromTable->getColumnByName($toColumn->getName());
92-
if ($toColumn->generateCreationScript() != $fromColumn->generateCreationScript()) {
98+
if ($toColumn->generateCreationScript() !== $fromColumn->generateCreationScript()) {
9399
$changedTable->addChangedColumn($toColumn);
94100
continue;
95101
}
96102

97103
if (!$fromColumn->getPreviousColumn() && !$toColumn->getPreviousColumn()) {
98104
continue;
99-
} elseif (!$fromColumn->getPreviousColumn() && $toColumn->getPreviousColumn() instanceof Column) {
105+
}
106+
if (!$fromColumn->getPreviousColumn() && $toColumn->getPreviousColumn() instanceof Column) {
100107
$this->addChangedColumn($changedTable, $toColumn);
101108
} elseif ($fromColumn->getPreviousColumn() instanceof Column && !$toColumn->getPreviousColumn()) {
102109
$this->addChangedColumn($changedTable, $toColumn);
103-
} elseif ($fromColumn->getPreviousColumn()->getName() != $toColumn->getPreviousColumn()->getName()) {
110+
} elseif ($fromColumn->getPreviousColumn()->getName() !== $toColumn->getPreviousColumn()->getName()) {
104111
$this->addChangedColumn($changedTable, $toColumn);
105112
}
106113
}
@@ -137,7 +144,7 @@ private function diffPrimaryKey(ChangedTable $changedTable)
137144
return;
138145
}
139146

140-
if ($fromTable->generatePrimaryKeyCreationScript() != $toTable->generatePrimaryKeyCreationScript()) {
147+
if ($fromTable->generatePrimaryKeyCreationScript() !== $toTable->generatePrimaryKeyCreationScript()) {
141148
$changedTable->setChangedPrimaryKeys($toTable->getPrimaryKeys());
142149
}
143150
}
@@ -167,7 +174,7 @@ private function diffIndexes(ChangedTable $changedTable)
167174

168175
// Determine changed indexes
169176
$fromIndex = $fromTable->getIndexByName($toIndex->getName());
170-
if ($toIndex->generateCreationScript() != $fromIndex->generateCreationScript()) {
177+
if ($toIndex->generateCreationScript() !== $fromIndex->generateCreationScript()) {
171178
$changedTable->addChangedIndex($toIndex);
172179
}
173180
}
@@ -198,7 +205,7 @@ private function diffForeignKeys(ChangedTable $changedTable)
198205

199206
// Determine changed foreign keys
200207
$fromForeignKey = $fromTable->getForeignKeyByName($toForeignKey->getName());
201-
if ($toForeignKey->generateCreationScript() != $fromForeignKey->generateCreationScript()) {
208+
if ($toForeignKey->generateCreationScript() !== $fromForeignKey->generateCreationScript()) {
202209
$changedTable->addChangedForeignKey($toForeignKey);
203210
}
204211
}
@@ -212,46 +219,71 @@ private function diffForeignKeys(ChangedTable $changedTable)
212219
*/
213220
public function generateMigrationScript(DatabaseDiff $databaseDiff, $displayProgress = false)
214221
{
215-
$migrationScript = '';
216-
$migrationScript .= '# Disable Foreign Keys Check' . PHP_EOL;
217-
$migrationScript .= 'SET FOREIGN_KEY_CHECKS = 0;' . PHP_EOL;
218-
$migrationScript .= 'SET SQL_MODE = \'\';' . PHP_EOL;
222+
return implode(PHP_EOL, $this->generateMigrationScriptArray($databaseDiff, $displayProgress));
223+
}
219224

220-
$migrationScript .= PHP_EOL . '# Deleted Tables' . PHP_EOL;
225+
/**
226+
* @param DatabaseDiff $databaseDiff
227+
* @param bool $displayProgress
228+
*
229+
* @return array
230+
*/
231+
public function generateMigrationScriptArray(DatabaseDiff $databaseDiff, $displayProgress = false)
232+
{
233+
$migrationScript = array();
234+
$migrationScript[] = '# Disable Foreign Keys Check';
235+
$migrationScript[] = 'SET FOREIGN_KEY_CHECKS = 0;';
236+
$migrationScript[] = 'SET SQL_MODE = \'\';';
237+
238+
$migrationScript[] = '';
239+
$migrationScript[] = '# Deleted Tables';
221240
foreach ($databaseDiff->getDeletedTables() as $deletedTable) {
222-
$migrationScript .= PHP_EOL . sprintf('-- deleted table `%s`' . PHP_EOL . PHP_EOL, $deletedTable->getName());
241+
$migrationScript[] = '';
242+
$migrationScript[] = sprintf('-- deleted table `%s`', $deletedTable->getName());
243+
$migrationScript[] = '';
244+
223245

224246
if ($displayProgress) {
225-
$migrationScript .= sprintf("SELECT 'Dropping table %s';" . PHP_EOL, $deletedTable->getName());
247+
$migrationScript[] = sprintf("SELECT 'Dropping table %s';", $deletedTable->getName());
226248
}
227249

228-
$migrationScript .= sprintf('DROP TABLE `%s`;' . PHP_EOL, $deletedTable->getName());
250+
$migrationScript[] = sprintf('DROP TABLE `%s`;', $deletedTable->getName());
229251
}
230252

231-
$migrationScript .= PHP_EOL . '# Changed Tables' . PHP_EOL;
253+
$migrationScript[] = '';
254+
$migrationScript[] = '# Changed Tables';
255+
$migrationScript[] = '';
232256
foreach ($databaseDiff->getChangedTables() as $changedTable) {
233-
$migrationScript .= PHP_EOL . sprintf('-- changed table `%s`' . PHP_EOL . PHP_EOL, $changedTable->getName());
257+
$migrationScript[] = '';
258+
$migrationScript[] = sprintf('-- changed table `%s`', $changedTable->getName());
259+
$migrationScript[] = '';
234260

235261
if ($displayProgress) {
236-
$migrationScript .= sprintf("SELECT 'Altering table %s';" . PHP_EOL, $changedTable->getName());
262+
$migrationScript[] = sprintf("SELECT 'Altering table %s';", $changedTable->getName());
237263
}
238264

239-
$migrationScript .= $changedTable->generateAlterScript() . PHP_EOL;
265+
$migrationScript[] = $changedTable->generateAlterScript();
240266
}
267+
$migrationScript[] = '';
268+
$migrationScript[] = '# New Tables';
269+
$migrationScript[] = '';
241270

242-
$migrationScript .= PHP_EOL . '# New Tables' . PHP_EOL;
243271
foreach ($databaseDiff->getNewTables() as $newTable) {
244-
$migrationScript .= PHP_EOL . sprintf('-- new table `%s`' . PHP_EOL . PHP_EOL, $newTable->getName());
272+
$migrationScript[] = '';
273+
$migrationScript[] = sprintf('-- new table `%s`', $newTable->getName());
274+
$migrationScript[] = '';
245275

246276
if ($displayProgress) {
247-
$migrationScript .= sprintf("SELECT 'Creating table %s';" . PHP_EOL, $newTable->getName());
277+
$migrationScript[] = sprintf("SELECT 'Creating table %s';", $newTable->getName());
248278
}
249279

250-
$migrationScript .= $newTable->generateCreationScript(true) . PHP_EOL;
280+
$migrationScript[] = $newTable->generateCreationScript(true);
251281
}
252282

253-
$migrationScript .= PHP_EOL . '# Disable Foreign Keys Check' . PHP_EOL;
254-
$migrationScript .= 'SET FOREIGN_KEY_CHECKS = 1;' . PHP_EOL;
283+
$migrationScript[] = '';
284+
$migrationScript[] = '# Disable Foreign Keys Check';
285+
$migrationScript[] = '';
286+
$migrationScript[] = 'SET FOREIGN_KEY_CHECKS = 1;';
255287

256288
return $migrationScript;
257289
}

src/Parser.php

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
use Camcima\MySqlDiff\Model\IndexColumn;
1010
use Camcima\MySqlDiff\Model\Table;
1111

12+
/**
13+
* Class Parser
14+
*
15+
* @package Camcima\MySqlDiff
16+
*/
1217
class Parser
1318
{
1419
/**
@@ -41,7 +46,8 @@ public function parseTables($sqlScript)
4146
preg_match_all(RegExpPattern::tables(), $sqlScript, $matches);
4247

4348
$tables = [];
44-
for ($i = 0; $i < count($matches[0]); $i++) {
49+
$loopCounter = count($matches[0]);
50+
for ($i = 0; $i < $loopCounter; $i++) {
4551
$name = $matches['tableName'][$i];
4652
$ifNotExists = $matches['ifNotExists'][$i];
4753
$definition = $matches['tableDefinition'][$i];
@@ -50,6 +56,8 @@ public function parseTables($sqlScript)
5056
$autoIncrement = $matches['autoIncrement'][$i];
5157
$defaultCharset = $matches['defaultCharset'][$i];
5258
$comment = $matches['comment'][$i];
59+
$rowFormat = $matches['rowFormat'][$i];
60+
$keyBlockSize = $matches['keyBlockSize'][$i];
5361

5462
$table = new Table($name);
5563
$table->setDefinition(trim($definition));
@@ -75,6 +83,14 @@ public function parseTables($sqlScript)
7583
$table->setComment(str_replace('\'\'', '\'', $comment));
7684
}
7785

86+
if ($rowFormat) {
87+
$table->setRowFormat($rowFormat);
88+
}
89+
90+
if ($keyBlockSize) {
91+
$table->setKeyBlockSize($keyBlockSize);
92+
}
93+
7894
$tables[$name] = $table;
7995
}
8096

@@ -100,7 +116,8 @@ public function parseColumns(Table $table)
100116
preg_match_all(RegExpPattern::column(), $table->getDefinition(), $matches);
101117

102118
$lastColumn = null;
103-
for ($i = 0; $i < count($matches[0]); $i++) {
119+
$loopCounter = count($matches[0]);
120+
for ($i = 0; $i < $loopCounter; $i++) {
104121
$columnName = $matches['columnName'][$i];
105122
$columnType = $matches['columnType'][$i];
106123
$intLength = $matches['intLength'][$i];
@@ -132,7 +149,7 @@ public function parseColumns(Table $table)
132149

133150
$column->setLength($this->getColumnLength($intLength, $decimalLength, $doubleLength, $floatLength, $charLength, $binaryLength, $yearLength));
134151
$column->setPrecision($this->getColumnPrecision($decimalPrecision, $doublePrecision, $floatPrecision));
135-
$column->setNullable($nullable != 'NOT NULL');
152+
$column->setNullable($nullable !== 'NOT NULL');
136153
$column->setAutoIncrement(!empty($autoIncrement));
137154

138155
if (!empty($defaultValue)) {
@@ -206,7 +223,8 @@ public function parseForeignKeys(Table $table)
206223
{
207224
preg_match_all(RegExpPattern::foreignKey(), $table->getDefinition(), $matches);
208225

209-
for ($i = 0; $i < count($matches[0]); $i++) {
226+
$loopCounter = count($matches[0]);
227+
for ($i = 0; $i < $loopCounter; $i++) {
210228
$name = $matches['name'][$i];
211229
$columnName = $matches['column'][$i];
212230
$referenceTableName = $matches['referenceTable'][$i];
@@ -238,7 +256,8 @@ public function parseIndexes(Table $table)
238256
{
239257
preg_match_all(RegExpPattern::index(), $table->getDefinition(), $matches);
240258

241-
for ($i = 0; $i < count($matches[0]); $i++) {
259+
$loopCounter = count($matches[0]);
260+
for ($i = 0; $i < $loopCounter; $i++) {
242261
$indexName = $matches['name'][$i];
243262
$indexColumnNames = explode(',', str_replace('`', '', $matches['columns'][$i]));
244263
$indexOptions = $matches['options'][$i];
@@ -289,21 +308,26 @@ private function getColumnLength($intLength, $decimalLength, $doubleLength, $flo
289308
{
290309
if (!empty($intLength)) {
291310
return (int) $intLength;
292-
} elseif (!empty($decimalLength)) {
311+
}
312+
if (!empty($decimalLength)) {
293313
return (int) $decimalLength;
294-
} elseif (!empty($doubleLength)) {
314+
}
315+
if (!empty($doubleLength)) {
295316
return (int) $doubleLength;
296-
} elseif (!empty($floatLength)) {
317+
}
318+
if (!empty($floatLength)) {
297319
return (int) $floatLength;
298-
} elseif (!empty($charLength)) {
320+
}
321+
if (!empty($charLength)) {
299322
return (int) $charLength;
300-
} elseif (!empty($binaryLength)) {
323+
}
324+
if (!empty($binaryLength)) {
301325
return (int) $binaryLength;
302-
} elseif (!empty($yearLength)) {
326+
}
327+
if (!empty($yearLength)) {
303328
return (int) $yearLength;
304-
} else {
305-
return;
306329
}
330+
return null;
307331
}
308332

309333
/**
@@ -317,12 +341,13 @@ private function getColumnPrecision($decimalPrecision, $doublePrecision, $floatP
317341
{
318342
if (!empty($decimalPrecision)) {
319343
return (int) $decimalPrecision;
320-
} elseif (!empty($doublePrecision)) {
344+
}
345+
if (!empty($doublePrecision)) {
321346
return (int) $doublePrecision;
322-
} elseif (!empty($floatPrecision)) {
347+
}
348+
if (!empty($floatPrecision)) {
323349
return (int) $floatPrecision;
324-
} else {
325-
return;
326350
}
351+
return null;
327352
}
328353
}

src/RegExpPattern.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
namespace Camcima\MySqlDiff;
44

55

6+
/**
7+
* Class RegExpPattern
8+
*
9+
* @package Camcima\MySqlDiff
10+
*/
611
class RegExpPattern
712
{
13+
/**
14+
* @var array
15+
*/
816
private static $columnTypeRegExps = [
917
'(?:tiny|small|medium|big)?int(?:\((?<intLength>\d+)\))?(?:\s+unsigned)?',
1018
'float(?:\s+unsigned)?(?:\((?<floatLength>\d+),(?<floatPrecision>\d+)\))?',
@@ -40,6 +48,10 @@ public static function tables()
4048
$pattern .= '|';
4149
$pattern .= '(?:DEFAULT CHARSET\s*=\s*(?<defaultCharset>[^;\s]+))?\s*';
4250
$pattern .= '|';
51+
$pattern .= '(?:\s+ROW_FORMAT\s*=\s*(?<rowFormat>[^;\s]+))?\s*';
52+
$pattern .= '|';
53+
$pattern .= '(?:\s+KEY_BLOCK_SIZE\s*=\s*(?<keyBlockSize>[^;\s]+))?\s*';
54+
$pattern .= '|';
4355
$pattern .= '(?:COLLATE\s*=\s*.+?)?\s*';
4456
$pattern .= '|';
4557
$pattern .= '(?:COMMENT\s*=\s*\'(?<comment>([^\']|\'\')+)\')?\s*';

0 commit comments

Comments
 (0)