Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
FlowUpdateRevisionTypeId
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 4
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 doDBUpdates
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
42
 updateRevision
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 getUpdateKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Flow\Maintenance;
4
5use Flow\Container;
6use Flow\DbFactory;
7use LoggedUpdateMaintenance;
8use Wikimedia\Rdbms\IDatabase;
9
10$IP = getenv( 'MW_INSTALL_PATH' );
11if ( $IP === false ) {
12    $IP = __DIR__ . '/../../..';
13}
14
15require_once "$IP/maintenance/Maintenance.php";
16
17/**
18 * Update flow_revision.rev_type_id
19 *
20 * @ingroup Maintenance
21 */
22class FlowUpdateRevisionTypeId extends LoggedUpdateMaintenance {
23
24    public function __construct() {
25        parent::__construct();
26        $this->addDescription( "Update flow_revision.rev_type_id" );
27        $this->requireExtension( 'Flow' );
28        $this->setBatchSize( 300 );
29    }
30
31    protected function doDBUpdates() {
32        $revId = '';
33        $batchSize = $this->getBatchSize();
34        $count = $this->getBatchSize();
35        /** @var DbFactory $dbFactory */
36        $dbFactory = Container::get( 'db.factory' );
37        $dbr = $dbFactory->getDB( DB_REPLICA );
38        $dbw = $dbFactory->getDB( DB_PRIMARY );
39
40        // If table flow_header_revision does not exist, that means the wiki
41        // has run the data migration before or the wiki starts from scratch,
42        // there is no point to run the script against invalid tables
43        if ( !$dbr->tableExists( 'flow_header_revision', __METHOD__ ) ) {
44            return true;
45        }
46
47        while ( $count == $batchSize ) {
48            $count = 0;
49            $res = $dbr->newSelectQueryBuilder()
50                ->select( [ 'rev_id', 'rev_type', 'tree_rev_descendant_id', 'header_workflow_id' ] )
51                ->from( 'flow_revision' )
52                ->leftJoin( 'flow_tree_revision', null, 'rev_id=tree_rev_id' )
53                ->leftJoin( 'flow_header_revision', null, 'rev_id=header_rev_id' )
54                ->where( $dbr->expr( 'rev_id', '>', $revId ) )
55                ->orderBy( 'rev_id' )
56                ->limit( $batchSize )
57                ->caller( __METHOD__ )
58                ->fetchResultSet();
59
60            foreach ( $res as $row ) {
61                $count++;
62                $revId = $row->rev_id;
63                switch ( $row->rev_type ) {
64                    case 'header':
65                        $this->updateRevision( $dbw, $row->rev_id, $row->header_workflow_id );
66                        break;
67                    case 'post':
68                        $this->updateRevision( $dbw, $row->rev_id, $row->tree_rev_descendant_id );
69                        break;
70                }
71            }
72            $dbFactory->waitForReplicas();
73        }
74
75        $dbw->dropTable( 'flow_header_revision', __METHOD__ );
76
77        return true;
78    }
79
80    /**
81     * @param IDatabase $dbw
82     * @param int $revId
83     * @param string $revTypeId
84     */
85    private function updateRevision( $dbw, $revId, $revTypeId ) {
86        if ( $revTypeId === null ) {
87            // this shouldn't actually be happening, but if it is, ignoring it
88            // will not make things worse - the revision is lost already
89            return;
90        }
91
92        $dbw->newUpdateQueryBuilder()
93            ->update( 'flow_revision' )
94            ->set( [ 'rev_type_id' => $revTypeId ] )
95            ->where( [ 'rev_id' => $revId ] )
96            ->caller( __METHOD__ )
97            ->execute();
98    }
99
100    /**
101     * Get the update key name to go in the update log table
102     *
103     * @return string
104     */
105    protected function getUpdateKey() {
106        return 'FlowUpdateRevisionTypeId';
107    }
108}
109
110$maintClass = FlowUpdateRevisionTypeId::class;
111require_once RUN_MAINTENANCE_IF_MAIN;