primary/replica in a definitely non-breaking way (#18106)

This commit is contained in:
Brandon Kelly
2020-06-16 01:33:27 -07:00
committed by GitHub
parent 4c416c7982
commit eb956e9c6b
27 changed files with 212 additions and 214 deletions

View File

@ -18,19 +18,19 @@ Yii Framework 2 Change Log
- Bug #18096: Fix InlineValidator with anonymous inline function not working well from EachValidator (trombipeti)
- Enh #18083: Add `Controller::$request` and `$response` (brandonkelly)
- Enh #18102: Use “primary”/“replica” terminology instead of “master”/“slave” (brandonkelly)
- Added `yii\db\Connection::$enableReplicas` and deprecated `$enableSlaves` via magic methods.
- Added `yii\db\Connection::$replicas` and deprecated `$slaves` via magic methods.
- Added `yii\db\Connection::$replicaConfig` and deprecated `$slaveConfig` via magic methods.
- Added `yii\db\Connection::$primaries` and deprecated `$masters` via magic methods.
- Added `yii\db\Connection::$primaryConfig` and deprecated `$masterConfig` via magic methods.
- Added `yii\db\Connection::$shufflePrimaries` and deprecated `$shuffleMasters` via magic methods.
- Added `yii\db\Connection::$enableReplicas` via magic methods and deprecated `$enableSlaves`.
- Added `yii\db\Connection::$replicas` via magic methods and deprecated `$slaves`.
- Added `yii\db\Connection::$replicaConfig` via magic methods and deprecated `$slaveConfig`.
- Added `yii\db\Connection::$primaries` via magic methods and deprecated `$masters`.
- Added `yii\db\Connection::$primaryConfig` via magic methods and deprecated `$masterConfig`.
- Added `yii\db\Connection::$shufflePrimaries` via magic methods and deprecated `$shuffleMasters`.
- Added `yii\db\Connection::getReplicaPdo()` and deprecated `getSlavePdo()`.
- Added `yii\db\Connection::getPrimaryPdo()` and deprecated `getMasterPdo()`.
- Added `yii\db\Connection::getReplica()` and deprecated `getSlave()`.
- Added `yii\db\Connection::getPrimary()` and deprecated `getMaster()`.
- Added `yii\db\Connection::usePrimary()` and deprecated `useMaster()`.
- Added `yii\validators\ExistValidator::$forcePrimaryDb` and deprecated `$forceMasterDb` via magic methods.
- Added `yii\validators\UniqueValidator::$forcePrimaryDb` and deprecated `$forceMasterDb` via magic methods.
- Added `yii\validators\ExistValidator::$forcePrimaryDb` via magic methods and deprecated `$forceMasterDb`.
- Added `yii\validators\UniqueValidator::$forcePrimaryDb` via magic methods and deprecated `$forceMasterDb`.
- Bug #18101: Fix behavior of OUTPUT INSERTED.* for SQL Server query: "insert default values"; correct MSSQL unit tests; turn off profiling echo message in migration test (darkdef)
- Bug #18105: Fix for old trigger in RBAC migration with/without prefixTable (darkdef)

View File

@ -252,9 +252,9 @@ class Command extends Component
$forRead = false;
}
if ($forRead || $forRead === null && $this->db->getSchema()->isReadQuery($sql)) {
$pdo = $this->db->getReplicaPdo();
$pdo = $this->db->getSlavePdo();
} else {
$pdo = $this->db->getPrimaryPdo();
$pdo = $this->db->getMasterPdo();
}
try {

View File

@ -114,6 +114,43 @@ use yii\caching\CacheInterface;
* @property bool $isActive Whether the DB connection is established. This property is read-only.
* @property string $lastInsertID The row ID of the last row inserted, or the last value retrieved from the
* sequence object. This property is read-only.
* @property bool $enableReplicas whether to enable read/write splitting by using [[replicas]] to read data.
* Note that if [[replicas]] is empty, read/write splitting will NOT be enabled no matter what value this property takes.
* @property array $replicas list of replica connection configurations. Each configuration is used to create a replica DB connection.
* When [[enableReplicas]] is true, one of these configurations will be chosen and used to create a DB connection
* for performing read queries only.
* @property array $replicaConfig the configuration that should be merged with every replica configuration listed in [[replicas]].
* For example,
*
* ```php
* [
* 'username' => 'replica',
* 'password' => 'replica',
* 'attributes' => [
* // use a smaller connection timeout
* PDO::ATTR_TIMEOUT => 10,
* ],
* ]
* ```
* @property array $primaries list of primary connection configurations. Each configuration is used to create a primary DB connection.
* When [[open()]] is called, one of these configurations will be chosen and used to create a DB connection
* which will be used by this object.
* Note that when this property is not empty, the connection setting (e.g. `dsn`, `username`) of this object will
* be ignored.
* @property array $primaryConfig the configuration that should be merged with every primary configuration listed in [[primaries]].
* For example,
*
* ```php
* [
* 'username' => 'primary',
* 'password' => 'primary',
* 'attributes' => [
* // use a smaller connection timeout
* PDO::ATTR_TIMEOUT => 10,
* ],
* ]
* ```
* @property bool $shufflePrimaries whether to shuffle [[primaries]] before getting one.
* @property-read Connection|null $primary The currently active primary connection. `null` is returned if no primary
* connection is available. This property is read-only.
* @property-read PDO $primaryPdo The PDO instance for the currently active primary connection. This property is
@ -128,7 +165,6 @@ use yii\caching\CacheInterface;
* is read-only.
* @property Transaction|null $transaction The currently active transaction. Null if no active transaction.
* This property is read-only.
* @mixin ConnectionDeprecationsTrait
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
@ -344,53 +380,57 @@ class Connection extends Component
/**
* @var bool whether to enable read/write splitting by using [[replicas]] to read data.
* Note that if [[replicas]] is empty, read/write splitting will NOT be enabled no matter what value this property takes.
* @since 2.0.36
* @deprecated since 2.0.36. Use [[enableReplicas]] instead.
*/
public $enableReplicas = true;
public $enableSlaves = true;
/**
* Returns the value of [[enableReplicas]].
* @return bool
* @deprecated since 2.0.36. Use [[enableReplicas]] instead.
* @since 2.0.36
* @internal
*/
public function getEnableSlaves()
public function getEnableReplicas()
{
return $this->enableReplicas;
return $this->enableSlaves;
}
/**
* Sets the value of [[enableReplicas]].
* @param bool $value
* @deprecated since 2.0.36. Use [[enableReplicas]] instead.
* @since 2.0.36
* @internal
*/
public function setEnableSlaves($value)
public function setEnableReplicas($value)
{
$this->enableReplicas = $value;
$this->enableSlaves = $value;
}
/**
* @var array list of replica connection configurations. Each configuration is used to create a replica DB connection.
* When [[enableReplicas]] is true, one of these configurations will be chosen and used to create a DB connection
* for performing read queries only.
* @see enableReplicas
* @see replicaConfig
* @since 2.0.36
* @see enableSlaves
* @see slaveConfig
* @deprecated since 2.0.36. Use [[replicas]] instead.
*/
public $replicas = [];
public $slaves = [];
/**
* Returns the value of [[replicas]].
* @return array
* @deprecated since 2.0.36. Use [[replicas]] instead.
* @since 2.0.36
* @internal
*/
public function getSlaves()
public function getReplicas()
{
return $this->replicas;
return $this->slaves;
}
/**
* Sets the value of [[replicas]].
* @param array $value
* @deprecated since 2.0.36. Use [[replicas]] instead.
* @since 2.0.36
* @internal
*/
public function setSlaves($value)
public function setReplicas($value)
{
$this->replicas = $value;
$this->slaves = $value;
}
/**
* @var array the configuration that should be merged with every replica configuration listed in [[replicas]].
@ -407,26 +447,28 @@ class Connection extends Component
* ]
* ```
*
* @since 2.0.36
* @deprecated since 2.0.36. Use [[replicaConfig]] instead.
*/
public $replicaConfig = [];
public $slaveConfig = [];
/**
* Returns the value of [[replicaConfig]].
* @return array
* @deprecated since 2.0.36. Use [[replicaConfig]] instead.
* @since 2.0.36
* @internal
*/
public function getSlaveConfig()
public function getReplicaConfig()
{
return $this->replicaConfig;
return $this->slaveConfig;
}
/**
* Sets the value of [[replicaConfig]].
* @param array $value
* @deprecated since 2.0.36. Use [[replicaConfig]] instead.
* @since 2.0.36
* @internal
*/
public function setSlaveConfig($value)
public function setReplicaConfig($value)
{
$this->replicaConfig = $value;
$this->slaveConfig = $value;
}
/**
* @var array list of primary connection configurations. Each configuration is used to create a primary DB connection.
@ -434,28 +476,30 @@ class Connection extends Component
* which will be used by this object.
* Note that when this property is not empty, the connection setting (e.g. `dsn`, `username`) of this object will
* be ignored.
* @see primaryConfig
* @see shufflePrimaries
* @since 2.0.36
* @see masterConfig
* @see shuffleMasters
* @deprecated since 2.0.36. Use [[primaries]] instead.
*/
public $primaries = [];
public $masters = [];
/**
* Returns the value of [[primaries]].
* @return array
* @deprecated since 2.0.36. Use [[primaries]] instead.
* @since 2.0.36
* @internal
*/
public function getMasters()
public function getPrimaries()
{
return $this->primaries;
return $this->masters;
}
/**
* Sets the value of [[primaries]].
* @param array $value
* @deprecated since 2.0.36. Use [[primaries]] instead.
* @since 2.0.36
* @internal
*/
public function setMasters($value)
public function setPrimaries($value)
{
$this->primaries = $value;
$this->masters = $value;
}
/**
* @var array the configuration that should be merged with every primary configuration listed in [[primaries]].
@ -472,51 +516,55 @@ class Connection extends Component
* ]
* ```
*
* @since 2.0.36
* @deprecated since 2.0.36. Use [[primaryConfig]] instead.
*/
public $primaryConfig = [];
public $masterConfig = [];
/**
* Returns the value of [[primaryConfig]].
* @return array
* @deprecated since 2.0.36. Use [[primaryConfig]] instead.
* @since 2.0.36
* @internal
*/
public function getMasterConfig()
public function getPrimaryConfig()
{
return $this->primaryConfig;
return $this->masterConfig;
}
/**
* Sets the value of [[primaryConfig]].
* @param array $value
* @deprecated since 2.0.36. Use [[primaryConfig]] instead.
* @since 2.0.36
* @internal
*/
public function setMasterConfig($value)
public function setPrimaryConfig($value)
{
$this->primaryConfig = $value;
$this->masterConfig = $value;
}
/**
* @var bool whether to shuffle [[primaries]] before getting one.
* @since 2.0.11
* @see primaries
* @since 2.0.36
* @see masters
* @deprecated since 2.0.36. Use [[shufflePrimaries]] instead.
*/
public $shufflePrimaries = true;
public $shuffleMasters = true;
/**
* Returns the value of [[shufflePrimaries]].
* @return bool
* @deprecated since 2.0.36. Use [[shufflePrimaries]] instead.
* @since 2.0.36
* @internal
*/
public function getShuffleMasters()
public function getShufflePrimaries()
{
return $this->shufflePrimaries;
return $this->shuffleMasters;
}
/**
* Sets the value of [[shufflePrimaries]].
* @param bool $value
* @deprecated since 2.0.36. Use [[shufflePrimaries]] instead.
* @since 2.0.36
* @internal
*/
public function setShuffleMasters($value)
public function setShufflePrimaries($value)
{
$this->shufflePrimaries = $value;
$this->shuffleMasters = $value;
}
/**
* @var bool whether to enable logging of database queries. Defaults to true.
@ -714,8 +762,8 @@ class Connection extends Component
return;
}
if (!empty($this->primaries)) {
$db = $this->getPrimary();
if (!empty($this->masters)) {
$db = $this->getMaster();
if ($db !== null) {
$this->pdo = $db->pdo;
return;
@ -1087,7 +1135,7 @@ class Connection extends Component
if (($pos = strpos($this->dsn, ':')) !== false) {
$this->_driverName = strtolower(substr($this->dsn, 0, $pos));
} else {
$this->_driverName = strtolower($this->getReplicaPdo()->getAttribute(PDO::ATTR_DRIVER_NAME));
$this->_driverName = strtolower($this->getSlavePdo()->getAttribute(PDO::ATTR_DRIVER_NAME));
}
}
@ -1124,9 +1172,9 @@ class Connection extends Component
*/
public function getReplicaPdo($fallbackToPrimary = true)
{
$db = $this->getReplica(false);
$db = $this->getSlave(false);
if ($db === null) {
return $fallbackToPrimary ? $this->getPrimaryPdo() : null;
return $fallbackToPrimary ? $this->getMasterPdo() : null;
}
return $db->pdo;
@ -1181,12 +1229,12 @@ class Connection extends Component
*/
public function getReplica($fallbackToPrimary = true)
{
if (!$this->enableReplicas) {
if (!$this->enableSlaves) {
return $fallbackToPrimary ? $this : null;
}
if ($this->_replica === false) {
$this->_replica = $this->openFromPool($this->replicas, $this->replicaConfig);
$this->_replica = $this->openFromPool($this->slaves, $this->slaveConfig);
}
return $this->_replica === null && $fallbackToPrimary ? $this : $this->_replica;
@ -1218,9 +1266,9 @@ class Connection extends Component
public function getPrimary()
{
if ($this->_primary === false) {
$this->_primary = $this->shufflePrimaries
? $this->openFromPool($this->primaries, $this->primaryConfig)
: $this->openFromPoolSequentially($this->primaries, $this->primaryConfig);
$this->_primary = $this->shuffleMasters
? $this->openFromPool($this->masters, $this->masterConfig)
: $this->openFromPoolSequentially($this->masters, $this->masterConfig);
}
return $this->_primary;
@ -1259,19 +1307,19 @@ class Connection extends Component
*/
public function usePrimary(callable $callback)
{
if ($this->enableReplicas) {
$this->enableReplicas = false;
if ($this->enableSlaves) {
$this->enableSlaves = false;
try {
$result = call_user_func($callback, $this);
} catch (\Exception $e) {
$this->enableReplicas = true;
$this->enableSlaves = true;
throw $e;
} catch (\Throwable $e) {
$this->enableReplicas = true;
$this->enableSlaves = true;
throw $e;
}
// TODO: use "finally" keyword when miminum required PHP version is >= 5.5
$this->enableReplicas = true;
$this->enableSlaves = true;
} else {
$result = call_user_func($callback, $this);
}

View File

@ -18,38 +18,6 @@ use PDO;
*/
trait ConnectionDeprecationsTrait
{
/**
* @var bool whether to enable read/write splitting by using [[Connection::$replicas]] to read data.
* @deprecated since 2.0.36. Use [[Connection::$enableReplicas]] instead.
*/
public $enableSlaves;
/**
* @var array list of replica connection configurations. Each configuration is used to create a replica DB connection.
* @deprecated since 2.0.36. Use [[Connection::$replicas]] instead.
*/
public $slaves;
/**
* @var array the configuration that should be merged with every replica configuration listed in
* [[Connection::$replicas]].
* @deprecated since 2.0.36. Use [[Connection::$replicaConfig]] instead.
*/
public $slaveConfig;
/**
* @var array list of primary connection configurations. Each configuration is used to create a primary DB connection.
* @deprecated since 2.0.36. Use [[Connection::$primaries]] instead.
*/
public $masters;
/**
* @var array the configuration that should be merged with every primary configuration listed in
* [[Connection::$primaries]].
* @deprecated since 2.0.36. Use [[Connection::$primaryConfig]] instead.
*/
public $masterConfig;
/**
* @var bool whether to shuffle [[Connection::$primaries]] before getting one.
* @deprecated since 2.0.36. Use [[Connection::$shufflePrimaries]] instead.
*/
public $shuffleMasters;
/**
* @var Connection|null The currently active primary connection. `null` is returned if no primary connection is
* available. This property is read-only.

View File

@ -89,7 +89,7 @@ class Migration extends Component implements MigrationInterface
parent::init();
$this->db = Instance::ensure($this->db, Connection::className());
$this->db->getSchema()->refresh();
$this->db->enableReplicas = false;
$this->db->enableSlaves = false;
}
/**

View File

@ -459,7 +459,7 @@ abstract class Schema extends BaseObject
return $str;
}
if (($value = $this->db->getReplicaPdo()->quote($str)) !== false) {
if (($value = $this->db->getSlavePdo()->quote($str)) !== false) {
return $value;
}
@ -696,7 +696,7 @@ abstract class Schema extends BaseObject
public function getServerVersion()
{
if ($this->_serverVersion === null) {
$this->_serverVersion = $this->db->getReplicaPdo()->getAttribute(\PDO::ATTR_SERVER_VERSION);
$this->_serverVersion = $this->db->getSlavePdo()->getAttribute(\PDO::ATTR_SERVER_VERSION);
}
return $this->_serverVersion;
}
@ -810,7 +810,7 @@ abstract class Schema extends BaseObject
*/
protected function normalizePdoRowKeyCase(array $row, $multiple)
{
if ($this->db->getReplicaPdo()->getAttribute(\PDO::ATTR_CASE) !== \PDO::CASE_UPPER) {
if ($this->db->getSlavePdo()->getAttribute(\PDO::ATTR_CASE) !== \PDO::CASE_UPPER) {
return $row;
}

View File

@ -91,7 +91,7 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface
*/
protected function findTableNames($schema = '')
{
$pdo = $this->db->getReplicaPdo();
$pdo = $this->db->getSlavePdo();
$tables = $pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE);
$tableNames = [];
foreach ($tables as $table) {
@ -109,7 +109,7 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface
*/
protected function loadTableSchema($name)
{
$pdo = $this->db->getReplicaPdo();
$pdo = $this->db->getSlavePdo();
$tableInfo = $pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE, $name);
@ -158,7 +158,7 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface
*/
protected function loadTablePrimaryKey($tableName)
{
$primaryKey = $this->db->getReplicaPdo()->cubrid_schema(\PDO::CUBRID_SCH_PRIMARY_KEY, $tableName);
$primaryKey = $this->db->getSlavePdo()->cubrid_schema(\PDO::CUBRID_SCH_PRIMARY_KEY, $tableName);
if (empty($primaryKey)) {
return null;
}
@ -182,7 +182,7 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface
3 => 'SET NULL',
];
$foreignKeys = $this->db->getReplicaPdo()->cubrid_schema(\PDO::CUBRID_SCH_IMPORTED_KEYS, $tableName);
$foreignKeys = $this->db->getSlavePdo()->cubrid_schema(\PDO::CUBRID_SCH_IMPORTED_KEYS, $tableName);
$foreignKeys = ArrayHelper::index($foreignKeys, null, 'FK_NAME');
ArrayHelper::multisort($foreignKeys, 'KEY_SEQ', SORT_ASC, SORT_NUMERIC);
$result = [];
@ -385,7 +385,7 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface
*/
private function loadTableConstraints($tableName, $returnType)
{
$constraints = $this->db->getReplicaPdo()->cubrid_schema(\PDO::CUBRID_SCH_CONSTRAINT, $tableName);
$constraints = $this->db->getSlavePdo()->cubrid_schema(\PDO::CUBRID_SCH_CONSTRAINT, $tableName);
$constraints = ArrayHelper::index($constraints, null, ['TYPE', 'NAME']);
ArrayHelper::multisort($constraints, 'KEY_ORDER', SORT_ASC, SORT_NUMERIC);
$result = [

View File

@ -403,7 +403,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
}
$version = $cache ? $cache->get($key) : null;
if (!$version) {
$version = $this->db->getReplicaPdo()->getAttribute(\PDO::ATTR_SERVER_VERSION);
$version = $this->db->getSlavePdo()->getAttribute(\PDO::ATTR_SERVER_VERSION);
if ($cache) {
$cache->set($key, $version, $this->db->schemaCacheDuration);
}

View File

@ -475,7 +475,7 @@ SQL;
protected function isOldMysql()
{
if ($this->_oldMysql === null) {
$version = $this->db->getReplicaPdo()->getAttribute(\PDO::ATTR_SERVER_VERSION);
$version = $this->db->getSlavePdo()->getAttribute(\PDO::ATTR_SERVER_VERSION);
$this->_oldMysql = version_compare($version, '5.1', '<=');
}

View File

@ -155,7 +155,7 @@ EOD;
throw new InvalidArgumentException("Can't reset sequence for composite primary key in table: $table");
}
// use primary connection to get the biggest PK value
$value = $this->db->usePrimary(function (Connection $db) use ($tableSchema) {
$value = $this->db->useMaster(function (Connection $db) use ($tableSchema) {
return $db->createCommand(
'SELECT MAX("' . $tableSchema->primaryKey[0] . '") FROM "'. $tableSchema->name . '"'
)->queryScalar();

View File

@ -57,7 +57,7 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface
if ($this->defaultSchema === null) {
$username = $this->db->username;
if (empty($username)) {
$username = isset($this->db->primaries[0]['username']) ? $this->db->primaries[0]['username'] : '';
$username = isset($this->db->masters[0]['username']) ? $this->db->masters[0]['username'] : '';
}
$this->defaultSchema = strtoupper($username);
}
@ -384,7 +384,7 @@ SQL;
if ($this->db->isActive) {
// get the last insert id from the primary connection
$sequenceName = $this->quoteSimpleTableName($sequenceName);
return $this->db->usePrimary(function (Connection $db) use ($sequenceName) {
return $this->db->useMaster(function (Connection $db) use ($sequenceName) {
return $db->createCommand("SELECT {$sequenceName}.CURRVAL FROM DUAL")->queryScalar();
});
} else {

View File

@ -221,7 +221,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
}
// enable to have ability to alter several tables
$this->db->getPrimaryPdo()->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
$this->db->getMasterPdo()->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
return $command;
}

View File

@ -206,7 +206,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
$tableName = $db->quoteTableName($tableName);
if ($value === null) {
$key = $this->db->quoteColumnName(reset($table->primaryKey));
$value = $this->db->usePrimary(function (Connection $db) use ($key, $tableName) {
$value = $this->db->useMaster(function (Connection $db) use ($key, $tableName) {
return $db->createCommand("SELECT MAX($key) FROM $tableName")->queryScalar();
});
} else {

View File

@ -56,7 +56,7 @@ class MysqlMutex extends DbMutex
*/
protected function acquireLock($name, $timeout = 0)
{
return $this->db->usePrimary(function ($db) use ($name, $timeout) {
return $this->db->useMaster(function ($db) use ($name, $timeout) {
/** @var \yii\db\Connection $db */
return (bool) $db->createCommand(
'SELECT GET_LOCK(:name, :timeout)',
@ -73,7 +73,7 @@ class MysqlMutex extends DbMutex
*/
protected function releaseLock($name)
{
return $this->db->usePrimary(function ($db) use ($name) {
return $this->db->useMaster(function ($db) use ($name) {
/** @var \yii\db\Connection $db */
return (bool) $db->createCommand(
'SELECT RELEASE_LOCK(:name)',

View File

@ -88,7 +88,7 @@ class OracleMutex extends DbMutex
$timeout = abs((int) $timeout);
// inside pl/sql scopes pdo binding not working correctly :(
$this->db->usePrimary(function ($db) use ($name, $timeout, $releaseOnCommit, &$lockStatus) {
$this->db->useMaster(function ($db) use ($name, $timeout, $releaseOnCommit, &$lockStatus) {
/** @var \yii\db\Connection $db */
$db->createCommand(
'DECLARE
@ -115,7 +115,7 @@ END;',
protected function releaseLock($name)
{
$releaseStatus = null;
$this->db->usePrimary(function ($db) use ($name, &$releaseStatus) {
$this->db->useMaster(function ($db) use ($name, &$releaseStatus) {
/** @var \yii\db\Connection $db */
$db->createCommand(
'DECLARE

View File

@ -73,7 +73,7 @@ class PgsqlMutex extends DbMutex
list($key1, $key2) = $this->getKeysFromName($name);
return $this->retryAcquire($timeout, function () use ($key1, $key2) {
return $this->db->usePrimary(function ($db) use ($key1, $key2) {
return $this->db->useMaster(function ($db) use ($key1, $key2) {
/** @var \yii\db\Connection $db */
return (bool) $db->createCommand(
'SELECT pg_try_advisory_lock(:key1, :key2)',
@ -92,7 +92,7 @@ class PgsqlMutex extends DbMutex
protected function releaseLock($name)
{
list($key1, $key2) = $this->getKeysFromName($name);
return $this->db->usePrimary(function ($db) use ($key1, $key2) {
return $this->db->useMaster(function ($db) use ($key1, $key2) {
/** @var \yii\db\Connection $db */
return (bool) $db->createCommand(
'SELECT pg_advisory_unlock(:key1, :key2)',

View File

@ -44,7 +44,7 @@ use yii\db\QueryInterface;
* ['type_id', 'exist', 'targetRelation' => 'type'],
* ```
*
* @mixin ForceMasterDbTrait
* @property bool $forcePrimaryDb whether this validator is forced to always use the primary DB connection
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
@ -89,26 +89,29 @@ class ExistValidator extends Validator
public $targetAttributeJunction = 'and';
/**
* @var bool whether this validator is forced to always use the primary DB connection
* @since 2.0.36
* @since 2.0.14
* @deprecated since 2.0.36. Use [[forcePrimaryDb]] instead.
*/
public $forcePrimaryDb = true;
public $forceMasterDb = true;
/**
* Returns the value of [[forcePrimaryDb]].
* @return bool
* @deprecated since 2.0.36. Use [[forcePrimaryDb]] instead.
* @since 2.0.36
* @internal
*/
public function getForceMasterDb()
public function getForcePrimaryDb()
{
return $this->forcePrimaryDb;
return $this->forceMasterDb;
}
/**
* Sets the value of [[forcePrimaryDb]].
* @param bool $value
* @deprecated since 2.0.36. Use [[forcePrimaryDb]] instead.
* @since 2.0.36
* @internal
*/
public function setForceMasterDb($value)
public function setForcePrimaryDb($value)
{
$this->forcePrimaryDb = $value;
$this->forceMasterDb = $value;
}
@ -152,8 +155,8 @@ class ExistValidator extends Validator
$relationQuery->andWhere($this->filter);
}
if ($this->forcePrimaryDb && method_exists($model::getDb(), 'usePrimary')) {
$model::getDb()->usePrimary(function() use ($relationQuery, &$exists) {
if ($this->forceMasterDb && method_exists($model::getDb(), 'useMaster')) {
$model::getDb()->useMaster(function() use ($relationQuery, &$exists) {
$exists = $relationQuery->exists();
});
} else {
@ -280,8 +283,8 @@ class ExistValidator extends Validator
$db = $targetClass::getDb();
$exists = false;
if ($this->forcePrimaryDb && method_exists($db, 'usePrimary')) {
$db->usePrimary(function ($db) use ($query, $value, &$exists) {
if ($this->forceMasterDb && method_exists($db, 'useMaster')) {
$db->useMaster(function ($db) use ($query, $value, &$exists) {
$exists = $this->queryValueExists($query, $value);
});
} else {

View File

@ -1,24 +0,0 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\validators;
/**
* @internal This trait is only used to denote deprecated magic properties of [[ExistValidator]] for IDEs via a
* `@mixin` tag. It is never actually loaded at runtime.
*
* @author Brandon Kelly <brandon@craftcms.com>
* @since 2.0.36
*/
trait ForceMasterDbTrait
{
/**
* @var bool whether this validator is forced to always use primary DB connection
* @deprecated since 2.0.36. Use [[ExistValidator::$forcePrimaryDb]] instead.
*/
public $forceMasterDb = true;
}

View File

@ -37,7 +37,7 @@ use yii\helpers\Inflector;
* ['a1', 'unique', 'targetAttribute' => ['a2', 'a1' => 'a3']]
* ```
*
* @mixin ForceMasterDbTrait
* @property bool $forcePrimaryDb whether this validator is forced to always use the primary DB connection
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
@ -94,26 +94,29 @@ class UniqueValidator extends Validator
public $targetAttributeJunction = 'and';
/**
* @var bool whether this validator is forced to always use the primary DB connection
* @since 2.0.26
* @since 2.0.14
* @deprecated since 2.0.36. Use [[forcePrimaryDb]] instead.
*/
public $forcePrimaryDb = true;
public $forceMasterDb = true;
/**
* Returns the value of [[forcePrimaryDb]].
* @return bool
* @deprecated since 2.0.36. Use [[forcePrimaryDb]] instead.
* @since 2.0.36
* @internal
*/
public function getForceMasterDb()
public function getForcePrimaryDb()
{
return $this->forcePrimaryDb;
return $this->forceMasterDb;
}
/**
* Sets the value of [[forcePrimaryDb]].
* @param bool $value
* @deprecated since 2.0.36. Use [[forcePrimaryDb]] instead.
* @since 2.0.36
* @internal
*/
public function setForceMasterDb($value)
public function setForcePrimaryDb($value)
{
$this->forcePrimaryDb = $value;
$this->forceMasterDb = $value;
}
@ -162,8 +165,8 @@ class UniqueValidator extends Validator
$modelExists = false;
if ($this->forcePrimaryDb && method_exists($db, 'usePrimary')) {
$db->usePrimary(function () use ($targetClass, $conditions, $model, &$modelExists) {
if ($this->forceMasterDb && method_exists($db, 'useMaster')) {
$db->useMaster(function () use ($targetClass, $conditions, $model, &$modelExists) {
$modelExists = $this->modelExists($targetClass, $conditions, $model);
});
} else {

View File

@ -114,7 +114,7 @@ class DbSession extends MultiFieldSession
return;
}
$row = $this->db->usePrimary(function() use ($oldID) {
$row = $this->db->useMaster(function() use ($oldID) {
return (new Query())->from($this->sessionTable)
->where(['id' => $oldID])
->createCommand($this->db)