MediaWiki master
PostgresUpdater.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Installer;
11
19
27
31 protected $db;
32
36 protected function getCoreUpdateList() {
37 return [
38 // 1.40
39 [ 'addField', 'externallinks', 'el_to_path', 'patch-externallinks-el_to_path.sql' ],
40
41 // 1.41
42 [ 'addField', 'user', 'user_is_temp', 'patch-user-user_is_temp.sql' ],
43 [ 'runMaintenance', MigrateRevisionCommentTemp::class ],
44 [ 'dropTable', 'revision_comment_temp' ],
45 [ 'runMaintenance', MigrateExternallinks::class ],
46 [ 'modifyField', 'externallinks', 'el_to', 'patch-externallinks-el_to_default.sql' ],
47 [ 'addField', 'pagelinks', 'pl_target_id', 'patch-pagelinks-target_id.sql' ],
48 [ 'dropField', 'externallinks', 'el_to', 'patch-externallinks-drop-el_to.sql' ],
49 [ 'runMaintenance', FixInconsistentRedirects::class ],
50 [ 'modifyField', 'image', 'img_size', 'patch-image-img_size_to_bigint.sql' ],
51 [ 'modifyField', 'filearchive', 'fa_size', 'patch-filearchive-fa_size_to_bigint.sql' ],
52 [ 'modifyField', 'oldimage', 'oi_size', 'patch-oldimage-oi_size_to_bigint.sql' ],
53 [ 'modifyField', 'uploadstash', 'us_size', 'patch-uploadstash-us_size_to_bigint.sql' ],
54
55 // 1.42
56 [ 'addField', 'user_autocreate_serial', 'uas_year', 'patch-user_autocreate_serial-uas_year.sql' ],
57 [ 'addTable', 'block_target', 'patch-block_target.sql' ],
58 [ 'dropIndex', 'categorylinks', 'cl_collation_ext', 'patch-drop-cl_collation_ext.sql' ],
59 [ 'runMaintenance', PopulateUserIsTemp::class ],
60 // Attempt a rename from `site_groups` to `site_group` prior to dropping the `sites` indexes,
61 // due to a typo in the original index-renaming database update from MW 1.36. (T374042)
62 [ 'renameIndex', 'sites', 'sites_group', 'site_group' ],
63 [ 'dropIndex', 'sites', 'site_type', 'patch-sites-drop_indexes.sql' ],
64 // Re-attempt to drop most of the dropped `sites`-table indexes:
65 // If a Postgres wiki previously ran `update.php` on MW 1.42 or above, the script would have errored after
66 // reaching the "DROP INDEX site_group;" line of `patch-sites-drop_indexes.sql` (due to a previous
67 // index-renaming typo from MW 1.36).
68 // However, as the `site_type` index itself _would_ have been successfully dropped, the `.sql` file listed
69 // in the `dropIndex` line above will not be re-applied on any future runs of `update.php`.
70 // Therefore, to ensure that the remaining indexes are definitely dropped on these wikis, we need to
71 // separately attempt to drop them again. (T374042; T374042#11017896)
72 [ 'dropIndex', 'sites', 'site_group', 'patch-sites-drop_site_group_index.sql' ],
73 [ 'dropIndex', 'sites', 'site_source', 'patch-sites-drop_site_source_index.sql' ],
74 [ 'dropIndex', 'sites', 'site_language', 'patch-sites-drop_site_language_index.sql' ],
75 [ 'dropIndex', 'sites', 'site_protocol', 'patch-sites-drop_site_protocol_index.sql' ],
76 [ 'dropIndex', 'sites', 'site_domain', 'patch-sites-drop_site_domain_index.sql' ],
77 [ 'dropIndex', 'sites', 'site_forward', 'patch-sites-drop_site_forward_index.sql' ],
78 [ 'dropIndex', 'iwlinks', 'iwl_prefix_from_title', 'patch-iwlinks-drop-iwl_prefix_from_title.sql' ],
79
80 // 1.43
81 [ 'migratePagelinks' ],
82 [ 'dropDefault', 'revision', 'rev_actor' ],
83 [ 'dropDefault', 'revision', 'rev_comment_id' ],
84 [ 'changeField', 'revision', 'rev_id', 'BIGINT', '' ],
85 [ 'changeField', 'revision', 'rev_parent_id', 'BIGINT', '' ],
86 [ 'changeField', 'recentchanges', 'rc_id', 'BIGINT', '' ],
87 [ 'changeField', 'change_tag', 'ct_rc_id', 'BIGINT', '' ],
88 [ 'runMaintenance', \MigrateBlocks::class ],
89 [ 'dropTable', 'ipblocks' ],
90 [ 'dropField', 'pagelinks', 'pl_title', 'patch-pagelinks-drop-pl_title.sql' ],
91 [ 'addPostDatabaseUpdateMaintenance', FixAutoblockLogTitles::class ],
92 [ 'migrateSearchindex' ],
93
94 // 1.44
95 [ 'addTable', 'file', 'patch-file.sql' ],
96 [ 'addField', 'categorylinks', 'cl_target_id', 'patch-categorylinks-target_id.sql' ],
97 [ 'addTable', 'collation', 'patch-collation.sql' ],
98 [ 'dropTable', 'module_deps' ],
99
100 // 1.45
101 [ 'addTable', 'existencelinks', 'patch-existencelinks.sql' ],
102 [ 'runMaintenance', FixWrongPasswordPrefixes::class ],
103 [ 'addIndex', 'categorylinks', 'cl_timestamp_id', 'patch-categorylinks-cl_timestamp_id.sql' ],
104 [ 'migrateCategorylinks' ],
105 [ 'normalizeCollation' ],
106 [ 'modifyPrimaryKey', 'categorylinks', [ 'cl_from', 'cl_target_id' ], 'patch-categorylinks-pk.sql' ],
107 [ 'addIndex', 'recentchanges', 'rc_source_name_timestamp',
108 'patch-recentchanges-rc_source_name_timestamp.sql' ],
109 [ 'addIndex', 'recentchanges', 'rc_name_source_patrolled_timestamp',
110 'patch-recentchanges-rc_name_source_patrolled_timestamp.sql' ],
111 [ 'dropField', 'recentchanges', 'rc_new', 'patch-recentchanges-drop-rc_new.sql' ],
112 [ 'dropField', 'categorylinks', 'cl_to', 'patch-categorylinks-drop-cl_to-cl_collation.sql' ],
113
114 // 1.46
115 [ 'addTable', 'watchlist_label', 'patch-watchlist_label.sql' ],
116 [ 'dropField', 'recentchanges', 'rc_type', 'patch-recentchanges-drop-rc_type.sql' ],
117 [ 'dropField', 'archive', 'ar_sha1', 'patch-archive-drop-ar_sha1.sql' ],
118 [ 'dropField', 'revision', 'rev_sha1', 'patch-revision-drop-rev_sha1.sql' ],
119 [ 'dropField', 'objectcache', 'modtoken', 'patch-objectcache-drop-modtoken.sql' ],
120 [ 'addField', 'imagelinks', 'il_target_id', 'patch-imagelinks-add-il_target_id.sql' ],
121 ];
122 }
123
125 protected function getInitialUpdateKeys() {
126 return [
127 'filearchive-fa_major_mime-patch-fa_major_mime-chemical.sql',
128 'image-img_major_mime-patch-img_major_mime-chemical.sql',
129 'oldimage-oi_major_mime-patch-oi_major_mime-chemical.sql',
130 'user_groups-ug_group-patch-ug_group-length-increase-255.sql',
131 'user_former_groups-ufg_group-patch-ufg_group-length-increase-255.sql',
132 'user_properties-up_property-patch-up_property.sql',
133 ];
134 }
135
140 protected function describeTable( $table ) {
141 $q = <<<END
142SELECT attname, attnum FROM pg_namespace, pg_class, pg_attribute
143 WHERE pg_class.relnamespace = pg_namespace.oid
144 AND attrelid=pg_class.oid AND attnum > 0
145 AND relname=%s AND nspname=%s
146END;
147 $res = $this->db->query(
148 sprintf( $q,
149 $this->db->addQuotes( $table ),
150 $this->db->addQuotes( $this->db->getCoreSchema() )
151 ),
152 __METHOD__
153 );
154 if ( !$res ) {
155 return null;
156 }
157
158 $cols = [];
159 foreach ( $res as $r ) {
160 $cols[] = [
161 "name" => $r[0],
162 "ord" => $r[1],
163 ];
164 }
165
166 return $cols;
167 }
168
173 protected function describeIndex( $idx ) {
174 // first fetch the key (which is a list of columns ords) and
175 // the table the index applies to (an oid)
176 $q = <<<END
177SELECT indkey, indrelid FROM pg_namespace, pg_class, pg_index
178 WHERE nspname=%s
179 AND pg_class.relnamespace = pg_namespace.oid
180 AND relname=%s
181 AND indexrelid=pg_class.oid
182END;
183 $res = $this->db->query(
184 sprintf(
185 $q,
186 $this->db->addQuotes( $this->db->getCoreSchema() ),
187 $this->db->addQuotes( $idx )
188 ),
189 __METHOD__
190 );
191 if ( !$res ) {
192 return null;
193 }
194 $r = $res->fetchRow();
195 if ( !$r ) {
196 return null;
197 }
198
199 $indkey = $r[0];
200 $relid = intval( $r[1] );
201 $indkeys = explode( ' ', $indkey );
202
203 $colnames = [];
204 foreach ( $indkeys as $rid ) {
205 $query = <<<END
206SELECT attname FROM pg_class, pg_attribute
207 WHERE attrelid=$relid
208 AND attnum=%d
209 AND attrelid=pg_class.oid
210END;
211 $r2 = $this->db->query( sprintf( $query, $rid ), __METHOD__ );
212 if ( !$r2 ) {
213 return null;
214 }
215 $row2 = $r2->fetchRow();
216 if ( !$row2 ) {
217 return null;
218 }
219 $colnames[] = $row2[0];
220 }
221
222 return $colnames;
223 }
224
229 protected function fkeyDeltype( $fkey ) {
230 $q = <<<END
231SELECT confdeltype FROM pg_constraint, pg_namespace
232 WHERE connamespace=pg_namespace.oid
233 AND nspname=%s
234 AND conname=%s;
235END;
236 $r = $this->db->query(
237 sprintf(
238 $q,
239 $this->db->addQuotes( $this->db->getCoreSchema() ),
240 $this->db->addQuotes( $fkey )
241 ),
242 __METHOD__
243 );
244 $row = $r->fetchRow();
245 return $row ? $row[0] : null;
246 }
247
253 protected function ruleDef( $table, $rule ) {
254 $q = <<<END
255SELECT definition FROM pg_rules
256 WHERE schemaname = %s
257 AND tablename = %s
258 AND rulename = %s
259END;
260 $r = $this->db->query(
261 sprintf(
262 $q,
263 $this->db->addQuotes( $this->db->getCoreSchema() ),
264 $this->db->addQuotes( $table ),
265 $this->db->addQuotes( $rule )
266 ),
267 __METHOD__
268 );
269 $row = $r->fetchRow();
270 if ( !$row ) {
271 return null;
272 }
273 $d = $row[0];
274
275 return $d;
276 }
277
283 protected function addSequence( $table, $pkey, $ns ) {
284 if ( !$this->db->sequenceExists( $ns ) ) {
285 $this->output( "Creating sequence $ns\n" );
286 if ( $pkey !== false ) {
287 $table = $this->db->addIdentifierQuotes( $table );
288 $this->db->query( "CREATE SEQUENCE $ns OWNED BY $table.$pkey", __METHOD__ );
289 $this->setDefault( $table, $pkey, '"nextval"(\'"' . $ns . '"\'::"regclass")' );
290 } else {
291 $this->db->query( "CREATE SEQUENCE $ns", __METHOD__ );
292 }
293 }
294 }
295
300 protected function dropSequence( $table, $ns ) {
301 if ( $this->db->sequenceExists( $ns ) ) {
302 $this->output( "Dropping sequence $ns\n" );
303 $this->db->query( "DROP SEQUENCE $ns CASCADE", __METHOD__ );
304 }
305 }
306
311 protected function renameSequence( $old, $new ) {
312 if ( $this->db->sequenceExists( $new ) ) {
313 $this->output( "...sequence $new already exists.\n" );
314
315 return;
316 }
317 if ( $this->db->sequenceExists( $old ) ) {
318 $this->output( "Renaming sequence $old to $new\n" );
319 $this->db->query( "ALTER SEQUENCE $old RENAME TO $new", __METHOD__ );
320 }
321 }
322
328 protected function setSequenceOwner( $table, $pkey, $seq ) {
329 if ( $this->db->sequenceExists( $seq ) ) {
330 $this->output( "Setting sequence $seq owner to $table.$pkey\n" );
331 $table = $this->db->addIdentifierQuotes( $table );
332 $this->db->query( "ALTER SEQUENCE $seq OWNED BY $table.$pkey", __METHOD__ );
333 }
334 }
335
341 protected function renameTable( $old, $new, $patch = false ) {
342 if ( $this->db->tableExists( $old, __METHOD__ ) ) {
343 $this->output( "Renaming table $old to $new\n" );
344 $old = $this->db->addIdentifierQuotes( $old );
345 $new = $this->db->addIdentifierQuotes( $new );
346 $this->db->query( "ALTER TABLE $old RENAME TO $new", __METHOD__ );
347 if ( $patch !== false ) {
348 $this->applyPatch( $patch );
349 }
350 }
351 }
352
362 protected function renameIndex(
363 $table, $old, $new, $skipBothIndexExistWarning = false, $a = false, $b = false
364 ) {
365 // First requirement: the table must exist
366 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
367 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
368
369 return true;
370 }
371
372 // Second requirement: the new index must be missing
373 if ( $this->db->indexExists( $table, $new, __METHOD__ ) ) {
374 $this->output( "...index $new already set on $table table.\n" );
375 if ( !$skipBothIndexExistWarning
376 && $this->db->indexExists( $table, $old, __METHOD__ )
377 ) {
378 $this->output( "...WARNING: $old still exists, despite it has been " .
379 "renamed into $new (which also exists).\n" .
380 " $old should be manually removed if not needed anymore.\n" );
381 }
382
383 return true;
384 }
385
386 // Third requirement: the old index must exist
387 if ( !$this->db->indexExists( $table, $old, __METHOD__ ) ) {
388 $this->output( "...skipping: index $old doesn't exist.\n" );
389
390 return true;
391 }
392
393 $this->db->query( "ALTER INDEX $old RENAME TO $new", __METHOD__ );
394 return true;
395 }
396
401 protected function dropPgField( $table, $field ) {
402 $fi = $this->db->fieldInfo( $table, $field );
403 if ( $fi === null ) {
404 $this->output( "...$table table does not contain $field field.\n" );
405 } else {
406 $this->output( "Dropping column '$table.$field'\n" );
407 $table = $this->db->addIdentifierQuotes( $table );
408 $this->db->query( "ALTER TABLE $table DROP COLUMN $field", __METHOD__ );
409 }
410 }
411
417 protected function addPgField( $table, $field, $type ) {
418 $fi = $this->db->fieldInfo( $table, $field );
419 if ( $fi !== null ) {
420 $this->output( "...column '$table.$field' already exists\n" );
421 } else {
422 $this->output( "Adding column '$table.$field'\n" );
423 $table = $this->db->addIdentifierQuotes( $table );
424 $this->db->query( "ALTER TABLE $table ADD $field $type", __METHOD__ );
425 }
426 }
427
434 protected function changeField( $table, $field, $newtype, $default ) {
435 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
436 $this->output( "...$table table does not exist, skipping default change.\n" );
437 return;
438 }
439 $fi = $this->db->fieldInfo( $table, $field );
440 if ( $fi === null ) {
441 $this->output( "...ERROR: expected column $table.$field to exist\n" );
442 exit( 1 );
443 }
444
445 if ( $fi->type() === strtolower( $newtype ) ) {
446 $this->output( "...column '$table.$field' is already of type '$newtype'\n" );
447 } else {
448 $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" );
449 $table = $this->db->addIdentifierQuotes( $table );
450 $sql = "ALTER TABLE $table ALTER $field TYPE $newtype";
451 if ( $default !== '' ) {
452 $res = [];
453 if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) {
454 $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]";
455 $this->db->query( $sqldef, __METHOD__ );
456 $default = preg_replace( '/\s*DEFAULT .+/', '', $default );
457 }
458 $sql .= " USING $default";
459 }
460 $this->db->query( $sql, __METHOD__ );
461 }
462 }
463
470 protected function changeFieldPurgeTable( $table, $field, $newtype, $default ) {
471 # # For a cache table, empty it if the field needs to be changed, because the old contents
472 # # may be corrupted. If the column is already the desired type, refrain from purging.
473 $fi = $this->db->fieldInfo( $table, $field );
474 if ( $fi === null ) {
475 $this->output( "...ERROR: expected column $table.$field to exist\n" );
476 exit( 1 );
477 }
478
479 if ( $fi->type() === $newtype ) {
480 $this->output( "...column '$table.$field' is already of type '$newtype'\n" );
481 } else {
482 $this->output( "Purging data from cache table '$table'\n" );
483 $table = $this->db->addIdentifierQuotes( $table );
484 $this->db->query( "DELETE from $table", __METHOD__ );
485 $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" );
486 $sql = "ALTER TABLE $table ALTER $field TYPE $newtype";
487 if ( strlen( $default ) ) {
488 $res = [];
489 if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) {
490 $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]";
491 $this->db->query( $sqldef, __METHOD__ );
492 $default = preg_replace( '/\s*DEFAULT .+/', '', $default );
493 }
494 $sql .= " USING $default";
495 }
496 $this->db->query( $sql, __METHOD__ );
497 }
498 }
499
505 protected function setDefault( $table, $field, $default ) {
506 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
507 $this->output( "...$table table does not exist, skipping default change.\n" );
508 return;
509 }
510 $info = $this->db->fieldInfo( $table, $field );
511 if ( $info && $info->defaultValue() !== $default ) {
512 $this->output( "Changing '$table.$field' default value\n" );
513 $table = $this->db->addIdentifierQuotes( $table );
514 $this->db->query( "ALTER TABLE $table ALTER $field SET DEFAULT "
515 . $this->db->addQuotes( $default ), __METHOD__ );
516 }
517 }
518
525 protected function dropDefault( $table, $field ) {
526 $info = $this->db->fieldInfo( $table, $field );
527 if ( $info && $info->defaultValue() !== false ) {
528 $this->output( "Removing '$table.$field' default value\n" );
529 $table = $this->db->addIdentifierQuotes( $table );
530 $this->db->query( "ALTER TABLE $table ALTER $field DROP DEFAULT", __METHOD__ );
531 }
532 }
533
540 protected function changeNullableField( $table, $field, $null, $update = false ) {
541 $fi = $this->db->fieldInfo( $table, $field );
542 if ( $fi === null ) {
543 return;
544 }
545 if ( $fi->isNullable() ) {
546 # # It's NULL - does it need to be NOT NULL?
547 if ( $null === 'NOT NULL' ) {
548 $this->output( "Changing '$table.$field' to not allow NULLs\n" );
549 $table = $this->db->addIdentifierQuotes( $table );
550 if ( $update ) {
551 $this->db->query( "UPDATE $table SET $field = DEFAULT WHERE $field IS NULL", __METHOD__ );
552 }
553 $this->db->query( "ALTER TABLE $table ALTER $field SET NOT NULL", __METHOD__ );
554 } else {
555 $this->output( "...column '$table.$field' is already set as NULL\n" );
556 }
557 } else {
558 # # It's NOT NULL - does it need to be NULL?
559 if ( $null === 'NULL' ) {
560 $this->output( "Changing '$table.$field' to allow NULLs\n" );
561 $table = $this->db->addIdentifierQuotes( $table );
562 $this->db->query( "ALTER TABLE $table ALTER $field DROP NOT NULL", __METHOD__ );
563 } else {
564 $this->output( "...column '$table.$field' is already set as NOT NULL\n" );
565 }
566 }
567 }
568
575 protected function addPgIndex( $table, $index, $type, $unique = false ) {
576 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
577 $this->output( "...$table table does not exist, skipping index.\n" );
578 } elseif ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
579 $this->output( "...index '$index' on table '$table' already exists\n" );
580 } else {
581 $this->output( "Creating index '$index' on table '$table' $type\n" );
582 $table = $this->db->addIdentifierQuotes( $table );
583 $unique = $unique ? 'UNIQUE' : '';
584 $this->db->query( "CREATE $unique INDEX $index ON $table $type", __METHOD__ );
585 }
586 }
587
593 protected function addPgExtIndex( $table, $index, $type ) {
594 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
595 $this->output( "...index '$index' on table '$table' already exists\n" );
596 } elseif ( preg_match( '/^\‍(/', $type ) ) {
597 $this->output( "Creating index '$index' on table '$table'\n" );
598 $table = $this->db->addIdentifierQuotes( $table );
599 $this->db->query( "CREATE INDEX $index ON $table $type", __METHOD__ );
600 } else {
601 $this->applyPatch( $type, true, "Creating index '$index' on table '$table'" );
602 }
603 }
604
609 protected function dropFkey( $table, $field ) {
610 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
611 $this->output( "...$table table does not exist, skipping constraint change.\n" );
612 return;
613 }
614 $fi = $this->db->fieldInfo( $table, $field );
615 if ( $fi === null ) {
616 $this->output( "WARNING! Column '$table.$field' does not exist but it should! " .
617 "Please report this.\n" );
618 return;
619 }
620
621 if ( $this->dropConstraint( $table, $field, 'foreignkey', $fi->conname() ) ) {
622 $this->output( "Dropping foreign key constraint on '$table.$field'\n" );
623 } else {
624 $this->output( "...foreign key constraint on '$table.$field' already does not exist\n" );
625 }
626 }
627
633 protected function changeFkeyDeferrable( $table, $field, $clause ) {
634 $fi = $this->db->fieldInfo( $table, $field );
635 if ( $fi === null ) {
636 $this->output( "WARNING! Column '$table.$field' does not exist but it should! " .
637 "Please report this.\n" );
638
639 return;
640 }
641 if ( $fi->is_deferred() && $fi->is_deferrable() ) {
642 return;
643 }
644 $this->output( "Altering column '$table.$field' to be DEFERRABLE INITIALLY DEFERRED\n" );
645
646 $conname = $fi->conname();
647 $conclause = "CONSTRAINT \"$conname\"";
648
649 if ( !$this->dropConstraint( $table, $field, 'foreignkey', $conname ) ) {
650 $this->output( "Column '$table.$field' does not have a foreign key " .
651 "constraint, will be added\n" );
652 $conclause = "";
653 }
654
655 $command =
656 "ALTER TABLE $table ADD $conclause " .
657 "FOREIGN KEY ($field) REFERENCES $clause DEFERRABLE INITIALLY DEFERRED";
658 $this->db->query( $command, __METHOD__ );
659 }
660
665 protected function dropPgIndex( $table, $index ) {
666 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
667 $this->output( "Dropping obsolete index '$index'\n" );
668 $this->db->query( "DROP INDEX \"" . $index . "\"", __METHOD__ );
669 }
670 }
671
677 protected function checkIndex( $index, $should_be, $good_def ) {
678 $pu = $this->db->indexAttributes( $index );
679 if ( $pu && $pu != $should_be ) {
680 $this->output( "Dropping obsolete version of index '$index'\n" );
681 $this->db->query( "DROP INDEX \"" . $index . "\"", __METHOD__ );
682 $pu = [];
683 } else {
684 $this->output( "...no need to drop index '$index'\n" );
685 }
686
687 if ( !$pu ) {
688 $this->output( "Creating index '$index'\n" );
689 $this->db->query( $good_def, __METHOD__ );
690 } else {
691 $this->output( "...index '$index' exists\n" );
692 }
693 }
694
700 protected function changePrimaryKey( $table, $shouldBe, $constraintName = null ) {
701 // https://wiki.postgresql.org/wiki/Retrieve_primary_key_columns
702 $result = $this->db->query(
703 "SELECT a.attname as column " .
704 "FROM pg_index i " .
705 "JOIN pg_attribute a ON a.attrelid = i.indrelid " .
706 "AND a.attnum = ANY(i.indkey) " .
707 "WHERE i.indrelid = '\"$table\"'::regclass " .
708 "AND i.indisprimary",
709 __METHOD__
710 );
711 $currentColumns = [];
712 foreach ( $result as $row ) {
713 $currentColumns[] = $row->column;
714 }
715
716 if ( $currentColumns == $shouldBe ) {
717 $this->output( "...no need to change primary key of '$table'\n" );
718 return;
719 }
720
721 $this->dropConstraint( $table, '', 'primary', $constraintName );
722
723 $table = $this->db->addIdentifierQuotes( $table );
724 $this->db->query(
725 "ALTER TABLE $table" .
726 " ADD PRIMARY KEY (" . implode( ',', $shouldBe ) . ');',
727 __METHOD__
728 );
729 }
730
742 protected function dropConstraint( $table, $field, $type, $conname = null ) {
743 if ( $conname === null ) {
744 if ( $type == 'primary' ) {
745 $conname = "{$table}_pkey";
746 } else {
747 $map = [ 'unique' => 'key', 'check' => 'check', 'foreignkey' => 'fkey' ];
748 $conname = "{$table}_{$field}_{$map[$type]}";
749 }
750 }
751
752 if ( $this->db->constraintExists( $table, $conname ) ) {
753 $table = $this->db->addIdentifierQuotes( $table );
754 $this->db->query(
755 "ALTER TABLE $table DROP CONSTRAINT $conname;",
756 __METHOD__
757 );
758
759 return true;
760 }
761
762 return false;
763 }
764
771 protected function migrateSearchindex() {
772 $updateKey = 'searchindex-pk-titlelength';
773 if ( !$this->tableExists( 'searchindex' ) ) {
774 return;
775 }
776
777 $primaryIndexExists = $this->db->indexExists( 'searchindex', 'searchindex_pkey', __METHOD__ );
778 if ( $this->updateRowExists( $updateKey ) || ( $primaryIndexExists ) ) {
779 $this->outputApplied( "...searchindex table has already been migrated.\n" );
780 if ( !$this->updateRowExists( $updateKey ) ) {
781 $this->insertUpdateRow( $updateKey );
782 }
783 return;
784 }
785
786 $apply = $this->applyPatch( 'patch-searchindex-pk-titlelength.sql', false, '...migrating searchindex table' );
787
788 if ( $apply ) {
789 $this->insertUpdateRow( $updateKey );
790 }
791 }
792}
Fix redirect pages with missing or incomplete row in the redirect table.
An error in a previous version of MediaWiki caused B type passwords to be written with an :A: prefix ...
Apply database changes after updating MediaWiki.
applyPatch( $path, $isFullPath=false, $msg=null)
Applies a SQL patch.
updateRowExists( $key)
Helper function: check if the given key is present in the updatelog table.
insertUpdateRow( $key, $val=null)
Helper function: Add a key to the updatelog table.
outputApplied(string $str)
Output a note about an update that has already been applied.
Class for handling updates to Postgres databases.
getInitialUpdateKeys()
Get an array of update keys to insert into the updatelog table after a new installation....
addPgExtIndex( $table, $index, $type)
checkIndex( $index, $should_be, $good_def)
dropConstraint( $table, $field, $type, $conname=null)
Drop generic constraint.
addPgIndex( $table, $index, $type, $unique=false)
changeNullableField( $table, $field, $null, $update=false)
changeField( $table, $field, $newtype, $default)
changeFkeyDeferrable( $table, $field, $clause)
changeFieldPurgeTable( $table, $field, $newtype, $default)
renameIndex( $table, $old, $new, $skipBothIndexExistWarning=false, $a=false, $b=false)
changePrimaryKey( $table, $shouldBe, $constraintName=null)
setDefault( $table, $field, $default)
renameTable( $old, $new, $patch=false)
migrateSearchindex()
Replaces unique index with primary key,modifies si_title length.
dropDefault( $table, $field)
Drop a default value from a field.
Maintenance script that merges the revision_comment_temp table into the revision table.
Maintenance script that Fills the user_is_temp column of the user table for users created before MW 1...
Postgres database abstraction layer.