Clean up applyPatch() usage in the installer.
[lhc/web/wiklou.git] / includes / installer / PostgresUpdater.php
index f5f8c41..499a2d6 100644 (file)
@@ -2,6 +2,21 @@
 /**
  * PostgreSQL-specific updater.
  *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup Deployment
  */
@@ -29,23 +44,29 @@ class PostgresUpdater extends DatabaseUpdater {
                return array(
                        # rename tables 1.7.3
                        # r15791 Change reserved word table names "user" and "text"
-                       array( 'renameTable', 'user', 'mwuser'),
-                       array( 'renameTable', 'text', 'pagecontent'),
-
-                       # new sequences
-                       array( 'addSequence', 'logging_log_id_seq'          ),
-                       array( 'addSequence', 'page_restrictions_pr_id_seq' ),
+                       array( 'renameTable', 'user', 'mwuser' ),
+                       array( 'renameTable', 'text', 'pagecontent' ),
+                       array( 'renameIndex', 'mwuser', 'user_pkey', 'mwuser_pkey'),
+                       array( 'renameIndex', 'mwuser', 'user_user_name_key', 'mwuser_user_name_key' ),
+                       array( 'renameIndex', 'pagecontent','text_pkey', 'pagecontent_pkey' ),
 
                        # renamed sequences
                        array( 'renameSequence', 'ipblocks_ipb_id_val', 'ipblocks_ipb_id_seq'         ),
                        array( 'renameSequence', 'rev_rev_id_val',      'revision_rev_id_seq'         ),
                        array( 'renameSequence', 'text_old_id_val',     'text_old_id_seq'             ),
-                       array( 'renameSequence', 'category_id_seq',     'category_cat_id_seq'         ),
                        array( 'renameSequence', 'rc_rc_id_seq',        'recentchanges_rc_id_seq'     ),
                        array( 'renameSequence', 'log_log_id_seq',      'logging_log_id_seq'          ),
                        array( 'renameSequence', 'pr_id_val',           'page_restrictions_pr_id_seq' ),
                        array( 'renameSequence', 'us_id_seq',           'uploadstash_us_id_seq' ),
 
+                       # since r58263
+                       array( 'renameSequence', 'category_id_seq', 'category_cat_id_seq'),
+
+                       # new sequences if not renamed above
+                       array( 'addSequence', 'logging', false, 'logging_log_id_seq' ),
+                       array( 'addSequence', 'page_restrictions', false, 'page_restrictions_pr_id_seq' ),
+                       array( 'addSequence', 'filearchive', 'fa_id', 'filearchive_fa_id_seq' ),
+
                        # new tables
                        array( 'addTable', 'category',          'patch-category.sql' ),
                        array( 'addTable', 'page',              'patch-page.sql' ),
@@ -69,11 +90,13 @@ class PostgresUpdater extends DatabaseUpdater {
                        array( 'addTable', 'uploadstash',       'patch-uploadstash.sql' ),
                        array( 'addTable', 'user_former_groups','patch-user_former_groups.sql' ),
                        array( 'addTable', 'config',            'patch-config.sql' ),
+                       array( 'addTable', 'external_user',     'patch-external_user.sql' ),
 
                        # Needed before new field
                        array( 'convertArchive2' ),
 
                        # new fields
+                       array( 'addPgField', 'updatelog',     'ul_value',             'TEXT' ),
                        array( 'addPgField', 'archive',       'ar_deleted',           'SMALLINT NOT NULL DEFAULT 0' ),
                        array( 'addPgField', 'archive',       'ar_len',               'INTEGER' ),
                        array( 'addPgField', 'archive',       'ar_page_id',           'INTEGER' ),
@@ -89,6 +112,7 @@ class PostgresUpdater extends DatabaseUpdater {
                        array( 'addPgField', 'ipblocks',      'ipb_create_account',   'SMALLINT NOT NULL DEFAULT 1' ),
                        array( 'addPgField', 'ipblocks',      'ipb_deleted',          'SMALLINT NOT NULL DEFAULT 0' ),
                        array( 'addPgField', 'ipblocks',      'ipb_enable_autoblock', 'SMALLINT NOT NULL DEFAULT 1' ),
+                       array( 'addPgField', 'ipblocks',      'ipb_parent_block_id',            'INTEGER DEFAULT NULL REFERENCES ipblocks(ipb_id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED' ),
                        array( 'addPgField', 'filearchive',   'fa_deleted',           'SMALLINT NOT NULL DEFAULT 0' ),
                        array( 'addPgField', 'logging',       'log_deleted',          'SMALLINT NOT NULL DEFAULT 0' ),
                        array( 'addPgField', 'logging',       'log_id',               "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('logging_log_id_seq')" ),
@@ -140,7 +164,7 @@ class PostgresUpdater extends DatabaseUpdater {
                        array( 'changeField', 'image',         'img_size',        'integer',  '' ),
                        array( 'changeField', 'image',         'img_width',       'integer',  '' ),
                        array( 'changeField', 'image',         'img_height',      'integer',  '' ),
-                       array( 'changeField', 'interwiki',     'iw_local',        'smallint', 'iw_local::smallint DEFAULT 0' ),
+                       array( 'changeField', 'interwiki',     'iw_local',        'smallint', 'iw_local::smallint' ),
                        array( 'changeField', 'interwiki',     'iw_trans',        'smallint', 'iw_trans::smallint DEFAULT 0' ),
                        array( 'changeField', 'ipblocks',      'ipb_auto',        'smallint', 'ipb_auto::smallint DEFAULT 0' ),
                        array( 'changeField', 'ipblocks',      'ipb_anon_only',   'smallint', "CASE WHEN ipb_anon_only=' ' THEN 0 ELSE ipb_anon_only::smallint END DEFAULT 0" ),
@@ -177,12 +201,16 @@ class PostgresUpdater extends DatabaseUpdater {
                        array( 'changeNullableField', 'oldimage', 'oi_timestamp',  'NULL' ),
                        array( 'changeNullableField', 'oldimage', 'oi_major_mime', 'NULL' ),
                        array( 'changeNullableField', 'oldimage', 'oi_minor_mime', 'NULL' ),
+                       array( 'changeNullableField', 'image', 'img_metadata', 'NOT NULL'),
+                       array( 'changeNullableField', 'filearchive', 'fa_metadata', 'NOT NULL'),
+                       array( 'changeNullableField', 'recentchanges', 'rc_cur_id', 'NULL' ),
 
                        array( 'checkOiDeleted' ),
 
                        # New indexes
                        array( 'addPgIndex', 'archive',       'archive_user_text',      '(ar_user_text)' ),
                        array( 'addPgIndex', 'image',         'img_sha1',               '(img_sha1)' ),
+                       array( 'addPgIndex', 'ipblocks',      'ipb_parent_block_id',              '(ipb_parent_block_id)' ),
                        array( 'addPgIndex', 'oldimage',      'oi_sha1',                '(oi_sha1)' ),
                        array( 'addPgIndex', 'page',          'page_mediawiki_title',   '(page_title) WHERE page_namespace = 8' ),
                        array( 'addPgIndex', 'pagelinks',     'pagelinks_title',        '(pl_title)' ),
@@ -195,12 +223,78 @@ class PostgresUpdater extends DatabaseUpdater {
                        array( 'addPgIndex', 'iwlinks',       'iwl_prefix_title_from',  '(iwl_prefix, iwl_title, iwl_from)' ),
                        array( 'addPgIndex', 'job',           'job_timestamp_idx',      '(job_timestamp)' ),
 
+                       array( 'checkIndex', 'pagelink_unique', array(
+                               array('pl_from', 'int4_ops', 'btree', 0),
+                               array('pl_namespace', 'int2_ops', 'btree', 0),
+                               array('pl_title', 'text_ops', 'btree', 0),
+                       ),
+                       'CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title)' ),
+                       array( 'checkIndex', 'cl_sortkey', array(
+                               array('cl_to', 'text_ops', 'btree', 0),
+                               array('cl_sortkey', 'text_ops', 'btree', 0),
+                               array('cl_from', 'int4_ops', 'btree', 0),
+                       ),
+                       'CREATE INDEX cl_sortkey ON "categorylinks" USING "btree" ("cl_to", "cl_sortkey", "cl_from")' ),
+                       array( 'checkIndex', 'logging_times', array(
+                               array('log_timestamp', 'timestamptz_ops', 'btree', 0),
+                       ),
+                       'CREATE INDEX "logging_times" ON "logging" USING "btree" ("log_timestamp")' ),
+                       array( 'dropIndex', 'oldimage', 'oi_name' ),
+                       array( 'checkIndex', 'oi_name_archive_name', array(
+                               array('oi_name', 'text_ops', 'btree', 0),
+                               array('oi_archive_name', 'text_ops', 'btree', 0),
+                       ),
+                       'CREATE INDEX "oi_name_archive_name" ON "oldimage" USING "btree" ("oi_name", "oi_archive_name")' ),
+                       array( 'checkIndex', 'oi_name_timestamp', array(
+                               array('oi_name', 'text_ops', 'btree', 0),
+                               array('oi_timestamp', 'timestamptz_ops', 'btree', 0),
+                       ),
+                       'CREATE INDEX "oi_name_timestamp" ON "oldimage" USING "btree" ("oi_name", "oi_timestamp")' ),
+                       array( 'checkIndex', 'page_main_title', array(
+                               array('page_title', 'text_pattern_ops', 'btree', 0),
+                       ),
+                       'CREATE INDEX "page_main_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 0)' ),
+                       array( 'checkIndex', 'page_mediawiki_title', array(
+                               array('page_title', 'text_pattern_ops', 'btree', 0),
+                       ),
+                       'CREATE INDEX "page_mediawiki_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 8)' ),
+                       array( 'checkIndex', 'page_project_title', array(
+                               array('page_title', 'text_pattern_ops', 'btree', 0),
+                       ),
+                       'CREATE INDEX "page_project_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 4)' ),
+                       array( 'checkIndex', 'page_talk_title', array(
+                               array('page_title', 'text_pattern_ops', 'btree', 0),
+                       ),
+                       'CREATE INDEX "page_talk_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 1)' ),
+                       array( 'checkIndex', 'page_user_title', array(
+                               array('page_title', 'text_pattern_ops', 'btree', 0),
+                       ),
+                       'CREATE INDEX "page_user_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 2)' ),
+                       array( 'checkIndex', 'page_utalk_title', array(
+                               array('page_title', 'text_pattern_ops', 'btree', 0),
+                       ),
+                       'CREATE INDEX "page_utalk_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 3)' ),
+                       array( 'checkIndex', 'ts2_page_text', array(
+                               array('textvector', 'tsvector_ops', 'gist', 0),
+                       ),
+                       'CREATE INDEX "ts2_page_text" ON "pagecontent" USING "gist" ("textvector")' ),
+                       array( 'checkIndex', 'ts2_page_title', array(
+                               array('titlevector', 'tsvector_ops', 'gist', 0),
+                       ),
+                       'CREATE INDEX "ts2_page_title" ON "page" USING "gist" ("titlevector")' ),
+
                        array( 'checkOiNameConstraint' ),
                        array( 'checkPageDeletedTrigger' ),
-                       array( 'checkRcCurIdNullable' ),
-                       array( 'checkPagelinkUniqueIndex' ),
                        array( 'checkRevUserFkey' ),
-                       array( 'checkIpbAdress' ),
+                       array( 'dropIndex', 'ipblocks', 'ipb_address'),
+                       array( 'checkIndex', 'ipb_address_unique', array(
+                               array('ipb_address', 'text_ops', 'btree', 0),
+                               array('ipb_user',    'int4_ops', 'btree', 0),
+                               array('ipb_auto',    'int2_ops', 'btree', 0),
+                               array('ipb_anon_only', 'int2_ops', 'btree', 0),
+                       ),
+                       'CREATE UNIQUE INDEX ipb_address_unique ON ipblocks (ipb_address,ipb_user,ipb_auto,ipb_anon_only)' ),
+
                        array( 'checkIwlPrefix' ),
 
                        # All FK columns should be deferred
@@ -213,6 +307,7 @@ class PostgresUpdater extends DatabaseUpdater {
                        array( 'changeFkeyDeferrable', 'imagelinks',       'il_from',         'page(page_id) ON DELETE CASCADE' ),
                        array( 'changeFkeyDeferrable', 'ipblocks',         'ipb_by',          'mwuser(user_id) ON DELETE CASCADE' ),
                        array( 'changeFkeyDeferrable', 'ipblocks',         'ipb_user',        'mwuser(user_id) ON DELETE SET NULL' ),
+                       array( 'changeFkeyDeferrable', 'ipblocks',         'ipb_parent_block_id',       'ipblocks(ipb_id) ON DELETE SET NULL' ),
                        array( 'changeFkeyDeferrable', 'langlinks',        'll_from',         'page(page_id) ON DELETE CASCADE' ),
                        array( 'changeFkeyDeferrable', 'logging',          'log_user',        'mwuser(user_id) ON DELETE SET NULL' ),
                        array( 'changeFkeyDeferrable', 'oldimage',         'oi_name',         'image(img_name) ON DELETE CASCADE ON UPDATE CASCADE' ),
@@ -232,6 +327,8 @@ class PostgresUpdater extends DatabaseUpdater {
                        array( 'changeFkeyDeferrable', 'user_properties',  'up_user',         'mwuser(user_id) ON DELETE CASCADE' ),
                        array( 'changeFkeyDeferrable', 'watchlist',        'wl_user',         'mwuser(user_id) ON DELETE CASCADE' ),
 
+                       # r81574
+                       array( 'addInterwikiType' ),
                        # end
                        array( 'tsearchFixes' ),
                );
@@ -392,10 +489,13 @@ END;
                return $d;
        }
 
-       protected function addSequence( $ns ) {
+       protected function addSequence( $table, $pkey, $ns ) {
                if ( !$this->db->sequenceExists( $ns ) ) {
                        $this->output( "Creating sequence $ns\n" );
                        $this->db->query( "CREATE SEQUENCE $ns" );
+                       if( $pkey !== false ) {
+                               $this->setDefault( $table,  $pkey, '"nextval"(\'"' . $ns . '"\'::"regclass")' );
+                       }
                }
        }
 
@@ -410,12 +510,22 @@ END;
                }
        }
 
-       protected function renameTable( $old, $new ) {
+       protected function renameTable( $old, $new, $patch = false ) {
                if ( $this->db->tableExists( $old ) ) {
                        $this->output( "Renaming table $old to $new\n" );
                        $old = $this->db->realTableName( $old, "quoted" );
                        $new = $this->db->realTableName( $new, "quoted" );
                        $this->db->query( "ALTER TABLE $old RENAME TO $new" );
+                       if( $patch !== false ) {
+                               $this->applyPatch( $patch );
+                       }
+               }
+       }
+
+       protected function renameIndex( $table, $old, $new ) {
+               if ( $this->db->indexExists( $table, $old ) ) {
+                       $this->output( "Renaming index $old to $new\n" );
+                       $this->db->query( "ALTER INDEX $old RENAME TO $new" );
                }
        }
 
@@ -451,13 +561,20 @@ END;
                                }
                                $sql .= " USING $default";
                        }
-                       $this->db->begin( __METHOD__ );
                        $this->db->query( $sql );
-                       $this->db->commit( __METHOD__ );
                }
        }
 
-       protected function changeNullableField( $table, $field, $null ) {
+       protected function setDefault( $table, $field, $default ) {
+
+               $info = $this->db->fieldInfo( $table, $field );
+               if ( $info->defaultValue() !== $default ) {
+                       $this->output( "Changing '$table.$field' default value\n" );
+                       $this->db->query( "ALTER TABLE $table ALTER $field SET DEFAULT " . $default );
+               }
+       }
+
+       protected function changeNullableField( $table, $field, $null) {
                $fi = $this->db->fieldInfo( $table, $field );
                if ( is_null( $fi ) ) {
                        $this->output( "...ERROR: expected column $table.$field to exist\n" );
@@ -496,11 +613,11 @@ END;
                if ( $this->db->indexExists( $table, $index ) ) {
                        $this->output( "...index '$index' on table '$table' already exists\n" );
                } else {
-                       $this->output( "Creating index '$index' on table '$table'\n" );
                        if ( preg_match( '/^\(/', $type ) ) {
+                               $this->output( "Creating index '$index' on table '$table'\n" );
                                $this->db->query( "CREATE INDEX $index ON $table $type" );
                        } else {
-                               $this->applyPatch( $type, true );
+                               $this->applyPatch( $type, true, "Creating index '$index' on table '$table'" );
                        }
                }
        }
@@ -530,7 +647,6 @@ END;
 
        protected function convertArchive2() {
                if ( $this->db->tableExists( "archive2" ) ) {
-                       $this->output( "Converting 'archive2' back to normal archive table\n" );
                        if ( $this->db->ruleExists( 'archive', 'archive_insert' ) ) {
                                $this->output( "Dropping rule 'archive_insert'\n" );
                                $this->db->query( 'DROP RULE archive_insert ON archive' );
@@ -539,7 +655,7 @@ END;
                                $this->output( "Dropping rule 'archive_delete'\n" );
                                $this->db->query( 'DROP RULE archive_delete ON archive' );
                        }
-                       $this->applyPatch( 'patch-remove-archive2.sql' );
+                       $this->applyPatch( 'patch-remove-archive2.sql', false, "Converting 'archive2' back to normal archive table" );
                } else {
                        $this->output( "...obsolete table 'archive2' does not exist\n" );
                }
@@ -574,38 +690,34 @@ END;
 
        protected function checkPageDeletedTrigger() {
                if ( !$this->db->triggerExists( 'page', 'page_deleted' ) ) {
-                       $this->output( "Adding function and trigger 'page_deleted' to table 'page'\n" );
-                       $this->applyPatch( 'patch-page_deleted.sql' );
+                       $this->applyPatch( 'patch-page_deleted.sql', false, "Adding function and trigger 'page_deleted' to table 'page'" );
                } else {
                        $this->output( "...table 'page' has 'page_deleted' trigger\n" );
                }
        }
 
-       protected function checkRcCurIdNullable(){
-               $fi = $this->db->fieldInfo( 'recentchanges', 'rc_cur_id' );
-               if ( !$fi->isNullable() ) {
-                       $this->output( "Removing NOT NULL constraint from 'recentchanges.rc_cur_id'\n" );
-                       $this->applyPatch( 'patch-rc_cur_id-not-null.sql' );
-               } else {
-                       $this->output( "...column 'recentchanges.rc_cur_id' has a NOT NULL constraint\n" );
+       protected function dropIndex( $table, $index, $patch = '', $fullpath = false ) {
+               if ( $this->db->indexExists( $table, $index ) ) {
+                       $this->output( "Dropping obsolete index '$index'\n" );
+                       $this->db->query( "DROP INDEX \"". $index ."\"" );
                }
        }
 
-       protected function checkPagelinkUniqueIndex() {
-               $pu = $this->describeIndex( 'pagelink_unique' );
-               if ( !is_null( $pu ) && ( $pu[0] != 'pl_from' || $pu[1] != 'pl_namespace' || $pu[2] != 'pl_title' ) ) {
-                       $this->output( "Dropping obsolete version of index 'pagelink_unique index'\n" );
-                       $this->db->query( 'DROP INDEX pagelink_unique' );
-                       $pu = null;
+       protected function checkIndex( $index, $should_be, $good_def ) {
+               $pu = $this->db->indexAttributes( $index );
+               if ( !empty( $pu ) && $pu != $should_be ) {
+                       $this->output( "Dropping obsolete version of index '$index'\n" );
+                       $this->db->query( "DROP INDEX \"". $index ."\"" );
+                       $pu = array();
                } else {
-                       $this->output( "...obsolete version of index 'pagelink_unique index' does not exist\n" );
+                       $this->output( "...no need to drop index '$index'\n" );
                }
 
-               if ( is_null( $pu ) ) {
-                       $this->output( "Creating index 'pagelink_unique index'\n" );
-                       $this->db->query( 'CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title)' );
+               if ( empty( $pu ) ) {
+                       $this->output( "Creating index '$index'\n" );
+                       $this->db->query( $good_def );
                } else {
-                       $this->output( "...index 'pagelink_unique_index' already exists\n" );
+                       $this->output( "...index '$index' exists\n" );
                }
        }
 
@@ -613,43 +725,30 @@ END;
                if ( $this->fkeyDeltype( 'revision_rev_user_fkey' ) == 'r' ) {
                        $this->output( "...constraint 'revision_rev_user_fkey' is ON DELETE RESTRICT\n" );
                } else {
-                       $this->output( "Changing constraint 'revision_rev_user_fkey' to ON DELETE RESTRICT\n" );
-                       $this->applyPatch( 'patch-revision_rev_user_fkey.sql' );
-               }
-       }
-
-       protected function checkIpbAdress() {
-               if ( $this->db->indexExists( 'ipblocks', 'ipb_address' ) ) {
-                       $this->output( "Removing deprecated index 'ipb_address'...\n" );
-                       $this->db->query( 'DROP INDEX ipb_address' );
-               }
-               if ( $this->db->indexExists( 'ipblocks', 'ipb_address_unique' ) ) {
-                       $this->output( "...have ipb_address_unique\n" );
-               } else {
-                       $this->output( "Adding ipb_address_unique index\n" );
-                       $this->applyPatch( 'patch-ipb_address_unique.sql' );
+                       $this->applyPatch( 'patch-revision_rev_user_fkey.sql', false, "Changing constraint 'revision_rev_user_fkey' to ON DELETE RESTRICT" );
                }
        }
 
        protected function checkIwlPrefix() {
                if ( $this->db->indexExists( 'iwlinks', 'iwl_prefix' ) ) {
-                       $this->output( "Replacing index 'iwl_prefix' with 'iwl_prefix_from_title'...\n" );
-                       $this->applyPatch( 'patch-rename-iwl_prefix.sql' );
+                       $this->applyPatch( 'patch-rename-iwl_prefix.sql', false, "Replacing index 'iwl_prefix' with 'iwl_prefix_from_title'" );
                }
        }
 
+       protected function addInterwikiType() {
+               $this->applyPatch( 'patch-add_interwiki.sql', false, "Refreshing add_interwiki()" );
+       }
+
        protected function tsearchFixes() {
                # Tweak the page_title tsearch2 trigger to filter out slashes
                # This is create or replace, so harmless to call if not needed
-               $this->output( "Refreshing ts2_page_title()...\n" );
-               $this->applyPatch( 'patch-ts2pagetitle.sql' );
+               $this->applyPatch( 'patch-ts2pagetitle.sql', false, "Refreshing ts2_page_title()" );
 
                # If the server is 8.3 or higher, rewrite the tsearch2 triggers
                # in case they have the old 'default' versions
                # Gather version numbers in case we need them
                if ( $this->db->getServerVersion() >= 8.3 ) {
-                       $this->output( "Rewriting tsearch2 triggers...\n" );
-                       $this->applyPatch( 'patch-tsearch2funcs.sql' );
+                       $this->applyPatch( 'patch-tsearch2funcs.sql', false, "Rewriting tsearch2 triggers" );
                }
        }
 }