Skip to content

Commit c19c392

Browse files
Change migration batching approach to avoid problematic syntax on MySQL (#2506)
Users have reported compatibility issues with the recent batched update approach used in several migrations, including #2496. MySQL 8.3 does not support LIMIT clauses on multi-table update statements. This PR changes the batching approach used for the two migrations affected by these issues to avoid the problematic syntax.
1 parent c3734da commit c19c392

2 files changed

Lines changed: 19 additions & 33 deletions

File tree

database/migrations/2024_08_24_160326_label2test_relationship_refactor.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public function up(): void
1818
->nullable();
1919
});
2020

21-
if (config('database.default') === 'pgsql') {
22-
$count = (int) DB::select('SELECT count(1) AS c FROM build')[0]->c;
21+
$count = (int) DB::select('SELECT count(1) AS c FROM build')[0]->c;
2322

24-
// Execute at most 10k batches of buildwise updates
23+
// Execute at most 10k batches of buildwise updates
24+
if (config('database.default') === 'pgsql') {
2525
for ($i = 0; $i < ceil($count / 10000); $i++) {
2626
DB::update('
2727
UPDATE label2test
@@ -35,18 +35,16 @@ public function up(): void
3535
', [$i]);
3636
}
3737
} else {
38-
// MySQL is a bit more finicky about large transactions, so update in batches of 100
39-
$rows_changed = 1;
40-
while ($rows_changed > 0) {
41-
$rows_changed = DB::update('
38+
for ($i = 0; $i < ceil($count / 10000); $i++) {
39+
DB::update('
4240
UPDATE label2test, build2test
4341
SET label2test.testid = build2test.id
4442
WHERE
4543
build2test.buildid = label2test.buildid
4644
AND build2test.outputid = label2test.outputid
4745
AND label2test.testid IS NULL
48-
LIMIT 100
49-
');
46+
AND build2test.buildid % ? = 0
47+
', [$i]);
5048
}
5149
}
5250

database/migrations/2024_10_13_160937_buildinformation_to_build_table.php

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,18 @@ public function up(): void
3434
WHERE build.id = buildinformation.buildid
3535
');
3636
} else {
37-
// MySQL is a bit more finicky about large transactions, so update in batches of 100
38-
$rows_changed = 1;
39-
while ($rows_changed > 0) {
40-
$rows_changed = DB::update('
41-
UPDATE build, buildinformation
42-
SET
43-
build.osname = buildinformation.osname,
44-
build.osplatform = buildinformation.osplatform,
45-
build.osrelease = buildinformation.osrelease,
46-
build.osversion = buildinformation.osversion,
47-
build.compilername = buildinformation.compilername,
48-
build.compilerversion = buildinformation.compilerversion
49-
WHERE build.id = buildinformation.buildid
50-
LIMIT 100
51-
');
52-
53-
// Delete the rows we just moved over
54-
DB::delete('
55-
DELETE FROM buildinformation
56-
WHERE buildid IN (
57-
SELECT id FROM build
58-
)
59-
');
60-
}
37+
DB::update('
38+
UPDATE build, buildinformation
39+
SET
40+
build.osname = buildinformation.osname,
41+
build.osplatform = buildinformation.osplatform,
42+
build.osrelease = buildinformation.osrelease,
43+
build.osversion = buildinformation.osversion,
44+
build.compilername = buildinformation.compilername,
45+
build.compilerversion = buildinformation.compilerversion
46+
WHERE
47+
build.id = buildinformation.buildid
48+
');
6149
}
6250

6351
Schema::dropIfExists('buildinformation');

0 commit comments

Comments
 (0)