[MediaWiki-commits] [Gerrit] Fixes in TreeRepository - change (mediawiki...Flow)

2014-02-28 Thread Matthias Mullie (Code Review)
Matthias Mullie has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/116083

Change subject: Fixes in TreeRepository
..

Fixes in TreeRepository

insertSelect will not work in unit tests, since they're in a temp table

Change-Id: I2af85eaad24bf45926dbe2505afdfca82ab28854
---
M includes/Repository/TreeRepository.php
1 file changed, 53 insertions(+), 17 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Flow 
refs/changes/83/116083/1

diff --git a/includes/Repository/TreeRepository.php 
b/includes/Repository/TreeRepository.php
index 9bb47fa..c0b2cde 100644
--- a/includes/Repository/TreeRepository.php
+++ b/includes/Repository/TreeRepository.php
@@ -91,21 +91,57 @@
),
__METHOD__
);
+
if ( $res  $ancestor !== null ) {
-   $res = $dbw-insertSelect(
-   $this-tableName,
-   $this-tableName,
-   array(
-   'tree_descendant_id' = 
$dbw-addQuotes( $descendant-getBinary() ),
-   'tree_ancestor_id' = 
'tree_ancestor_id',
-   'tree_depth' = 'tree_depth + 1',
-   ),
-   array(
-   'tree_descendant_id' = 
$ancestor-getBinary(),
-   ),
-   __METHOD__
-   );
+   try {
+   $res = $dbw-insertSelect(
+   $this-tableName,
+   $this-tableName,
+   array(
+   'tree_descendant_id' = 
$dbw-addQuotes( $descendant-getBinary() ),
+   'tree_ancestor_id' = 
'tree_ancestor_id',
+   'tree_depth' = 'tree_depth + 
1',
+   ),
+   array(
+   'tree_descendant_id' = 
$ancestor-getBinary(),
+   ),
+   __METHOD__
+   );
+   } catch( \DBQueryError $e ) {
+   /*
+* insertSelect won't work on temporary tables 
(as used for MW
+* unit tests), because it refers to the same 
table twice, in
+* one query.
+* In this case, we'll do a separate select  
insert.
+*
+* @see 
https://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html
+* @see 
http://dba.stackexchange.com/questions/45270/mysql-error-1137-hy000-at-line-9-cant-reopen-table-temp-table
+*/
+   if ( $dbw-lastErrno() === 1137 ) {
+   // @todo: needs to be done for ALL 
depths, not just one
+   $rows = $dbw-select(
+   $this-tableName,
+   array( 'tree_depth' ),
+   array( 'tree_descendant_id' = 
$ancestor-getBinary() ),
+   __METHOD__
+   );
+
+   $res = true;
+   foreach ( $rows as $row ) {
+   $res = $dbw-insert(
+   $this-tableName,
+   array(
+   
'tree_descendant_id' = $descendant-getBinary(),
+   
'tree_ancestor_id' = $ancestor-getBinary(),
+   'tree_depth' = 
$row-tree_depth + 1,
+   ),
+   __METHOD__
+   );
+   }
+   }
+   }
}
+
if ( !$res ) {
$this-cache-delete( $parentKey );
$this-cache-delete( $pathKey );
@@ -163,7 +199,7 @@
if ( isset( 

[MediaWiki-commits] [Gerrit] Fixes in TreeRepository - change (mediawiki...Flow)

2014-02-28 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

Change subject: Fixes in TreeRepository
..


Fixes in TreeRepository

insertSelect will not work in unit tests, since they're in a temp table

Change-Id: I2af85eaad24bf45926dbe2505afdfca82ab28854
---
M includes/Repository/TreeRepository.php
1 file changed, 53 insertions(+), 17 deletions(-)

Approvals:
  EBernhardson: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/Repository/TreeRepository.php 
b/includes/Repository/TreeRepository.php
index 9bb47fa..c0b2cde 100644
--- a/includes/Repository/TreeRepository.php
+++ b/includes/Repository/TreeRepository.php
@@ -91,21 +91,57 @@
),
__METHOD__
);
+
if ( $res  $ancestor !== null ) {
-   $res = $dbw-insertSelect(
-   $this-tableName,
-   $this-tableName,
-   array(
-   'tree_descendant_id' = 
$dbw-addQuotes( $descendant-getBinary() ),
-   'tree_ancestor_id' = 
'tree_ancestor_id',
-   'tree_depth' = 'tree_depth + 1',
-   ),
-   array(
-   'tree_descendant_id' = 
$ancestor-getBinary(),
-   ),
-   __METHOD__
-   );
+   try {
+   $res = $dbw-insertSelect(
+   $this-tableName,
+   $this-tableName,
+   array(
+   'tree_descendant_id' = 
$dbw-addQuotes( $descendant-getBinary() ),
+   'tree_ancestor_id' = 
'tree_ancestor_id',
+   'tree_depth' = 'tree_depth + 
1',
+   ),
+   array(
+   'tree_descendant_id' = 
$ancestor-getBinary(),
+   ),
+   __METHOD__
+   );
+   } catch( \DBQueryError $e ) {
+   /*
+* insertSelect won't work on temporary tables 
(as used for MW
+* unit tests), because it refers to the same 
table twice, in
+* one query.
+* In this case, we'll do a separate select  
insert.
+*
+* @see 
https://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html
+* @see 
http://dba.stackexchange.com/questions/45270/mysql-error-1137-hy000-at-line-9-cant-reopen-table-temp-table
+*/
+   if ( $dbw-lastErrno() === 1137 ) {
+   // @todo: needs to be done for ALL 
depths, not just one
+   $rows = $dbw-select(
+   $this-tableName,
+   array( 'tree_depth' ),
+   array( 'tree_descendant_id' = 
$ancestor-getBinary() ),
+   __METHOD__
+   );
+
+   $res = true;
+   foreach ( $rows as $row ) {
+   $res = $dbw-insert(
+   $this-tableName,
+   array(
+   
'tree_descendant_id' = $descendant-getBinary(),
+   
'tree_ancestor_id' = $ancestor-getBinary(),
+   'tree_depth' = 
$row-tree_depth + 1,
+   ),
+   __METHOD__
+   );
+   }
+   }
+   }
}
+
if ( !$res ) {
$this-cache-delete( $parentKey );
$this-cache-delete( $pathKey );
@@ -163,7 +199,7 @@
if ( isset( 
$cacheResult[$cacheKeys[$descendant-getAlphadecimal()]] ) ) {