1717final class Version20260607082210 extends AbstractMigration
1818{
1919 public function up (Schema $ schema ): void
20+ {
21+ do {
22+ $ resultsFound = $ this ->processBatch ();
23+ } while ($ resultsFound );
24+ }
25+
26+ public function processBatch (): bool
2027 {
2128 $ qb = $ this ->connection ->createQueryBuilder ();
2229 $ qb
@@ -25,37 +32,33 @@ public function up(Schema $schema): void
2532 // If this migration times out, this will ensure it can be rerun, and it will continue where it was left, so
2633 // it can be run multiple times until all short URLs have been processed
2734 ->where ($ qb ->expr ()->eq ('long_url_hash ' , ':longUrlHash ' ))
28- ->setParameters (['longUrlHash ' => '' ]);
35+ ->setParameters (['longUrlHash ' => '' ])
36+ ->setMaxResults (10_000 );
2937 $ shortUrlsResult = $ qb ->executeQuery ();
3038
31- $ iteration = 1 ;
32- $ this -> connection -> beginTransaction () ;
39+ return $ this -> connection -> transactional ( function () use ( $ shortUrlsResult ) {
40+ $ resultsFound = false ;
3341
34- while ($ row = $ shortUrlsResult ->fetchAssociative ()) {
35- // Every few updates, commit the transaction and begin a new one
36- if (($ iteration % 10_000 ) === 0 ) {
37- $ this ->connection ->commit ();
38- $ this ->connection ->beginTransaction ();
39- }
40- $ iteration ++;
42+ while ($ row = $ shortUrlsResult ->fetchAssociative ()) {
43+ $ resultsFound = true ;
4144
42- $ updateQb = $ this ->connection ->createQueryBuilder ();
43- $ updateQb
44- ->update ('short_urls ' )
45- ->set ('long_url_hash ' , ':binHash ' )
46- ->where ($ updateQb ->expr ()->eq ('id ' , ':id ' ))
47- ->setParameters ([
48- 'id ' => $ row ['id ' ],
49- 'binHash ' => hex2bin (hash ('sha256 ' , $ row ['original_url ' ])),
50- ], [
51- 'binHash ' => Types::BINARY ,
52- ])
53- ->setMaxResults (1 )
54- ->executeStatement ();
55- }
45+ $ updateQb = $ this ->connection ->createQueryBuilder ();
46+ $ updateQb
47+ ->update ('short_urls ' )
48+ ->set ('long_url_hash ' , ':binHash ' )
49+ ->where ($ updateQb ->expr ()->eq ('id ' , ':id ' ))
50+ ->setParameters ([
51+ 'id ' => $ row ['id ' ],
52+ 'binHash ' => hex2bin (hash ('sha256 ' , $ row ['original_url ' ])),
53+ ], [
54+ 'binHash ' => Types::BINARY ,
55+ ])
56+ ->setMaxResults (1 )
57+ ->executeStatement ();
58+ }
5659
57- // Commit any pending update that is still pending
58- $ this -> connection -> commit ( );
60+ return $ resultsFound ;
61+ } );
5962 }
6063
6164 public function isTransactional (): bool
0 commit comments