Skip to content

Commit

Permalink
Change migration batching approach to avoid problematic syntax on MyS…
Browse files Browse the repository at this point in the history
…QL (#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.
  • Loading branch information
williamjallen authored Oct 21, 2024
1 parent c3734da commit c19c392
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public function up(): void
->nullable();
});

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

// Execute at most 10k batches of buildwise updates
// Execute at most 10k batches of buildwise updates
if (config('database.default') === 'pgsql') {
for ($i = 0; $i < ceil($count / 10000); $i++) {
DB::update('
UPDATE label2test
Expand All @@ -35,18 +35,16 @@ public function up(): void
', [$i]);
}
} else {
// MySQL is a bit more finicky about large transactions, so update in batches of 100
$rows_changed = 1;
while ($rows_changed > 0) {
$rows_changed = DB::update('
for ($i = 0; $i < ceil($count / 10000); $i++) {
DB::update('
UPDATE label2test, build2test
SET label2test.testid = build2test.id
WHERE
build2test.buildid = label2test.buildid
AND build2test.outputid = label2test.outputid
AND label2test.testid IS NULL
LIMIT 100
');
AND build2test.buildid % ? = 0
', [$i]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,18 @@ public function up(): void
WHERE build.id = buildinformation.buildid
');
} else {
// MySQL is a bit more finicky about large transactions, so update in batches of 100
$rows_changed = 1;
while ($rows_changed > 0) {
$rows_changed = DB::update('
UPDATE build, buildinformation
SET
build.osname = buildinformation.osname,
build.osplatform = buildinformation.osplatform,
build.osrelease = buildinformation.osrelease,
build.osversion = buildinformation.osversion,
build.compilername = buildinformation.compilername,
build.compilerversion = buildinformation.compilerversion
WHERE build.id = buildinformation.buildid
LIMIT 100
');

// Delete the rows we just moved over
DB::delete('
DELETE FROM buildinformation
WHERE buildid IN (
SELECT id FROM build
)
');
}
DB::update('
UPDATE build, buildinformation
SET
build.osname = buildinformation.osname,
build.osplatform = buildinformation.osplatform,
build.osrelease = buildinformation.osrelease,
build.osversion = buildinformation.osversion,
build.compilername = buildinformation.compilername,
build.compilerversion = buildinformation.compilerversion
WHERE
build.id = buildinformation.buildid
');
}

Schema::dropIfExists('buildinformation');
Expand Down

0 comments on commit c19c392

Please sign in to comment.