Jeroen De Dauw has uploaded a new change for review.
https://gerrit.wikimedia.org/r/89136
Change subject: Implement index modification methods in MediaWikiSchemaModifier
......................................................................
Implement index modification methods in MediaWikiSchemaModifier
Change-Id: I39b8c9a8dd503fe8fffa7621eaf71d1ffc86d5ba
---
M src/MediaWiki/MediaWikiSchemaModifier.php
M src/Schema/SchemaModifier.php
M tests/phpunit/MediaWiki/MediaWikiSchemaModifierTest.php
3 files changed, 124 insertions(+), 6 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseDatabase
refs/changes/36/89136/1
diff --git a/src/MediaWiki/MediaWikiSchemaModifier.php
b/src/MediaWiki/MediaWikiSchemaModifier.php
index 4ec2942..58e804a 100644
--- a/src/MediaWiki/MediaWikiSchemaModifier.php
+++ b/src/MediaWiki/MediaWikiSchemaModifier.php
@@ -8,6 +8,8 @@
use Wikibase\Database\Schema\Definitions\IndexDefinition;
use Wikibase\Database\Schema\FieldAdditionFailedException;
use Wikibase\Database\Schema\FieldRemovalFailedException;
+use Wikibase\Database\Schema\IndexAdditionFailedException;
+use Wikibase\Database\Schema\IndexRemovalFailedException;
use Wikibase\Database\Schema\SchemaModificationSqlBuilder;
use Wikibase\Database\Schema\SchemaModifier;
@@ -91,10 +93,21 @@
* @param string $tableName
* @param string $indexName
*
- * TODO: document throws
+ * @throws IndexRemovalFailedException
*/
public function removeIndex( $tableName, $indexName ) {
- // TODO
+ $success = $this->getDB()->query(
+ $this->sqlBuilder->getRemoveIndexSql( $tableName,
$indexName ),
+ __METHOD__
+ );
+
+ if ( $success === false ) {
+ throw new IndexRemovalFailedException(
+ $tableName,
+ $indexName,
+ $this->getDB()->lastQuery()
+ );
+ }
}
/**
@@ -103,10 +116,21 @@
* @param string $tableName
* @param IndexDefinition $index
*
- * TODO: document throws
+ * @throws IndexAdditionFailedException
*/
public function addIndex( $tableName, IndexDefinition $index ) {
- // TODO
+ $success = $this->getDB()->query(
+ $this->sqlBuilder->getAddIndexSql( $tableName, $index ),
+ __METHOD__
+ );
+
+ if ( $success === false ) {
+ throw new IndexAdditionFailedException(
+ $tableName,
+ $index,
+ $this->getDB()->lastQuery()
+ );
+ }
}
}
diff --git a/src/Schema/SchemaModifier.php b/src/Schema/SchemaModifier.php
index d9812d5..df4e368 100644
--- a/src/Schema/SchemaModifier.php
+++ b/src/Schema/SchemaModifier.php
@@ -32,7 +32,7 @@
* @param string $tableName
* @param string $indexName
*
- * TODO: document throws
+ * @throws IndexRemovalFailedException
*/
public function removeIndex( $tableName, $indexName );
@@ -40,7 +40,7 @@
* @param string $tableName
* @param IndexDefinition $index
*
- * TODO: document throws
+ * @throws IndexAdditionFailedException
*/
public function addIndex( $tableName, IndexDefinition $index );
diff --git a/tests/phpunit/MediaWiki/MediaWikiSchemaModifierTest.php
b/tests/phpunit/MediaWiki/MediaWikiSchemaModifierTest.php
index 59c1bd3..faed531 100644
--- a/tests/phpunit/MediaWiki/MediaWikiSchemaModifierTest.php
+++ b/tests/phpunit/MediaWiki/MediaWikiSchemaModifierTest.php
@@ -82,6 +82,63 @@
$modifier->addField( $tableName, $field );
}
+ public function testRemoveIndex() {
+ $tableName = 'tableName';
+ $indexName = 'indexName';
+ $sql = 'foo bar baz';
+
+ $sqlBuilder = $this->getMock(
'Wikibase\Database\Schema\SchemaModificationSqlBuilder' );
+ $sqlBuilder->expects( $this->once() )
+ ->method( 'getRemoveIndexSql' )
+ ->with(
+ $this->equalTo( $tableName ),
+ $this->equalTo( $indexName )
+ )
+ ->will( $this->returnValue( $sql ) );
+
+ $connection = $this->getMock( 'DatabaseMysql' );
+
+ $connection->expects( $this->once() )
+ ->method( 'query' )
+ ->with( $this->equalTo( $sql ) )
+ ->will( $this->returnValue( true ) );
+
+ $connectionProvider = $this->getMockConnectionProvider(
$connection );
+
+ $modifier = new MediaWikiSchemaModifier( $connectionProvider,
$sqlBuilder );
+
+ $modifier->removeIndex( $tableName, $indexName );
+ }
+
+ public function testAddIndex() {
+ $tableName = 'indexName';
+ $index = $this->getMockBuilder(
'Wikibase\Database\Schema\Definitions\IndexDefinition' )
+ ->disableOriginalConstructor()->getMock();
+ $sql = 'foo bar baz';
+
+ $sqlBuilder = $this->getMock(
'Wikibase\Database\Schema\SchemaModificationSqlBuilder' );
+ $sqlBuilder->expects( $this->once() )
+ ->method( 'getAddIndexSql' )
+ ->with(
+ $this->equalTo( $tableName ),
+ $this->equalTo( $index )
+ )
+ ->will( $this->returnValue( $sql ) );
+
+ $connection = $this->getMock( 'DatabaseMysql' );
+
+ $connection->expects( $this->once() )
+ ->method( 'query' )
+ ->with( $this->equalTo( $sql ) )
+ ->will( $this->returnValue( true ) );
+
+ $connectionProvider = $this->getMockConnectionProvider(
$connection );
+
+ $modifier = new MediaWikiSchemaModifier( $connectionProvider,
$sqlBuilder );
+
+ $modifier->addIndex( $tableName, $index );
+ }
+
public function testRemoveFieldThrowsExceptionOnQueryFailure() {
$sqlBuilder = $this->getMock(
'Wikibase\Database\Schema\SchemaModificationSqlBuilder' );
@@ -119,4 +176,41 @@
$modifier->addField( 'foo', $field );
}
+ public function testRemoveIndexThrowsExceptionOnQueryFailure() {
+ $sqlBuilder = $this->getMock(
'Wikibase\Database\Schema\SchemaModificationSqlBuilder' );
+
+ $connection = $this->getMock( 'DatabaseMysql' );
+
+ $connection->expects( $this->once() )
+ ->method( 'query' )
+ ->will( $this->returnValue( false ) );
+
+ $connectionProvider = $this->getMockConnectionProvider(
$connection );
+
+ $modifier = new MediaWikiSchemaModifier( $connectionProvider,
$sqlBuilder );
+
+ $this->setExpectedException(
'Wikibase\Database\Schema\IndexRemovalFailedException' );
+ $modifier->removeIndex( 'foo', 'bar' );
+ }
+
+ public function testAddIndexThrowsExceptionOnQueryFailure() {
+ $sqlBuilder = $this->getMock(
'Wikibase\Database\Schema\SchemaModificationSqlBuilder' );
+
+ $connection = $this->getMock( 'DatabaseMysql' );
+
+ $connection->expects( $this->once() )
+ ->method( 'query' )
+ ->will( $this->returnValue( false ) );
+
+ $connectionProvider = $this->getMockConnectionProvider(
$connection );
+
+ $modifier = new MediaWikiSchemaModifier( $connectionProvider,
$sqlBuilder );
+
+ $index = $this->getMockBuilder(
'Wikibase\Database\Schema\Definitions\IndexDefinition' )
+ ->disableOriginalConstructor()->getMock();
+
+ $this->setExpectedException(
'Wikibase\Database\Schema\IndexAdditionFailedException' );
+ $modifier->addIndex( 'foo', $index );
+ }
+
}
--
To view, visit https://gerrit.wikimedia.org/r/89136
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I39b8c9a8dd503fe8fffa7621eaf71d1ffc86d5ba
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseDatabase
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits