Commit 65987f62 by Qiang Xue

test WIP

parent c64ffdb2
...@@ -495,6 +495,8 @@ $rows = $db->useMaster(function ($db) { ...@@ -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 Working with database schema
---------------------------- ----------------------------
......
...@@ -296,12 +296,12 @@ class Connection extends Component ...@@ -296,12 +296,12 @@ class Connection extends Component
* @var boolean whether to enable read/write splitting by using [[slaves]] to read data. * @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. * 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. * @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. * for performing read queries only.
* @see enableSlave * @see enableSlaves
* @see slaveConfig * @see slaveConfig
*/ */
public $slaves = []; public $slaves = [];
...@@ -711,11 +711,12 @@ class Connection extends Component ...@@ -711,11 +711,12 @@ class Connection extends Component
} }
/** /**
* Returns the PDO instance for read queries. * Returns the PDO instance for the currently active slave connection.
* When [[enableSlave]] is true, one of the slaves will be used for read queries, and its PDO instance * When [[enableSlaves]] 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. * will be returned by this method.
* @param boolean $fallbackToMaster whether to return a master PDO in case none of the slave connections is available. * @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) public function getSlavePdo($fallbackToMaster = true)
{ {
...@@ -728,9 +729,9 @@ class Connection extends Component ...@@ -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]]. * 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() public function getMasterPdo()
{ {
...@@ -740,13 +741,14 @@ class Connection extends Component ...@@ -740,13 +741,14 @@ class Connection extends Component
/** /**
* Returns the currently active slave connection. * 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. * 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 none of the slave connections is available. * @param boolean $fallbackToMaster whether to return a master connection in case there is no slave connection available.
* @return Connection the currently active slave. Null is returned if there is slave 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) public function getSlave($fallbackToMaster = true)
{ {
if (!$this->enableSlave) { if (!$this->enableSlaves) {
return $fallbackToMaster ? $this : null; return $fallbackToMaster ? $this : null;
} }
...@@ -775,15 +777,16 @@ class Connection extends Component ...@@ -775,15 +777,16 @@ class Connection extends Component
*/ */
public function useMaster(callable $callback) public function useMaster(callable $callback)
{ {
$enableSlave = $this->enableSlave; $enableSlave = $this->enableSlaves;
$this->enableSlave = false; $this->enableSlaves = false;
$result = call_user_func($callback, $this); $result = call_user_func($callback, $this);
$this->enableSlave = $enableSlave; $this->enableSlaves = $enableSlave;
return $result; return $result;
} }
/** /**
* Opens the connection to a server in the pool. * 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 $pool the list of connection configurations in the server pool
* @param array $sharedConfig the configuration common to those given in `$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 * @return Connection the opened DB connection, or null if no server is available
...@@ -824,6 +827,7 @@ class Connection extends Component ...@@ -824,6 +827,7 @@ class Connection extends Component
} catch (\Exception $e) { } catch (\Exception $e) {
Yii::warning("Connection ({$config['dsn']}) failed: " . $e->getMessage(), __METHOD__); Yii::warning("Connection ({$config['dsn']}) failed: " . $e->getMessage(), __METHOD__);
if ($cache instanceof Cache) { if ($cache instanceof Cache) {
// mark this server as dead and only retry it after the specified interval
$cache->set($key, 1, $this->serverRetryInterval); $cache->set($key, 1, $this->serverRetryInterval);
} }
} }
......
...@@ -44,26 +44,32 @@ abstract class DatabaseTestCase extends TestCase ...@@ -44,26 +44,32 @@ abstract class DatabaseTestCase extends TestCase
if (!$reset && $this->db) { if (!$reset && $this->db) {
return $this->db; return $this->db;
} }
$db = new \yii\db\Connection; $config = $this->database;
$db->dsn = $this->database['dsn']; if (isset($config['fixture'])) {
if (isset($this->database['username'])) { $fixture = $config['fixture'];
$db->username = $this->database['username']; unset($config['fixture']);
$db->password = $this->database['password']; } else {
$fixture = null;
} }
if (isset($this->database['attributes'])) { return $this->db = $this->prepareDatabase($config, $fixture);
$db->attributes = $this->database['attributes']; }
public function prepareDatabase($config, $fixture)
{
if (!isset($config['class'])) {
$config['class'] = 'yii\db\Connection';
} }
if ($open) { /* @var $db \yii\db\Connection */
$db->open(); $db = \Yii::createObject($config);
$lines = explode(';', file_get_contents($this->database['fixture'])); $db->open();
if ($fixture !== null) {
$lines = explode(';', file_get_contents($fixture));
foreach ($lines as $line) { foreach ($lines as $line) {
if (trim($line) !== '') { if (trim($line) !== '') {
$db->pdo->exec($line); $db->pdo->exec($line);
} }
} }
} }
$this->db = $db;
return $db; return $db;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment