Fix DBA::lock() testability because of "autocommits"
This commit is contained in:
parent
10e8c5594d
commit
ea4b066df3
3 changed files with 45 additions and 25 deletions
|
@ -34,6 +34,8 @@ trait DatabaseTestTrait
|
||||||
StaticDatabase::statConnect($_SERVER);
|
StaticDatabase::statConnect($_SERVER);
|
||||||
// Rollbacks every DB usage (in case the test couldn't call tearDown)
|
// Rollbacks every DB usage (in case the test couldn't call tearDown)
|
||||||
StaticDatabase::statRollback();
|
StaticDatabase::statRollback();
|
||||||
|
// Rollback the first, outer transaction just 2 be sure
|
||||||
|
StaticDatabase::getGlobConnection()->rollBack();
|
||||||
// Start the first, outer transaction
|
// Start the first, outer transaction
|
||||||
StaticDatabase::getGlobConnection()->beginTransaction();
|
StaticDatabase::getGlobConnection()->beginTransaction();
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,7 +69,7 @@ class ExtendedPDO extends PDO
|
||||||
{
|
{
|
||||||
if($this->_transactionDepth <= 0 || !$this->hasSavepoint()) {
|
if($this->_transactionDepth <= 0 || !$this->hasSavepoint()) {
|
||||||
parent::beginTransaction();
|
parent::beginTransaction();
|
||||||
$this->_transactionDepth = $this->_transactionDepth < 0 ? 0 : $this->_transactionDepth;
|
$this->_transactionDepth = 0;
|
||||||
} else {
|
} else {
|
||||||
$this->exec("SAVEPOINT LEVEL{$this->_transactionDepth}");
|
$this->exec("SAVEPOINT LEVEL{$this->_transactionDepth}");
|
||||||
}
|
}
|
||||||
|
@ -84,14 +84,15 @@ class ExtendedPDO extends PDO
|
||||||
*/
|
*/
|
||||||
public function commit()
|
public function commit()
|
||||||
{
|
{
|
||||||
|
// We don't want to "really" commit something, so skip the most outer hierarchy
|
||||||
|
if ($this->_transactionDepth <= 1 && $this->hasSavepoint()) {
|
||||||
|
$this->_transactionDepth = $this->_transactionDepth <= 0 ? 0 : 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
$this->_transactionDepth--;
|
$this->_transactionDepth--;
|
||||||
|
|
||||||
if($this->_transactionDepth <= 0 || !$this->hasSavepoint()) {
|
$this->exec("RELEASE SAVEPOINT LEVEL{$this->_transactionDepth}");
|
||||||
parent::commit();
|
|
||||||
$this->_transactionDepth = $this->_transactionDepth < 0 ? 0 : $this->_transactionDepth;
|
|
||||||
} else {
|
|
||||||
$this->exec("RELEASE SAVEPOINT LEVEL{$this->_transactionDepth}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -102,14 +103,15 @@ class ExtendedPDO extends PDO
|
||||||
*/
|
*/
|
||||||
public function rollBack()
|
public function rollBack()
|
||||||
{
|
{
|
||||||
if ($this->_transactionDepth <= 0) {
|
|
||||||
throw new PDOException('Rollback error : There is no transaction started');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->_transactionDepth--;
|
$this->_transactionDepth--;
|
||||||
|
|
||||||
if($this->_transactionDepth == 0 || !$this->hasSavepoint()) {
|
if($this->_transactionDepth <= 0 || !$this->hasSavepoint()) {
|
||||||
parent::rollBack();
|
$this->_transactionDepth = 0;
|
||||||
|
try {
|
||||||
|
parent::rollBack();
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
// this shouldn't happen, but it does ...
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$this->exec("ROLLBACK TO SAVEPOINT LEVEL{$this->_transactionDepth}");
|
$this->exec("ROLLBACK TO SAVEPOINT LEVEL{$this->_transactionDepth}");
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,9 @@ class StaticDatabase extends Database
|
||||||
*/
|
*/
|
||||||
private static $staticConnection;
|
private static $staticConnection;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
private $_locked = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Override the behaviour of connect, due there is just one, static connection at all
|
* Override the behaviour of connect, due there is just one, static connection at all
|
||||||
*
|
*
|
||||||
|
@ -77,6 +80,31 @@ class StaticDatabase extends Database
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Mock for locking tables */
|
||||||
|
public function lock($table)
|
||||||
|
{
|
||||||
|
if ($this->_locked) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->in_transaction = true;
|
||||||
|
$this->_locked = true;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Mock for unlocking tables */
|
||||||
|
public function unlock()
|
||||||
|
{
|
||||||
|
// See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
|
||||||
|
$this->performCommit();
|
||||||
|
|
||||||
|
$this->in_transaction = false;
|
||||||
|
$this->_locked = false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Does a commit
|
* Does a commit
|
||||||
*
|
*
|
||||||
|
@ -163,18 +191,6 @@ class StaticDatabase extends Database
|
||||||
return self::$staticConnection;
|
return self::$staticConnection;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a global commit for every nested transaction of the static connection
|
|
||||||
*/
|
|
||||||
public static function statCommit()
|
|
||||||
{
|
|
||||||
if (isset(self::$staticConnection)) {
|
|
||||||
while (self::$staticConnection->getTransactionDepth() > 0) {
|
|
||||||
self::$staticConnection->commit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform a global rollback for every nested transaction of the static connection
|
* Perform a global rollback for every nested transaction of the static connection
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue