0 AND relname=%s AND nspname=%s END; $res = $this->db->query( sprintf( $q, $this->db->addQuotes( $table ), $this->db->addQuotes( $this->db->getCoreSchema() ) ) ); if ( !$res ) { return null; } $cols = []; foreach ( $res as $r ) { $cols[] = [ "name" => $r[0], "ord" => $r[1], ]; } return $cols; } function describeIndex( $idx ) { // first fetch the key (which is a list of columns ords) and // the table the index applies to (an oid) $q = <<db->query( sprintf( $q, $this->db->addQuotes( $this->db->getCoreSchema() ), $this->db->addQuotes( $idx ) ) ); if ( !$res ) { return null; } $r = $this->db->fetchRow( $res ); if ( !$r ) { return null; } $indkey = $r[0]; $relid = intval( $r[1] ); $indkeys = explode( ' ', $indkey ); $colnames = []; foreach ( $indkeys as $rid ) { $query = <<db->query( sprintf( $query, $rid ) ); if ( !$r2 ) { return null; } $row2 = $this->db->fetchRow( $r2 ); if ( !$row2 ) { return null; } $colnames[] = $row2[0]; } return $colnames; } function fkeyDeltype( $fkey ) { $q = <<db->query( sprintf( $q, $this->db->addQuotes( $this->db->getCoreSchema() ), $this->db->addQuotes( $fkey ) ) ); $row = $this->db->fetchRow( $r ); if ( !$row ) { return null; } return $row[0]; } function ruleDef( $table, $rule ) { $q = <<db->query( sprintf( $q, $this->db->addQuotes( $this->db->getCoreSchema() ), $this->db->addQuotes( $table ), $this->db->addQuotes( $rule ) ) ); $row = $this->db->fetchRow( $r ); if ( !$row ) { return null; } $d = $row[0]; return $d; } 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")' ); } } } protected function renameSequence( $old, $new ) { if ( $this->db->sequenceExists( $new ) ) { $this->output( "...sequence $new already exists.\n" ); return; } if ( $this->db->sequenceExists( $old ) ) { $this->output( "Renaming sequence $old to $new\n" ); $this->db->query( "ALTER SEQUENCE $old RENAME TO $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, $skipBothIndexExistWarning = false, $a = false, $b = false ) { // First requirement: the table must exist if ( !$this->db->tableExists( $table, __METHOD__ ) ) { $this->output( "...skipping: '$table' table doesn't exist yet.\n" ); return; } // Second requirement: the new index must be missing if ( $this->db->indexExists( $table, $new, __METHOD__ ) ) { $this->output( "...index $new already set on $table table.\n" ); if ( !$skipBothIndexExistWarning && $this->db->indexExists( $table, $old, __METHOD__ ) ) { $this->output( "...WARNING: $old still exists, despite it has been " . "renamed into $new (which also exists).\n" . " $old should be manually removed if not needed anymore.\n" ); } return; } // Third requirement: the old index must exist if ( !$this->db->indexExists( $table, $old, __METHOD__ ) ) { $this->output( "...skipping: index $old doesn't exist.\n" ); return; } $this->db->query( "ALTER INDEX $old RENAME TO $new" ); } protected function addPgField( $table, $field, $type ) { $fi = $this->db->fieldInfo( $table, $field ); if ( !is_null( $fi ) ) { $this->output( "...column '$table.$field' already exists\n" ); return; } else { $this->output( "Adding column '$table.$field'\n" ); $this->db->query( "ALTER TABLE $table ADD $field $type" ); } } protected function changeField( $table, $field, $newtype, $default ) { $fi = $this->db->fieldInfo( $table, $field ); if ( is_null( $fi ) ) { $this->output( "...ERROR: expected column $table.$field to exist\n" ); exit( 1 ); } if ( $fi->type() === $newtype ) { $this->output( "...column '$table.$field' is already of type '$newtype'\n" ); } else { $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" ); $sql = "ALTER TABLE $table ALTER $field TYPE $newtype"; if ( strlen( $default ) ) { $res = []; if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) { $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]"; $this->db->query( $sqldef ); $default = preg_replace( '/\s*DEFAULT .+/', '', $default ); } $sql .= " USING $default"; } $this->db->query( $sql ); } } protected function changeFieldPurgeTable( $table, $field, $newtype, $default ) { # # For a cache table, empty it if the field needs to be changed, because the old contents # # may be corrupted. If the column is already the desired type, refrain from purging. $fi = $this->db->fieldInfo( $table, $field ); if ( is_null( $fi ) ) { $this->output( "...ERROR: expected column $table.$field to exist\n" ); exit( 1 ); } if ( $fi->type() === $newtype ) { $this->output( "...column '$table.$field' is already of type '$newtype'\n" ); } else { $this->output( "Purging data from cache table '$table'\n" ); $this->db->query( "DELETE from $table" ); $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" ); $sql = "ALTER TABLE $table ALTER $field TYPE $newtype"; if ( strlen( $default ) ) { $res = []; if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) { $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]"; $this->db->query( $sqldef ); $default = preg_replace( '/\s*DEFAULT .+/', '', $default ); } $sql .= " USING $default"; } $this->db->query( $sql ); } } 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" ); exit( 1 ); } if ( $fi->isNullable() ) { # # It's NULL - does it need to be NOT NULL? if ( 'NOT NULL' === $null ) { $this->output( "Changing '$table.$field' to not allow NULLs\n" ); $this->db->query( "ALTER TABLE $table ALTER $field SET NOT NULL" ); } else { $this->output( "...column '$table.$field' is already set as NULL\n" ); } } else { # # It's NOT NULL - does it need to be NULL? if ( 'NULL' === $null ) { $this->output( "Changing '$table.$field' to allow NULLs\n" ); $this->db->query( "ALTER TABLE $table ALTER $field DROP NOT NULL" ); } else { $this->output( "...column '$table.$field' is already set as NOT NULL\n" ); } } } public function addPgIndex( $table, $index, $type ) { 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' $type\n" ); $this->db->query( "CREATE INDEX $index ON $table $type" ); } } public function addPgExtIndex( $table, $index, $type ) { if ( $this->db->indexExists( $table, $index ) ) { $this->output( "...index '$index' on table '$table' already exists\n" ); } else { 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, "Creating index '$index' on table '$table'" ); } } } protected function dropFkey( $table, $field ) { $fi = $this->db->fieldInfo( $table, $field ); if ( is_null( $fi ) ) { $this->output( "WARNING! Column '$table.$field' does not exist but it should! " . "Please report this.\n" ); return; } $conname = $fi->conname(); if ( $fi->conname() ) { $this->output( "Dropping foreign key constraint on '$table.$field'\n" ); $conclause = "CONSTRAINT \"$conname\""; $command = "ALTER TABLE $table DROP CONSTRAINT $conname"; $this->db->query( $command ); } else { $this->output( "...foreign key constraint on '$table.$field' already does not exist\n" ); }; } protected function changeFkeyDeferrable( $table, $field, $clause ) { $fi = $this->db->fieldInfo( $table, $field ); if ( is_null( $fi ) ) { $this->output( "WARNING! Column '$table.$field' does not exist but it should! " . "Please report this.\n" ); return; } if ( $fi->is_deferred() && $fi->is_deferrable() ) { return; } $this->output( "Altering column '$table.$field' to be DEFERRABLE INITIALLY DEFERRED\n" ); $conname = $fi->conname(); if ( $fi->conname() ) { $conclause = "CONSTRAINT \"$conname\""; $command = "ALTER TABLE $table DROP CONSTRAINT $conname"; $this->db->query( $command ); } else { $this->output( "Column '$table.$field' does not have a foreign key " . "constraint, will be added\n" ); $conclause = ""; } $command = "ALTER TABLE $table ADD $conclause " . "FOREIGN KEY ($field) REFERENCES $clause DEFERRABLE INITIALLY DEFERRED"; $this->db->query( $command ); } protected function convertArchive2() { if ( $this->db->tableExists( "archive2" ) ) { if ( $this->db->ruleExists( 'archive', 'archive_insert' ) ) { $this->output( "Dropping rule 'archive_insert'\n" ); $this->db->query( 'DROP RULE archive_insert ON archive' ); } if ( $this->db->ruleExists( 'archive', 'archive_delete' ) ) { $this->output( "Dropping rule 'archive_delete'\n" ); $this->db->query( 'DROP RULE archive_delete ON archive' ); } $this->applyPatch( 'patch-remove-archive2.sql', false, "Converting 'archive2' back to normal archive table" ); } else { $this->output( "...obsolete table 'archive2' does not exist\n" ); } } protected function checkOiDeleted() { if ( $this->db->fieldInfo( 'oldimage', 'oi_deleted' )->type() !== 'smallint' ) { $this->output( "Changing 'oldimage.oi_deleted' to type 'smallint'\n" ); $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted DROP DEFAULT" ); $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted TYPE SMALLINT USING (oi_deleted::smallint)" ); $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted SET DEFAULT 0" ); } else { $this->output( "...column 'oldimage.oi_deleted' is already of type 'smallint'\n" ); } } protected function checkOiNameConstraint() { if ( $this->db->hasConstraint( "oldimage_oi_name_fkey_cascaded" ) ) { $this->output( "...table 'oldimage' has correct cascading delete/update " . "foreign key to image\n" ); } else { if ( $this->db->hasConstraint( "oldimage_oi_name_fkey" ) ) { $this->db->query( "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey" ); } if ( $this->db->hasConstraint( "oldimage_oi_name_fkey_cascade" ) ) { $this->db->query( "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey_cascade" ); } $this->output( "Making foreign key on table 'oldimage' (to image) a cascade delete/update\n" ); $this->db->query( "ALTER TABLE oldimage ADD CONSTRAINT oldimage_oi_name_fkey_cascaded " . "FOREIGN KEY (oi_name) REFERENCES image(img_name) " . "ON DELETE CASCADE ON UPDATE CASCADE" ); } } protected function checkPageDeletedTrigger() { if ( !$this->db->triggerExists( 'page', 'page_deleted' ) ) { $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 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 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 = []; } else { $this->output( "...no need to drop index '$index'\n" ); } if ( empty( $pu ) ) { $this->output( "Creating index '$index'\n" ); $this->db->query( $good_def ); } else { $this->output( "...index '$index' exists\n" ); } } protected function checkRevUserFkey() { if ( $this->fkeyDeltype( 'revision_rev_user_fkey' ) == 'r' ) { $this->output( "...constraint 'revision_rev_user_fkey' is ON DELETE RESTRICT\n" ); } else { $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->applyPatch( 'patch-rename-iwl_prefix.sql', false, "Replacing index 'iwl_prefix' with 'iwl_prefix_title_from'" ); } } 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->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->applyPatch( 'patch-tsearch2funcs.sql', false, "Rewriting tsearch2 triggers" ); } } protected function rebuildTextSearch() { if ( $this->updateRowExists( 'patch-textsearch_bug66650.sql' ) ) { $this->output( "...T68650 already fixed or not applicable.\n" ); return; }; $this->applyPatch( 'patch-textsearch_bug66650.sql', false, 'Rebuilding text search for T68650' ); } }