John Erling Blad has submitted this change and it was merged.

Change subject: Implementing create table in the query interface
......................................................................


Implementing create table in the query interface

Change-Id: I763305af860c44867b7119769f93f5a4e3b62732
---
M repo/config/Wikibase.experimental.php
A repo/includes/Database/MWDB/ExtendedAbstraction.php
A repo/includes/Database/MWDB/ExtendedMySQLAbstraction.php
M repo/includes/Database/MediaWikiQueryInterface.php
M repo/includes/Database/ObservableQueryInterface.php
M repo/includes/Database/QueryInterface.php
A repo/tests/phpunit/includes/Database/MWDB/ExtendedAbstractionTest.php
A repo/tests/phpunit/includes/Database/MWDB/ExtendedMySQLAbstractionTest.php
A repo/tests/phpunit/includes/Database/MediaWikiQueryInterfaceTest.php
9 files changed, 657 insertions(+), 4 deletions(-)

Approvals:
  John Erling Blad: Verified; Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/config/Wikibase.experimental.php 
b/repo/config/Wikibase.experimental.php
index 57193a5..483e855 100644
--- a/repo/config/Wikibase.experimental.php
+++ b/repo/config/Wikibase.experimental.php
@@ -47,6 +47,9 @@
 
 
 $classes = array(
+       'Wikibase\Repo\Database\MWDB\ExtendedAbstraction',
+       'Wikibase\Repo\Database\MWDB\ExtendedMySQLAbstraction',
+
        'Wikibase\Repo\Database\FieldDefinition',
        'Wikibase\Repo\Database\MediaWikiQueryInterface',
        'Wikibase\Repo\Database\ObservableQueryInterface',
@@ -84,6 +87,9 @@
 
        $wgAutoloadClasses['Wikibase\Repo\Test\Query\QueryStoreTest']
                = $dir . 'tests/phpunit/includes/Query/QueryStoreTest.php';
+
+       
$wgAutoloadClasses['Wikibase\Repo\Test\Database\MWDB\ExtendedAbstractionTest']
+               = $dir . 
'tests/phpunit/includes/Database/MWDB/ExtendedAbstractionTest.php';
 }
 
 unset( $dir );
@@ -118,7 +124,10 @@
                'content/QueryContent',
                'content/QueryHandler',
 
+               'Database/MWDB/ExtendedMySQLAbstraction',
+
                'Database/FieldDefinition',
+               'Database/MediaWikiQueryInterface',
                'Database/TableBuilder',
                'Database/TableDefinition',
 
diff --git a/repo/includes/Database/MWDB/ExtendedAbstraction.php 
b/repo/includes/Database/MWDB/ExtendedAbstraction.php
new file mode 100644
index 0000000..5c06b34
--- /dev/null
+++ b/repo/includes/Database/MWDB/ExtendedAbstraction.php
@@ -0,0 +1,99 @@
+<?php
+
+namespace Wikibase\Repo\Database\MWDB;
+
+use Wikibase\Repo\Database\TableDefinition;
+use Wikibase\Repo\DBConnectionProvider;
+use InvalidArgumentException;
+use DatabaseBase;
+
+/**
+ * Base database abstraction class to put stuff into that is not present
+ * in the MW core db abstraction layer.
+ *
+ * Like to core class DatabaseBase, each deriving class provides support
+ * for a specific type of database.
+ *
+ * Everything implemented in these classes could go into DatabaseBase and
+ * deriving classes, though this might take quite some time, hence 
implementation
+ * is first done here. If you feel like taking core CR crap and waiting a few
+ * months, by all means try to get the functionality into core.
+ *
+ * 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
+ *
+ * @since wd.db
+ *
+ * @file
+ * @ingroup WikibaseRepo
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+abstract class ExtendedAbstraction {
+
+       /**
+        * @since wd.db
+        *
+        * @var DBConnectionProvider
+        */
+       private $connectionProvider;
+
+       /**
+        * @since wd.db
+        *
+        * @param DBConnectionProvider $connectionProvider
+        */
+       public function __construct( DBConnectionProvider $connectionProvider ) 
{
+               $this->connectionProvider = $connectionProvider;
+       }
+
+       /**
+        * @since wd.db
+        *
+        * @return DatabaseBase
+        * @throws InvalidArgumentException
+        */
+       public function getDB() {
+               $db = $this->connectionProvider->getConnection();
+
+               if ( $db->getType() !== $this->getType() ) {
+                       throw new InvalidArgumentException();
+               }
+
+               return $db;
+       }
+
+       /**
+        * Create the provided table.
+        *
+        * @since wd.db
+        *
+        * @param TableDefinition $table
+        *
+        * @return boolean Success indicator
+        */
+       public abstract function createTable( TableDefinition $table );
+
+       /**
+        * Returns the type of the supported MW DB abstraction class.
+        *
+        * @since wd.db
+        *
+        * @return string
+        */
+       protected abstract function getType();
+
+}
\ No newline at end of file
diff --git a/repo/includes/Database/MWDB/ExtendedMySQLAbstraction.php 
b/repo/includes/Database/MWDB/ExtendedMySQLAbstraction.php
new file mode 100644
index 0000000..84da264
--- /dev/null
+++ b/repo/includes/Database/MWDB/ExtendedMySQLAbstraction.php
@@ -0,0 +1,125 @@
+<?php
+
+namespace Wikibase\Repo\Database\MWDB;
+
+use Wikibase\Repo\Database\TableDefinition;
+use Wikibase\Repo\Database\FieldDefinition;
+use RuntimeException;
+
+/**
+ * MySQL implementation of ExtendedAbstraction.
+ *
+ * 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
+ *
+ * @since wd.db
+ *
+ * @file
+ * @ingroup WikibaseRepo
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class ExtendedMySQLAbstraction extends ExtendedAbstraction {
+
+       /**
+        * @see ExtendedAbstraction::getType
+        *
+        * @since wd.db
+        *
+        * @return string
+        */
+       protected function getType() {
+               return 'mysql';
+       }
+
+       /**
+        * @see ExtendedAbstraction::createTable
+        *
+        * @since wd.db
+        *
+        * @param TableDefinition $table
+        *
+        * @return boolean Success indicator
+        */
+       public function createTable( TableDefinition $table ) {
+               $db = $this->getDB();
+
+               // TODO: Escape table name?
+               // TODO: get rid of global (DatabaseBase currently provides no 
access to its mTablePrefix field)
+               $sql = 'CREATE TABLE `' . $db->getDBname() . '`.' . 
$GLOBALS['wgDBprefix'] . $table->getName() . ' (';
+
+               $fields = array();
+
+               foreach ( $table->getFields() as $field ) {
+                       $fields[] = $field->getName() . ' ' . 
$this->getFieldSQL( $field );
+               }
+
+               $sql .= implode( ',', $fields );
+
+               // TODO: table options
+               $sql .= ') ' . 'ENGINE=InnoDB, DEFAULT CHARSET=binary';
+
+               $success = $db->query( $sql, __METHOD__ );
+
+               return $success !== false;
+       }
+
+       /**
+        * @since wd.db
+        *
+        * @param FieldDefinition $field
+        *
+        * @return string
+        * @throws RuntimeException
+        */
+       protected function getFieldSQL( FieldDefinition $field ) {
+               $sql = $this->getFieldType( $field->getType() );
+
+               if ( $field->getDefault() !== null ) {
+                       $sql .= ' DEFAULT ' . $this->getDB()->addQuotes( 
$field->getDefault() );
+               }
+
+               // TODO: add all field stuff relevant here
+
+               $sql .= $field->allowsNull() ? ' NULL' : ' NOT NULL';
+
+               return $sql;
+       }
+
+       /**
+        * Returns the MySQL field type for a given FieldDefinition type 
constant.
+        *
+        * @param string $fieldType
+        *
+        * @return string
+        * @throws RuntimeException
+        */
+       protected function getFieldType( $fieldType ) {
+               switch ( $fieldType ) {
+                       case FieldDefinition::TYPE_INTEGER:
+                               return 'INT';
+                       case FieldDefinition::TYPE_FLOAT:
+                               return 'FLOAT';
+                       case FieldDefinition::TYPE_TEXT:
+                               return 'BLOB';
+                       case FieldDefinition::TYPE_BOOLEAN:
+                               return 'TINYINT';
+                       default:
+                               throw new RuntimeException( __CLASS__ . ' does 
not support db fields of type ' . $fieldType );
+               }
+       }
+
+}
diff --git a/repo/includes/Database/MediaWikiQueryInterface.php 
b/repo/includes/Database/MediaWikiQueryInterface.php
index a4a5e6d..86313f0 100644
--- a/repo/includes/Database/MediaWikiQueryInterface.php
+++ b/repo/includes/Database/MediaWikiQueryInterface.php
@@ -3,6 +3,8 @@
 namespace Wikibase\Repo\Database;
 
 use Wikibase\Repo\DBConnectionProvider;
+use Wikibase\Repo\Database\TableDefinition;
+use Wikibase\Repo\Database\MWDB\ExtendedAbstraction;
 
 /**
  * Implementation of the QueryInterface interface using the MediaWiki
@@ -39,10 +41,21 @@
        private $connectionProvider;
 
        /**
-        * @param DBConnectionProvider $connectionProvider
+        * @var ExtendedAbstraction
         */
-       public function __construct( DBConnectionProvider $connectionProvider ) 
{
+       private $extendedAbstraction;
+
+       /**
+        * Constructor.
+        *
+        * @since wd.db
+        *
+        * @param DBConnectionProvider $connectionProvider
+        * @param ExtendedAbstraction $extendedAbstraction
+        */
+       public function __construct( DBConnectionProvider $connectionProvider, 
ExtendedAbstraction $extendedAbstraction ) {
                $this->connectionProvider = $connectionProvider;
+               $this->extendedAbstraction = $extendedAbstraction;
        }
 
        /**
@@ -55,6 +68,8 @@
        /**
         * @see QueryInterface::tableExists
         *
+        * @since wd.db
+        *
         * @param string $tableName
         *
         * @return boolean
@@ -63,4 +78,30 @@
                return $this->getDB()->tableExists( $tableName, __METHOD__ );
        }
 
+       /**
+        * @see QueryInterface::createTable
+        *
+        * @since wd.db
+        *
+        * @param TableDefinition $table
+        *
+        * @return boolean Success indicator
+        */
+       public function createTable( TableDefinition $table ) {
+               return $this->extendedAbstraction->createTable( $table );
+       }
+
+       /**
+        * @see QueryInterface::dropTable
+        *
+        * @since wd.db
+        *
+        * @param string $tableName
+        *
+        * @return boolean Success indicator
+        */
+       public function dropTable( $tableName ) {
+               return $this->getDB()->dropTable( $tableName, __METHOD__ ) !== 
false;
+       }
+
 }
diff --git a/repo/includes/Database/ObservableQueryInterface.php 
b/repo/includes/Database/ObservableQueryInterface.php
index c4b9b56..48af945 100644
--- a/repo/includes/Database/ObservableQueryInterface.php
+++ b/repo/includes/Database/ObservableQueryInterface.php
@@ -39,6 +39,11 @@
        private $callbacks = array();
 
        /**
+        * Register a callback that should be called whenever the methods
+        * which name is provided is called with the arguments this method got.
+        *
+        * @since wd.db
+        *
         * @param string $method
         * @param callable $callback
         */
@@ -46,7 +51,13 @@
                $this->callbacks[$method] = $callback;
        }
 
-       private function runCallbacks( $method, $args ) {
+       /**
+        * @since wd.db
+        *
+        * @param string $method
+        * @param array $args
+        */
+       private function runCallbacks( $method, array $args ) {
                if ( array_key_exists( $method, $this->callbacks ) ) {
                        call_user_func_array( $this->callbacks[$method], $args 
);
                }
@@ -55,6 +66,8 @@
        /**
         * @see QueryInterface::tableExists
         *
+        * @since wd.db
+        *
         * @param string $tableName
         *
         * @return boolean
@@ -63,4 +76,30 @@
                $this->runCallbacks( __FUNCTION__, func_get_args() );
        }
 
+       /**
+        * @see QueryInterface::createTable
+        *
+        * @since wd.db
+        *
+        * @param TableDefinition $table
+        *
+        * @return boolean
+        */
+       public function createTable( TableDefinition $table ) {
+               $this->runCallbacks( __FUNCTION__, func_get_args() );
+       }
+
+       /**
+        * @see QueryInterface::dropTable
+        *
+        * @since wd.db
+        *
+        * @param string $tableName
+        *
+        * @return boolean Success indicator
+        */
+       public function dropTable( $tableName ) {
+               $this->runCallbacks( __FUNCTION__, func_get_args() );
+       }
+
 }
diff --git a/repo/includes/Database/QueryInterface.php 
b/repo/includes/Database/QueryInterface.php
index 7619f9b..419fca4 100644
--- a/repo/includes/Database/QueryInterface.php
+++ b/repo/includes/Database/QueryInterface.php
@@ -33,10 +33,34 @@
        /**
         * Returns if the table exists in the database.
         *
+        * @since wd.db
+        *
         * @param string $tableName
         *
-        * @return boolean
+        * @return boolean Success indicator
         */
        public function tableExists( $tableName );
 
+       /**
+        * Creates a table based on the provided definition in the store.
+        *
+        * @since wd.db
+        *
+        * @param TableDefinition $table
+        *
+        * @return boolean Success indicator
+        */
+       public function createTable( TableDefinition $table );
+
+       /**
+        * Removes the table with provided name from the store.
+        *
+        * @since wd.db
+        *
+        * @param string $tableName
+        *
+        * @return boolean Success indicator
+        */
+       public function dropTable( $tableName );
+
 }
diff --git 
a/repo/tests/phpunit/includes/Database/MWDB/ExtendedAbstractionTest.php 
b/repo/tests/phpunit/includes/Database/MWDB/ExtendedAbstractionTest.php
new file mode 100644
index 0000000..810a71f
--- /dev/null
+++ b/repo/tests/phpunit/includes/Database/MWDB/ExtendedAbstractionTest.php
@@ -0,0 +1,116 @@
+<?php
+
+namespace Wikibase\Repo\Test\Database\MWDB;
+
+use Wikibase\Repo\Database\MWDB\ExtendedAbstraction;
+use Wikibase\Repo\Database\TableDefinition;
+use Wikibase\Repo\Database\FieldDefinition;
+
+/**
+ * Base class with tests for the 
Wikibase\Repo\Database\MWDB\ExtendedAbstraction deriving classes.
+ *
+ * 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
+ * @since wd.db
+ *
+ * @ingroup WikibaseRepoTest
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+abstract class ExtendedAbstractionTest extends \MediaWikiTestCase {
+
+       /**
+        * @return ExtendedAbstraction
+        */
+       protected abstract function newInstance();
+
+       protected function tearDown() {
+               parent::tearDown();
+
+               $this->dropTablesIfStillThere();
+       }
+
+       protected function dropTablesIfStillThere() {
+               $queryInterface = $this->newInstance();
+
+               foreach ( array( 'differentfieldtypes', 'defaultfieldvalues', 
'notnullfields' ) as $tableName ) {
+                       if ( $queryInterface->getDB()->tableExists( $tableName 
) ) {
+                               $queryInterface->getDB()->dropTable( $tableName 
);
+                       }
+               }
+       }
+
+       public function tableProvider() {
+               $tables = array();
+
+               $tables[] = new TableDefinition( 'differentfieldtypes', array(
+                       new FieldDefinition( 'intfield', 
FieldDefinition::TYPE_INTEGER ),
+                       new FieldDefinition( 'floatfield', 
FieldDefinition::TYPE_FLOAT ),
+                       new FieldDefinition( 'textfield', 
FieldDefinition::TYPE_TEXT ),
+                       new FieldDefinition( 'boolfield', 
FieldDefinition::TYPE_BOOLEAN ),
+               ) );
+
+               $tables[] = new TableDefinition( 'defaultfieldvalues', array(
+                       new FieldDefinition( 'intfield', 
FieldDefinition::TYPE_INTEGER, 42 ),
+               ) );
+
+               $tables[] = new TableDefinition( 'notnullfields', array(
+                       new FieldDefinition( 'intfield', 
FieldDefinition::TYPE_INTEGER, null, null, false ),
+                       new FieldDefinition( 'textfield', 
FieldDefinition::TYPE_TEXT, null, null, false ),
+               ) );
+
+               return $this->arrayWrap( $tables );
+       }
+
+       /**
+        * @dataProvider tableProvider
+        *
+        * @param TableDefinition $table
+        */
+       public function testCreateAndDropTable( TableDefinition $table ) {
+               $extendedAbstraction = $this->newInstance();
+
+               $this->assertFalse(
+                       $extendedAbstraction->getDB()->tableExists( 
$table->getName() ),
+                       'Table should not exist before creation'
+               );
+
+               $success = $extendedAbstraction->createTable( $table );
+
+               $this->assertTrue(
+                       $success,
+                       'Creation function returned success'
+               );
+
+               $this->assertTrue(
+                       $extendedAbstraction->getDB()->tableExists( 
$table->getName() ),
+                       'Table "' . $table->getName() . '" exists after 
creation'
+               );
+
+               $this->assertTrue(
+                       $extendedAbstraction->getDB()->dropTable( 
$table->getName() ),
+                       'Table removal worked'
+               );
+
+               $this->assertFalse(
+                       $extendedAbstraction->getDB()->tableExists( 
$table->getName() ),
+                       'Table should not exist after deletion'
+               );
+       }
+
+}
diff --git 
a/repo/tests/phpunit/includes/Database/MWDB/ExtendedMySQLAbstractionTest.php 
b/repo/tests/phpunit/includes/Database/MWDB/ExtendedMySQLAbstractionTest.php
new file mode 100644
index 0000000..1c173b5
--- /dev/null
+++ b/repo/tests/phpunit/includes/Database/MWDB/ExtendedMySQLAbstractionTest.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Wikibase\Repo\Test\Database\MWDB;
+
+use Wikibase\Repo\Database\MWDB\ExtendedMySQLAbstraction;
+use Wikibase\Repo\LazyDBConnectionProvider;
+
+/**
+ * Unit tests for the Wikibase\Repo\Database\MWDB\ExtendedMySQLAbstraction 
class.
+ *
+ * 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
+ * @since wd.db
+ *
+ * @ingroup WikibaseRepoTest
+ *
+ * @group Wikibase
+ * @group WikibaseRepo
+ * @group WikibaseDatabase
+ * @group Database
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class ExtendedMySQLAbstractionTest extends ExtendedAbstractionTest {
+
+       /**
+        * @see ExtendedAbstractionTest::newInstance
+        *
+        * @return ExtendedMySQLAbstraction
+        */
+       protected function newInstance() {
+               return new ExtendedMySQLAbstraction( new 
LazyDBConnectionProvider( DB_MASTER ) );
+       }
+
+}
diff --git 
a/repo/tests/phpunit/includes/Database/MediaWikiQueryInterfaceTest.php 
b/repo/tests/phpunit/includes/Database/MediaWikiQueryInterfaceTest.php
new file mode 100644
index 0000000..85603f5
--- /dev/null
+++ b/repo/tests/phpunit/includes/Database/MediaWikiQueryInterfaceTest.php
@@ -0,0 +1,150 @@
+<?php
+
+namespace Wikibase\Repo\Test\Database;
+
+use Wikibase\Repo\Database\MediaWikiQueryInterface;
+use Wikibase\Repo\Database\TableDefinition;
+use Wikibase\Repo\Database\FieldDefinition;
+
+/**
+ * Unit tests for the Wikibase\Repo\Database\MediaWikiQueryInterface class.
+ *
+ * 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
+ * @since wd.db
+ *
+ * @ingroup WikibaseRepoTest
+ *
+ * @group Wikibase
+ * @group WikibaseRepo
+ * @group WikibaseDatabase
+ * @group Database
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class MediaWikiQueryInterfaceTest extends \MediaWikiTestCase {
+
+       protected function tearDown() {
+               parent::tearDown();
+
+               $this->dropTablesIfStillThere();
+       }
+
+       protected function dropTablesIfStillThere() {
+               $queryInterface = $this->newInstance();
+
+               foreach ( array( 'differentfieldtypes', 'defaultfieldvalues', 
'notnullfields' ) as $tableName ) {
+                       if ( $queryInterface->tableExists( $tableName ) ) {
+                               $queryInterface->dropTable( $tableName );
+                       }
+               }
+       }
+
+       /**
+        * @return MediaWikiQueryInterface
+        */
+       protected function newInstance() {
+               $conn = new \Wikibase\Repo\LazyDBConnectionProvider( DB_MASTER 
);
+
+               return new MediaWikiQueryInterface(
+                       $conn,
+                       new 
\Wikibase\Repo\Database\MWDB\ExtendedMySQLAbstraction( $conn )
+               );
+       }
+
+       public function tableExistsProvider() {
+               $argLists = array();
+
+               $argLists[] = array( 'user', true );
+               $argLists[] = array( 'xdgxftjhreyetfytj', false );
+
+               return $argLists;
+       }
+
+       /**
+        * @dataProvider tableExistsProvider
+        *
+        * @param string $tableName
+        * @param boolean $expected
+        */
+       public function testTableExists( $tableName, $expected ) {
+               $actual = $this->newInstance()->tableExists( $tableName );
+
+               $this->assertInternalType( 'boolean', $actual );
+               $this->assertEquals( $expected, $actual );
+       }
+
+       public function tableProvider() {
+               $tables = array();
+
+               $tables[] = new TableDefinition( 'differentfieldtypes', array(
+                       new FieldDefinition( 'intfield', 
FieldDefinition::TYPE_INTEGER ),
+                       new FieldDefinition( 'floatfield', 
FieldDefinition::TYPE_FLOAT ),
+                       new FieldDefinition( 'textfield', 
FieldDefinition::TYPE_TEXT ),
+                       new FieldDefinition( 'boolfield', 
FieldDefinition::TYPE_BOOLEAN ),
+               ) );
+
+               $tables[] = new TableDefinition( 'defaultfieldvalues', array(
+                       new FieldDefinition( 'intfield', 
FieldDefinition::TYPE_INTEGER, 42 ),
+               ) );
+
+               $tables[] = new TableDefinition( 'notnullfields', array(
+                       new FieldDefinition( 'intfield', 
FieldDefinition::TYPE_INTEGER, null, null, false ),
+                       new FieldDefinition( 'textfield', 
FieldDefinition::TYPE_TEXT, null, null, false ),
+               ) );
+
+               return $this->arrayWrap( $tables );
+       }
+
+       /**
+        * @dataProvider tableProvider
+        *
+        * @param TableDefinition $table
+        */
+       public function testCreateAndDropTable( TableDefinition $table ) {
+               $queryInterface = $this->newInstance();
+
+               $this->assertFalse(
+                       $queryInterface->tableExists( $table->getName() ),
+                       'Table should not exist before creation'
+               );
+
+               $success = $queryInterface->createTable( $table );
+
+               $this->assertTrue(
+                       $success,
+                       'Creation function returned success'
+               );
+
+               $this->assertTrue(
+                       $queryInterface->tableExists( $table->getName() ),
+                       'Table "' . $table->getName() . '" exists after 
creation'
+               );
+
+               $this->assertTrue(
+                       $queryInterface->dropTable( $table->getName() ),
+                       'Table removal worked'
+               );
+
+               $this->assertFalse(
+                       $queryInterface->tableExists( $table->getName() ),
+                       'Table should not exist after deletion'
+               );
+       }
+
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I763305af860c44867b7119769f93f5a4e3b62732
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: John Erling Blad <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to