From 2f60cd20b6f3aea8776b20658c8810f04035e8f6 Mon Sep 17 00:00:00 2001 From: Dereckson Date: Tue, 18 Dec 2012 02:03:44 +0100 Subject: [PATCH] New methods to drop or rename extension index in DatabaseUpdater - DatabaseUpdater::dropExtensionIndex( $tableName, $indexName, $sqlPath ) - DatabaseUpdater::renameExtensionIndex( $tableName, $oldIndexName, $newIndexName, $sqlPath, $skipBothIndexExistWarning = false ) [ Case use ] This feature is required to help extensions schema change to ensure compatibility with SQLite. First extensions to use it will be Echo and Flagged Revisions. Change-Id: Ia2782d644593ab6b64b67720ed61b3994db10346 --- RELEASE-NOTES-1.21 | 2 + includes/installer/DatabaseUpdater.php | 71 +++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/RELEASE-NOTES-1.21 b/RELEASE-NOTES-1.21 index 896dfe44fe..27692e873d 100644 --- a/RELEASE-NOTES-1.21 +++ b/RELEASE-NOTES-1.21 @@ -66,6 +66,8 @@ production. * (bug 24620) Add types to LogFormatter. * jQuery JSON upgraded from 2.3 to 2.4.0. * Added GetDoubleUnderscoreIDs hook, for modifying the list of magic words. +* DatabaseUpdater class has two new methods to ease extensions schema changes: + dropExtensionIndex and renameExtensionIndex. === Bug fixes in 1.21 === * (bug 40353) SpecialDoubleRedirect should support interwiki redirects. diff --git a/includes/installer/DatabaseUpdater.php b/includes/installer/DatabaseUpdater.php index 2fe96765f9..cd5f90b952 100644 --- a/includes/installer/DatabaseUpdater.php +++ b/includes/installer/DatabaseUpdater.php @@ -256,6 +256,19 @@ abstract class DatabaseUpdater { $this->extensionUpdates[] = array( 'dropField', $tableName, $columnName, $sqlPath, true ); } + /** + * Drop an index from an extension table + * + * @since 1.21 + * + * @param $tableName string The table name + * @param $indexName string The index name + * @param $sqlPath string The path to the SQL change path + */ + public function dropExtensionIndex( $tableName, $indexName, $sqlPath ) { + $this->extensionUpdates[] = array( 'dropIndex', $tableName, $indexName, $sqlPath, true ); + } + /** * * @since 1.20 @@ -267,6 +280,21 @@ abstract class DatabaseUpdater { $this->extensionUpdates[] = array( 'dropTable', $tableName, $sqlPath, true ); } + /** + * Rename an index on an extension table + * + * @since 1.21 + * + * @param $tableName string The table name + * @param $oldIndexName string The old index name + * @param $newIndexName string The new index name + * @param $skipBothIndexExistWarning Boolean: Whether to warn if both the old and the new indexes exist. [facultative; by default, false] + * @param $sqlPath string The path to the SQL change path + */ + public function renameExtensionIndex( $tableName, $oldIndexName, $newIndexName, $sqlPath, $skipBothIndexExistWarning = false ) { + $this->extensionUpdates[] = array( 'renameIndex', $tableName, $oldIndexName, $newIndexName, $skipBothIndexExistWarning, $sqlPath, true ); + } + /** * * @since 1.20 @@ -569,6 +597,7 @@ abstract class DatabaseUpdater { /** * Applies a SQL patch + * * @param $path String Path to the patch file * @param $isFullPath Boolean Whether to treat $path as a relative or not * @param $msg String Description of the patch @@ -599,6 +628,7 @@ abstract class DatabaseUpdater { /** * Add a new table to the database + * * @param $name String Name of the new table * @param $patch String Path to the patch file * @param $fullpath Boolean Whether to treat $patch path as a relative or not @@ -619,6 +649,7 @@ abstract class DatabaseUpdater { /** * Add a new field to an existing table + * * @param $table String Name of the table to modify * @param $field String Name of the new field * @param $patch String Path to the patch file @@ -642,6 +673,7 @@ abstract class DatabaseUpdater { /** * Add a new index to an existing table + * * @param $table String Name of the table to modify * @param $index String Name of the new index * @param $patch String Path to the patch file @@ -690,7 +722,7 @@ abstract class DatabaseUpdater { * Drop an index from an existing table * * @param $table String: Name of the table to modify - * @param $index String: Name of the old index + * @param $index String: Name of the index * @param $patch String: Path to the patch file * @param $fullpath Boolean: Whether to treat $patch path as a relative or not * @return Boolean false if this was skipped because schema changes are skipped @@ -708,6 +740,43 @@ abstract class DatabaseUpdater { return true; } + /** + * Rename an index from an existing table + * + * @param $table String: Name of the table to modify + * @param $oldIndex String: Old name of the index + * @param $newIndex String: New name of the index + * @param $skipBothIndexExistWarning Boolean: Whether to warn if both the old and the new indexes exist. + * @param $patch String: Path to the patch file + * @param $fullpath Boolean: Whether to treat $patch path as a relative or not + * @return Boolean false if this was skipped because schema changes are skipped + */ + protected function renameIndex( $table, $oldIndex, $newIndex, $skipBothIndexExistWarning, $patch, $fullpath = false ) { + // First requirement: the table must exist + if ( !$this->db->tableExists( $table, __METHOD__ ) ) { + $this->output( "...skipping: '$table' table doesn't exist yet.\n" ); + return false; + } + + // Second requirement: the new index must be missing + if ( $this->db->indexExists( $table, $newIndex, __METHOD__ ) ) { + $this->output( "...index $newIndex already set on $table table.\n" ); + if ( !$skipBothIndexExistWarning && $this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) { + $this->output( "...WARNING: $oldIndex still exists, despite it has been renamed into $newIndex (which also exists).\n $oldIndex should be manually removed if not needed anymore.\n" ); + } + return true; + } + + // Third requirement: the old index must exist + if ( !$this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) { + $this->output( "...skipping: index $oldIndex doesn't exist.\n" ); + return false; + } + + // Requirements have been satisfied, patch can be applied + return $this->applyPatch( $patch, $fullpath, "Renaming index $oldIndex into $newIndex to table $table" ); + } + /** * If the specified table exists, drop it, or execute the * patch if one is provided. -- 2.20.1