MysqlUpdater: Add utility functions to alter default constraint

There's occassionaly a need to alter default constraints, currently
a separate patch file is required to do so everytime which is quite
onerous and can be simplified.

Borrow a pair of set/drop functions from PostgresUpdater to make the
process more efficient.

Also convert usages of patch files to use the methods.

Change-Id: I846b94d0a721295f5bcdfe21f658116704e2d350
This commit is contained in:
Ammar Abdulhamid 2020-11-02 09:58:42 +01:00 committed by Ammarpad
parent a5dc8fa7ab
commit 5db104f6a1
4 changed files with 42 additions and 18 deletions

View file

@ -343,7 +343,7 @@ class MysqlUpdater extends DatabaseUpdater {
[ 'addField', 'oldimage', 'oi_description_id', 'patch-oldimage-oi_description_id.sql' ],
[ 'addField', 'protected_titles', 'pt_reason_id', 'patch-protected_titles-pt_reason_id.sql' ],
[ 'addField', 'recentchanges', 'rc_comment_id', 'patch-recentchanges-rc_comment_id.sql' ],
[ 'modifyField', 'revision', 'rev_comment', 'patch-revision-rev_comment-default.sql' ],
[ 'setDefault', 'revision', 'rev_comment', '' ],
// This field was added in 1.31, but is put here so it can be used by 'migrateComments'
[ 'addField', 'image', 'img_description_id', 'patch-image-img_description_id.sql' ],
@ -370,7 +370,11 @@ class MysqlUpdater extends DatabaseUpdater {
[ 'addField', 'recentchanges', 'rc_actor', 'patch-recentchanges-rc_actor.sql' ],
[ 'addField', 'logging', 'log_actor', 'patch-logging-log_actor.sql' ],
[ 'migrateActors' ],
[ 'modifyField', 'revision', 'rev_text_id', 'patch-rev_text_id-default.sql' ],
// Adds a default value to the rev_text_id field to allow Multi Content
// Revisions migration to happen where rows will have to be added to the
// revision table with no rev_text_id.
[ 'setDefault', 'revision', 'rev_text_id', 0 ],
[ 'modifyTable', 'site_stats', 'patch-site_stats-modify.sql' ],
[ 'populateArchiveRevId' ],
[ 'addIndex', 'recentchanges', 'rc_namespace_title_timestamp',
@ -379,8 +383,7 @@ class MysqlUpdater extends DatabaseUpdater {
// 1.32
[ 'addTable', 'change_tag_def', 'patch-change_tag_def.sql' ],
[ 'populateExternallinksIndex60' ],
[ 'modifyfield', 'externallinks', 'el_index_60',
'patch-externallinks-el_index_60-drop-default.sql' ],
[ 'dropDefault', 'externallinks', 'el_index_60' ],
[ 'runMaintenance', DeduplicateArchiveRevId::class, 'maintenance/deduplicateArchiveRevId.php' ],
[ 'addField', 'change_tag', 'ct_tag_id', 'patch-change_tag-tag_id.sql' ],
[ 'addIndex', 'archive', 'ar_revid_uniq', 'patch-archive-ar_rev_id-unique.sql' ],
@ -1369,4 +1372,39 @@ class MysqlUpdater extends DatabaseUpdater {
return $vars;
}
/**
* Drop a default value from a field
*
* @since 1.36
* @param string $table
* @param string $field
*/
protected function dropDefault( $table, $field ) {
$info = $this->db->fieldInfo( $table, $field );
if ( $info && $info->defaultValue() !== false ) {
$this->output( "Removing '$table.$field' default value\n" );
$this->db->query( "ALTER TABLE $table ALTER COLUMN $field DROP DEFAULT", __METHOD__ );
}
}
/**
* Set a default value for a field
*
* @since 1.36
* @param string $table
* @param string $field
* @param mixed $default
*/
protected function setDefault( $table, $field, $default ) {
$info = $this->db->fieldInfo( $table, $field );
if ( $info && $info->defaultValue() !== $default ) {
$this->output( "Changing '$table.$field' default value\n" );
$this->db->query(
"ALTER TABLE $table ALTER COLUMN $field SET DEFAULT "
. $this->db->addQuotes( $default ), __METHOD__
);
}
}
}

View file

@ -1,2 +0,0 @@
-- @since 1.32
ALTER TABLE /*$wgDBprefix*/externallinks ALTER COLUMN el_index_60 DROP DEFAULT;

View file

@ -1,10 +0,0 @@
--
-- Adds a default value to the rev_text_id field in the revision table.
-- This is to allow the Multi Content Revisions migration to happen where
-- rows will have to be added to the revision table with no rev_text_id.
--
-- 2018-03-12
--
ALTER TABLE /*$wgDBprefix*/revision
ALTER COLUMN rev_text_id SET DEFAULT 0;

View file

@ -1,2 +0,0 @@
ALTER TABLE /*_*/revision
ALTER COLUMN rev_comment SET DEFAULT '';