Jeroen De Dauw has submitted this change and it was merged.

Change subject: fix SQLiteTableDefinitionReader and Unique indexes
......................................................................


fix SQLiteTableDefinitionReader and Unique indexes

Change-Id: I0f51a3561b0bc82678d86374c72f375ccf887795
---
M src/SQLite/SQLiteTableDefinitionReader.php
M tests/integration/MediaWiki/Schema/TableCreateReadDeleteTest.php
M tests/phpunit/SQLite/SQLiteTableDefinitionReaderTest.php
3 files changed, 33 insertions(+), 9 deletions(-)

Approvals:
  Jeroen De Dauw: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/src/SQLite/SQLiteTableDefinitionReader.php 
b/src/SQLite/SQLiteTableDefinitionReader.php
index 1c9eedf..a256f24 100644
--- a/src/SQLite/SQLiteTableDefinitionReader.php
+++ b/src/SQLite/SQLiteTableDefinitionReader.php
@@ -111,7 +111,6 @@
                        default:
                                throw new RuntimeException( __CLASS__ . ' does 
not support db fields of type ' . $type );
                }
-
        }
 
        private function getFieldDefault( $default ) {
@@ -147,14 +146,15 @@
        }
 
        private function getIndex( $sql ){
-               preg_match( '/CREATE ([^ ]+) ([^ ]+) ON ([^ ]+) \((.+)\)\z/', 
$sql, $createParts );
+               preg_match( '/CREATE (INDEX|UNIQUE INDEX) ([^ ]+) ON ([^ ]+) 
\((.+)\)\z/', $sql, $createParts );
                $parsedColumns = explode( ',', $createParts[4] );
                $columns = array();
                foreach( $parsedColumns as $columnName ){
                        //default unrestricted index size limit
                        $columns[ $columnName ] = 0;
                }
-               return new IndexDefinition( $createParts[2], $columns , 
strtolower( $createParts[1] ) );
+               $type = $this->getIndexType( $createParts[1] );
+               return new IndexDefinition( $createParts[2], $columns , $type );
        }
 
        /**
@@ -170,6 +170,19 @@
                );
        }
 
+       private function getIndexType( $type ) {
+               switch ( $type ) {
+                       case 'INDEX':
+                               return IndexDefinition::TYPE_INDEX;
+                               break;
+                       case 'UNIQUE INDEX':
+                               return IndexDefinition::TYPE_UNIQUE;
+                               break;
+                       default:
+                               throw new RuntimeException( __CLASS__ . ' does 
not support db indexes of type ' . $type );
+               }
+       }
+
        /**
         * Returns an array of all primary keys for a given table
         * @param string $tableName
diff --git a/tests/integration/MediaWiki/Schema/TableCreateReadDeleteTest.php 
b/tests/integration/MediaWiki/Schema/TableCreateReadDeleteTest.php
index 2aef0c6..dbcc34f 100644
--- a/tests/integration/MediaWiki/Schema/TableCreateReadDeleteTest.php
+++ b/tests/integration/MediaWiki/Schema/TableCreateReadDeleteTest.php
@@ -89,8 +89,8 @@
                                new FieldDefinition( 'boolfield', 
FieldDefinition::TYPE_BOOLEAN, FieldDefinition::NOT_NULL, true ),
                        ),
                        array(
-                               //TODO uncomment the below line in test once 
text keys correctly specify a key length
-                               //new IndexDefinition( 'PRIMARY', array( 
'textfield' => 100 ), IndexDefinition::TYPE_PRIMARY ),
+                               //TODO test PRIMARY key
+                               //TODO test UNIQUE INDEX
                                new IndexDefinition( 'somename', array( 
'intfield' => 0, 'floatfield' => 0 ) )
                        )
                );
diff --git a/tests/phpunit/SQLite/SQLiteTableDefinitionReaderTest.php 
b/tests/phpunit/SQLite/SQLiteTableDefinitionReaderTest.php
index f3d5c17..4bfbb83 100644
--- a/tests/phpunit/SQLite/SQLiteTableDefinitionReaderTest.php
+++ b/tests/phpunit/SQLite/SQLiteTableDefinitionReaderTest.php
@@ -50,8 +50,8 @@
         */
        public function testReadDefinition( $results, TableDefinition 
$expectedDefinition ) {
                $reader = $this->newInstance( $results );
-               $definition = $reader->readDefinition( 'dbNametableName' );
-               $this->assertEquals( $definition, $expectedDefinition );
+               $readDefinition = $reader->readDefinition( 'dbNametableName' );
+               $this->assertEquals( $expectedDefinition, $readDefinition );
        }
 
        public function sqlAndDefinitionProvider() {
@@ -59,9 +59,15 @@
 
                $argLists[] = array(
                        array(
+                               //create sql
                                array( (object)array( 'sql' => 'CREATE TABLE 
dbNametableName (primaryField INT NOT NULL, textField BLOB NULL, intField INT 
DEFAULT 42 NOT NULL, PRIMARY KEY (textField, primaryField))' ) ),
-                               array( (object)array( 'sql' => 'CREATE INDEX 
indexName ON dbNametableName (intField,textField)' ) ),
-                               array( (object)array( 'sql' => 'CREATE TABLE 
dbNametableName (primaryField INT NOT NULL, textField BLOB NULL, intField INT 
DEFAULT 42 NOT NULL, PRIMARY KEY (textField, primaryField))' ) ),
+                               //indexes sql
+                               array(
+                                       (object)array( 'sql' => 'CREATE UNIQUE 
INDEX uniqueName ON dbNametableName (textField)' ),
+                                       (object)array( 'sql' => 'CREATE INDEX 
indexName ON dbNametableName (intField,textField)' )
+                               ),
+                               //primarykey sql
+                               array( (object)array( 'sql' => 'PRIMARY KEY 
(textField,primaryField)' ) ),
                        ),
                        new TableDefinition(
                                'dbNametableName',
@@ -90,6 +96,11 @@
                                                IndexDefinition::TYPE_INDEX
                                        ),
                                        new IndexDefinition(
+                                               'uniqueName',
+                                               array( 'textField' => 0 ),
+                                               IndexDefinition::TYPE_UNIQUE
+                                       ),
+                                       new IndexDefinition(
                                                'PRIMARY',
                                                array( 'textField' => 0, 
'primaryField' => 0 ),
                                                IndexDefinition::TYPE_PRIMARY

-- 
To view, visit https://gerrit.wikimedia.org/r/89527
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I0f51a3561b0bc82678d86374c72f375ccf887795
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseDatabase
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to