Duplicate table indexes in DatabaseSqlite::duplicateTableStructure
authorMarius Hoch <hoo@online.de>
Thu, 6 Aug 2015 00:55:18 +0000 (02:55 +0200)
committerMarius Hoch <hoo@online.de>
Thu, 6 Aug 2015 11:45:31 +0000 (13:45 +0200)
This is consistent with what we do with MySQL and is generally
convenient to have for certain integration tests.

Change-Id: Iaea79bd11263bf75061f19a94da9645ef634422f

includes/db/DatabaseSqlite.php

index 2bad711..9c93951 100644 (file)
@@ -964,7 +964,36 @@ class DatabaseSqlite extends DatabaseBase {
                        }
                }
 
-               return $this->query( $sql, $fname );
+               $res = $this->query( $sql, $fname );
+
+               // Take over indexes
+               $indexList = $this->query( 'PRAGMA INDEX_LIST(' . $this->addQuotes( $oldName ) . ')' );
+               foreach ( $indexList as $index ) {
+                       if ( strpos( $index->name, 'sqlite_autoindex' ) === 0 ) {
+                               continue;
+                       }
+
+                       if ( $index->unique ) {
+                               $sql = 'CREATE UNIQUE INDEX';
+                       } else {
+                               $sql = 'CREATE INDEX';
+                       }
+                       // Try to come up with a new index name, given indexes have database scope in SQLite
+                       $indexName = $newName . '_' . $index->name;
+                       $sql .= ' ' . $indexName . ' ON ' . $newName;
+
+                       $indexInfo = $this->query( 'PRAGMA INDEX_INFO(' . $this->addQuotes( $index->name ) . ')' );
+                       $fields = array();
+                       foreach ( $indexInfo as $indexInfoRow ) {
+                               $fields[ $indexInfoRow->seqno ] = $indexInfoRow->name;
+                       }
+
+                       $sql .= '(' . implode( ',', $fields ) . ')';
+
+                       $this->query( $sql );
+               }
+
+               return $res;
        }
 
        /**