Browse Source

test WIP

tags/2.0.0-rc
Qiang Xue 10 years ago
parent
commit
65987f62ae
  1. 2
      docs/guide/db-dao.md
  2. 36
      framework/db/Connection.php
  3. 30
      tests/unit/framework/db/DatabaseTestCase.php

2
docs/guide/db-dao.md

@ -495,6 +495,8 @@ $rows = $db->useMaster(function ($db) {
});
```
You may also directly set `$db->enableSlaves` to be false to direct all queries to the master connection.
Working with database schema
----------------------------

36
framework/db/Connection.php

@ -296,12 +296,12 @@ class Connection extends Component
* @var boolean whether to enable read/write splitting by using [[slaves]] to read data.
* Note that if [[slaves]] is empty, read/write splitting will NOT be enabled no matter what value this property takes.
*/
public $enableSlave = true;
public $enableSlaves = true;
/**
* @var array list of slave connection configurations. Each configuration is used to create a slave DB connection.
* When [[enableSlave]] is true, one of these configurations will be chosen and used to create a DB connection
* When [[enableSlaves]] is true, one of these configurations will be chosen and used to create a DB connection
* for performing read queries only.
* @see enableSlave
* @see enableSlaves
* @see slaveConfig
*/
public $slaves = [];
@ -711,11 +711,12 @@ class Connection extends Component
}
/**
* Returns the PDO instance for read queries.
* When [[enableSlave]] is true, one of the slaves will be used for read queries, and its PDO instance
* will be returned by this method. If no slave is available, the [[writePdo]] will be returned.
* Returns the PDO instance for the currently active slave connection.
* When [[enableSlaves]] is true, one of the slaves will be used for read queries, and its PDO instance
* will be returned by this method.
* @param boolean $fallbackToMaster whether to return a master PDO in case none of the slave connections is available.
* @return PDO the PDO instance for read queries. Null is returned if no server is available.
* @return PDO the PDO instance for the currently active slave connection. Null is returned if no slave connection
* is available and `$fallbackToMaster` is false.
*/
public function getSlavePdo($fallbackToMaster = true)
{
@ -728,9 +729,9 @@ class Connection extends Component
}
/**
* Returns the PDO instance for write queries.
* Returns the PDO instance for the currently active master connection.
* This method will open the master DB connection and then return [[pdo]].
* @return PDO the PDO instance for write queries.
* @return PDO the PDO instance for the currently active master connection.
*/
public function getMasterPdo()
{
@ -740,13 +741,14 @@ class Connection extends Component
/**
* Returns the currently active slave connection.
* If this method is called the first time, it will try to open a slave connection when [[enableSlave]] is true.
* @param boolean $fallbackToMaster whether to return a master connection in case none of the slave connections is available.
* @return Connection the currently active slave. Null is returned if there is slave available.
* If this method is called the first time, it will try to open a slave connection when [[enableSlaves]] is true.
* @param boolean $fallbackToMaster whether to return a master connection in case there is no slave connection available.
* @return Connection the currently active slave connection. Null is returned if there is slave available and
* `$fallbackToMaster` is false.
*/
public function getSlave($fallbackToMaster = true)
{
if (!$this->enableSlave) {
if (!$this->enableSlaves) {
return $fallbackToMaster ? $this : null;
}
@ -775,15 +777,16 @@ class Connection extends Component
*/
public function useMaster(callable $callback)
{
$enableSlave = $this->enableSlave;
$this->enableSlave = false;
$enableSlave = $this->enableSlaves;
$this->enableSlaves = false;
$result = call_user_func($callback, $this);
$this->enableSlave = $enableSlave;
$this->enableSlaves = $enableSlave;
return $result;
}
/**
* Opens the connection to a server in the pool.
* This method implements the load balancing among the given list of the servers.
* @param array $pool the list of connection configurations in the server pool
* @param array $sharedConfig the configuration common to those given in `$pool`.
* @return Connection the opened DB connection, or null if no server is available
@ -824,6 +827,7 @@ class Connection extends Component
} catch (\Exception $e) {
Yii::warning("Connection ({$config['dsn']}) failed: " . $e->getMessage(), __METHOD__);
if ($cache instanceof Cache) {
// mark this server as dead and only retry it after the specified interval
$cache->set($key, 1, $this->serverRetryInterval);
}
}

30
tests/unit/framework/db/DatabaseTestCase.php

@ -44,26 +44,32 @@ abstract class DatabaseTestCase extends TestCase
if (!$reset && $this->db) {
return $this->db;
}
$db = new \yii\db\Connection;
$db->dsn = $this->database['dsn'];
if (isset($this->database['username'])) {
$db->username = $this->database['username'];
$db->password = $this->database['password'];
$config = $this->database;
if (isset($config['fixture'])) {
$fixture = $config['fixture'];
unset($config['fixture']);
} else {
$fixture = null;
}
if (isset($this->database['attributes'])) {
$db->attributes = $this->database['attributes'];
return $this->db = $this->prepareDatabase($config, $fixture);
}
public function prepareDatabase($config, $fixture)
{
if (!isset($config['class'])) {
$config['class'] = 'yii\db\Connection';
}
if ($open) {
$db->open();
$lines = explode(';', file_get_contents($this->database['fixture']));
/* @var $db \yii\db\Connection */
$db = \Yii::createObject($config);
$db->open();
if ($fixture !== null) {
$lines = explode(';', file_get_contents($fixture));
foreach ($lines as $line) {
if (trim($line) !== '') {
$db->pdo->exec($line);
}
}
}
$this->db = $db;
return $db;
}
}

Loading…
Cancel
Save