Skip to content

Commit df21d44

Browse files
author
Paul Rogers
committed
fix(MigrateDumpCommand): Correct sorting trimmed constraints when multiple blocks of constraints.
1 parent e9d5e0c commit df21d44

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Commands/MigrateDumpCommand.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,15 @@ public static function trimUnderscoresFromForeign(string $sql) : string
177177
);
178178

179179
// Reorder constraints for consistency since dump put underscored first.
180-
if (preg_match_all('/(?:^|,)\s*CONSTRAINT\s+.*?(?:,|\)\s*\))/imu', $trimmed, $m)) {
181-
$constraints_original = implode(PHP_EOL, $m[0]);
180+
$offset = 0;
181+
// Sort each adjacent block of constraints.
182+
while (preg_match('/((?:^|,)?\s*CONSTRAINT\s+.*?(?:,|\)\s*\)))+/imu', $trimmed, $m, PREG_OFFSET_CAPTURE, $offset)) {
183+
// Bump offset to avoid unintentionally reprocessing already sorted.
184+
$offset = $m[count($m) - 1][1] + strlen($m[count($m) - 1][0]);
185+
$constraints_original = $m[0][0];
186+
if (! preg_match_all('/(?:^|,)\s*CONSTRAINT\s+.*?(?:,|\)\s*\))/imu', $constraints_original, $m)) {
187+
continue;
188+
}
182189
$constraints_array = $m[0];
183190
foreach ($constraints_array as &$constraint) {
184191
$constraint = trim($constraint, ",\r\n");

tests/Mysql/MigrateDumpTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,20 @@ public function test_trimUnderscoresFromForeign()
3434
$sql = "KEY z_index,
3535
CONSTRAINT `__b_fk` FOREIGN KEY (`b`) REFERENCES `b` ON(`b`),
3636
CONSTRAINT `a_fk` FOREIGN KEY (`a`) REFERENCES `a` ON(`a`)
37+
);
38+
...KEY z2_index,
39+
CONSTRAINT `__d_fk` FOREIGN KEY (`d`) REFERENCES `d` ON(`d`),
40+
CONSTRAINT `c_fk` FOREIGN KEY (`c`) REFERENCES `c` ON(`c`)
3741
);";
3842
$trimmed = MigrateDumpCommand::trimUnderscoresFromForeign($sql);
3943
$this->assertEquals(
4044
"KEY z_index,
4145
CONSTRAINT `a_fk` FOREIGN KEY (`a`) REFERENCES `a` ON(`a`),
4246
CONSTRAINT `b_fk` FOREIGN KEY (`b`) REFERENCES `b` ON(`b`)
47+
);
48+
...KEY z2_index,
49+
CONSTRAINT `c_fk` FOREIGN KEY (`c`) REFERENCES `c` ON(`c`),
50+
CONSTRAINT `d_fk` FOREIGN KEY (`d`) REFERENCES `d` ON(`d`)
4351
);",
4452
$trimmed
4553
);

0 commit comments

Comments
 (0)