jenkins-bot has submitted this change and it was merged.

Change subject: Batch lookup of user ids rather than select per update
......................................................................


Batch lookup of user ids rather than select per update

Fetch all of the local username => local id mappings in a single select
rather than doing one select per global user.

Change also includes some variable name changes to make reading the
source a bit more clear and moves inline comments to their own source
lines.

Co-Authored-By: Bryan Davis <[email protected]>
Change-Id: Ie1084440736cc7e728920351a5ca08ed435c50ca
---
M maintenance/populateLocalAndGlobalIds.php
1 file changed, 34 insertions(+), 10 deletions(-)

Approvals:
  Kaldari: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/maintenance/populateLocalAndGlobalIds.php 
b/maintenance/populateLocalAndGlobalIds.php
index 3f59839..1b8c27f 100644
--- a/maintenance/populateLocalAndGlobalIds.php
+++ b/maintenance/populateLocalAndGlobalIds.php
@@ -22,7 +22,7 @@
                        if ( $wiki == 'enwiki' ) {
                                continue;
                        }
-                       $globalId = -1;
+                       $lastGlobalId = -1;
                        $lb = wfGetLB( $wiki );
                        $ldbr = $lb->getConnection( DB_SLAVE, [], $wiki );
                        do {
@@ -30,28 +30,52 @@
                                        [ 'localuser', 'globaluser' ],
                                        [ 'lu_name', 'gu_id' ],
                                        [
-                                               'gu_id >= ' . $globalId, // 
Start from where we left off in last batch
+                                               // Start from where we left off 
in last batch
+                                               'gu_id >= ' . $lastGlobalId,
                                                'lu_wiki' => $wiki,
-                                               'lu_local_id' => null, // Only 
pick records not already populated
+                                               // Only pick records not 
already populated
+                                               'lu_local_id' => null,
                                                'gu_name = lu_name'
                                        ],
                                        __METHOD__,
                                        [ 'LIMIT' => $this->mBatchSize, 'ORDER 
BY' => 'gu_id ASC' ]
                                );
+                               $numRows = $rows->numRows();
+
+                               $globalUidToLocalName = [];
                                foreach ( $rows as $row ) {
-                                       $globalId = $row->gu_id; // Save this 
so we know where to fetch our next batch from
-                                       $localId = $ldbr->selectField( 'user', 
'user_id', [ 'user_name' => $row->lu_name ] );
+                                       $globalUidToLocalName[$row->gu_id] = 
$row->lu_name;
+                               }
+                               if ( !$globalUidToLocalName ) {
+                                       $this->output( "All users migrated; 
Wiki: $wiki \n" );
+                                       continue;
+                               }
+
+                               $localNameToUid = [];
+                               $localIds = $ldbr->select(
+                                       'user',
+                                       [ 'user_id', 'user_name' ],
+                                       [ 'user_name' => array_values( 
$globalUidToLocalName ) ]
+                               );
+                               foreach ( $localIds as $lid ) {
+                                       $localNameToUid[$lid->user_name] = 
$lid->user_id;
+                               }
+                               foreach ( $globalUidToLocalName as $gid => 
$uname ) {
+                                       // Save progress so we know where to 
start our next batch
+                                       $lastGlobalId = $gid;
                                        $result = $dbw->update(
                                                'localuser',
-                                               [ 'lu_local_id' => $localId, 
'lu_global_id' => $row->gu_id ],
-                                               [ 'lu_name' => $row->lu_name, 
'lu_wiki' => $wiki ]
+                                               [
+                                                       'lu_local_id' => 
$localNameToUid[$uname],
+                                                       'lu_global_id' => $gid
+                                               ],
+                                               [ 'lu_name' => $uname, 
'lu_wiki' => $wiki ]
                                        );
                                        if ( !$result ) {
-                                               $this->output( "Update failed 
for global user $globalId for wiki $wiki \n" );
+                                               $this->output( "Update failed 
for global user $lastGlobalId for wiki $wiki \n" );
                                        }
                                }
-                               $numRows = $rows->numRows();
-                               $this->output( "Updated $numRows records. Last 
user: $globalId; Wiki: $wiki \n" );
+                               $this->output( "Updated $numRows records. Last 
user: $lastGlobalId; Wiki: $wiki \n" );
                                wfWaitForSlaves();
                        } while ( $numRows >= $this->mBatchSize );
                        $lb->reuseConnection( $ldbr );

-- 
To view, visit https://gerrit.wikimedia.org/r/323181
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie1084440736cc7e728920351a5ca08ed435c50ca
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/CentralAuth
Gerrit-Branch: master
Gerrit-Owner: Reedy <[email protected]>
Gerrit-Reviewer: BryanDavis <[email protected]>
Gerrit-Reviewer: Kaldari <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: Niharika29 <[email protected]>
Gerrit-Reviewer: Reedy <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to