Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config/migration-snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,18 @@
*/

'reorder' => env('MIGRATION_SNAPSHOT_REORDER', false),

/*
|--------------------------------------------------------------------------
| Whether to trim underscores from foreign constraints for consistency.
|--------------------------------------------------------------------------
|
| Percona's Online Schema Change for Mysql may prepend foreign constraints
| with underscores. Since it may not be used in all environments some dumped
| snapshots may not match, adding unnecessary noise to source control.
| Enable this trimming to get more consistent snapshots when PTOSC may be
| used.
|
*/
'trim-underscores' => env('MIGRATION_SNAPSHOT_TRIM_UNDERSCORES', false),
];
50 changes: 50 additions & 0 deletions src/Commands/MigrateDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ private static function mysqlDump(array $db_config, string $schema_sql_path) : i
return 1;
}
$schema_sql = preg_replace('/\s+AUTO_INCREMENT=[0-9]+/iu', '', $schema_sql);
$schema_sql = self::trimUnderscoresFromForeign($schema_sql);
if (false === file_put_contents($schema_sql_path, $schema_sql)) {
return 1;
}
Expand Down Expand Up @@ -156,6 +157,55 @@ private static function mysqlDump(array $db_config, string $schema_sql_path) : i
return $exit_code;
}

/**
* Trim underscores from FK constraint names to workaround PTOSC quirk.
*
* @param string $sql like "CONSTRAINT _my_fk FOREIGN KEY ..."
*
* @return string without leading underscores like "CONSTRAINT my_fk ...".
*/
public static function trimUnderscoresFromForeign(string $sql) : string
{
if (! config('migration-snapshot.trim-underscores')) {
return $sql;
}

$trimmed = preg_replace(
'/(^|,)(\s*CONSTRAINT\s+[`"]?)_+(.*?[`"]?\s+FOREIGN\s+KEY\b.*)/imu',
'\1\2\3',
$sql
);

// Reorder constraints for consistency since dump put underscored first.
$offset = 0;
// Sort each adjacent block of constraints.
while (preg_match('/((?:^|,)?\s*CONSTRAINT\s+.*?(?:,|\)\s*\)))+/imu', $trimmed, $m, PREG_OFFSET_CAPTURE, $offset)) {
// Bump offset to avoid unintentionally reprocessing already sorted.
$offset = $m[count($m) - 1][1] + strlen($m[count($m) - 1][0]);
$constraints_original = $m[0][0];
if (! preg_match_all('/(?:^|,)\s*CONSTRAINT\s+.*?(?:,|\)\s*\))/imu', $constraints_original, $m)) {
continue;
}
$constraints_array = $m[0];
foreach ($constraints_array as &$constraint) {
$constraint = trim($constraint, ",\r\n");
// Trim extra parenthesis at the end of table definitions.
$constraint = preg_replace('/(\s*\))\s*\)\z/imu', '\1', $constraint, 1);
}
sort($constraints_array);
$separator = ',' . PHP_EOL;
// Comma or "\n)".
$terminator = preg_match('/(,|\s*\))\z/imu', $constraints_original, $m)
? $m[1] : '';
$constraints_sorted = $separator
. implode($separator, $constraints_array)
. $terminator;
$trimmed = str_replace($constraints_original, $constraints_sorted, $trimmed);
}

return $trimmed;
}

/**
* @param array $db_config like ['host' => , 'port' => ].
*
Expand Down
25 changes: 25 additions & 0 deletions tests/Mysql/MigrateDumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MigrateDumpTest extends TestCase
protected function getEnvironmentSetUp($app)
{
$app['config']->set('migration-snapshot.reorder', true);
$app['config']->set('migration-snapshot.trim-underscores', true);
}

public function test_handle()
Expand All @@ -28,6 +29,30 @@ public function test_handle()
$this->assertRegExp("/[\r\n]\z/mu", $last_character);
}

public function test_trimUnderscoresFromForeign()
{
$sql = "KEY z_index,
CONSTRAINT `__b_fk` FOREIGN KEY (`b`) REFERENCES `b` ON(`b`),
CONSTRAINT `a_fk` FOREIGN KEY (`a`) REFERENCES `a` ON(`a`)
);
...KEY z2_index,
CONSTRAINT `__d_fk` FOREIGN KEY (`d`) REFERENCES `d` ON(`d`),
CONSTRAINT `c_fk` FOREIGN KEY (`c`) REFERENCES `c` ON(`c`)
);";
$trimmed = MigrateDumpCommand::trimUnderscoresFromForeign($sql);
$this->assertEquals(
"KEY z_index,
CONSTRAINT `a_fk` FOREIGN KEY (`a`) REFERENCES `a` ON(`a`),
CONSTRAINT `b_fk` FOREIGN KEY (`b`) REFERENCES `b` ON(`b`)
);
...KEY z2_index,
CONSTRAINT `c_fk` FOREIGN KEY (`c`) REFERENCES `c` ON(`c`),
CONSTRAINT `d_fk` FOREIGN KEY (`d`) REFERENCES `d` ON(`d`)
);",
$trimmed
);
}

public function test_reorderMigrationRows()
{
$output = [
Expand Down