composer update
This commit is contained in:
@@ -1,31 +1,15 @@
|
||||
<?php
|
||||
/*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the MIT license. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\DBAL\Sharding\SQLAzure;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Closure;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\DBAL\Schema\Synchronizer\AbstractSchemaSynchronizer;
|
||||
use Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer;
|
||||
use Doctrine\DBAL\Schema\Synchronizer\SchemaSynchronizer;
|
||||
use Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use RuntimeException;
|
||||
use function array_merge;
|
||||
|
||||
/**
|
||||
@@ -35,30 +19,19 @@ use function array_merge;
|
||||
* by partitioning the passed schema into subschemas for the federation and the
|
||||
* global database and then applying the operations step by step using the
|
||||
* {@see \Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer}.
|
||||
*
|
||||
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
||||
*/
|
||||
class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer
|
||||
{
|
||||
const FEDERATION_TABLE_FEDERATED = 'azure.federated';
|
||||
const FEDERATION_DISTRIBUTION_NAME = 'azure.federatedOnDistributionName';
|
||||
public const FEDERATION_TABLE_FEDERATED = 'azure.federated';
|
||||
public const FEDERATION_DISTRIBUTION_NAME = 'azure.federatedOnDistributionName';
|
||||
|
||||
/**
|
||||
* @var \Doctrine\DBAL\Sharding\SQLAzure\SQLAzureShardManager
|
||||
*/
|
||||
/** @var SQLAzureShardManager */
|
||||
private $shardManager;
|
||||
|
||||
/**
|
||||
* @var \Doctrine\DBAL\Schema\Synchronizer\SchemaSynchronizer
|
||||
*/
|
||||
/** @var SchemaSynchronizer */
|
||||
private $synchronizer;
|
||||
|
||||
/**
|
||||
* @param \Doctrine\DBAL\Connection $conn
|
||||
* @param \Doctrine\DBAL\Sharding\SQLAzure\SQLAzureShardManager $shardManager
|
||||
* @param \Doctrine\DBAL\Schema\Synchronizer\SchemaSynchronizer|null $sync
|
||||
*/
|
||||
public function __construct(Connection $conn, SQLAzureShardManager $shardManager, SchemaSynchronizer $sync = null)
|
||||
public function __construct(Connection $conn, SQLAzureShardManager $shardManager, ?SchemaSynchronizer $sync = null)
|
||||
{
|
||||
parent::__construct($conn);
|
||||
$this->shardManager = $shardManager;
|
||||
@@ -72,13 +45,13 @@ class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer
|
||||
{
|
||||
$sql = [];
|
||||
|
||||
list($global, $federation) = $this->partitionSchema($createSchema);
|
||||
[$global, $federation] = $this->partitionSchema($createSchema);
|
||||
|
||||
$globalSql = $this->synchronizer->getCreateSchema($global);
|
||||
if ($globalSql) {
|
||||
$sql[] = "-- Create Root Federation\n" .
|
||||
"USE FEDERATION ROOT WITH RESET;";
|
||||
$sql = array_merge($sql, $globalSql);
|
||||
'USE FEDERATION ROOT WITH RESET;';
|
||||
$sql = array_merge($sql, $globalSql);
|
||||
}
|
||||
|
||||
$federationSql = $this->synchronizer->getCreateSchema($federation);
|
||||
@@ -87,8 +60,8 @@ class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer
|
||||
$defaultValue = $this->getFederationTypeDefaultValue();
|
||||
|
||||
$sql[] = $this->getCreateFederationStatement();
|
||||
$sql[] = "USE FEDERATION " . $this->shardManager->getFederationName() . " (" . $this->shardManager->getDistributionKey() . " = " . $defaultValue . ") WITH RESET, FILTERING = OFF;";
|
||||
$sql = array_merge($sql, $federationSql);
|
||||
$sql[] = 'USE FEDERATION ' . $this->shardManager->getFederationName() . ' (' . $this->shardManager->getDistributionKey() . ' = ' . $defaultValue . ') WITH RESET, FILTERING = OFF;';
|
||||
$sql = array_merge($sql, $federationSql);
|
||||
}
|
||||
|
||||
return $sql;
|
||||
@@ -99,7 +72,7 @@ class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer
|
||||
*/
|
||||
public function getUpdateSchema(Schema $toSchema, $noDrops = false)
|
||||
{
|
||||
return $this->work($toSchema, function ($synchronizer, $schema) use ($noDrops) {
|
||||
return $this->work($toSchema, static function ($synchronizer, $schema) use ($noDrops) {
|
||||
return $synchronizer->getUpdateSchema($schema, $noDrops);
|
||||
});
|
||||
}
|
||||
@@ -109,7 +82,7 @@ class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer
|
||||
*/
|
||||
public function getDropSchema(Schema $dropSchema)
|
||||
{
|
||||
return $this->work($dropSchema, function ($synchronizer, $schema) {
|
||||
return $this->work($dropSchema, static function ($synchronizer, $schema) {
|
||||
return $synchronizer->getDropSchema($schema);
|
||||
});
|
||||
}
|
||||
@@ -148,7 +121,7 @@ class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer
|
||||
|
||||
if ($globalSql) {
|
||||
$sql[] = "-- Work on Root Federation\nUSE FEDERATION ROOT WITH RESET;";
|
||||
$sql = array_merge($sql, $globalSql);
|
||||
$sql = array_merge($sql, $globalSql);
|
||||
}
|
||||
|
||||
$shards = $this->shardManager->getShards();
|
||||
@@ -156,15 +129,17 @@ class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer
|
||||
$this->shardManager->selectShard($shard['rangeLow']);
|
||||
|
||||
$federationSql = $this->synchronizer->getDropAllSchema();
|
||||
if ($federationSql) {
|
||||
$sql[] = "-- Work on Federation ID " . $shard['id'] . "\n" .
|
||||
"USE FEDERATION " . $this->shardManager->getFederationName() . " (" . $this->shardManager->getDistributionKey() . " = " . $shard['rangeLow'].") WITH RESET, FILTERING = OFF;";
|
||||
$sql = array_merge($sql, $federationSql);
|
||||
if (! $federationSql) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$sql[] = '-- Work on Federation ID ' . $shard['id'] . "\n" .
|
||||
'USE FEDERATION ' . $this->shardManager->getFederationName() . ' (' . $this->shardManager->getDistributionKey() . ' = ' . $shard['rangeLow'] . ') WITH RESET, FILTERING = OFF;';
|
||||
$sql = array_merge($sql, $federationSql);
|
||||
}
|
||||
|
||||
$sql[] = "USE FEDERATION ROOT WITH RESET;";
|
||||
$sql[] = "DROP FEDERATION " . $this->shardManager->getFederationName();
|
||||
$sql[] = 'USE FEDERATION ROOT WITH RESET;';
|
||||
$sql[] = 'DROP FEDERATION ' . $this->shardManager->getFederationName();
|
||||
|
||||
return $sql;
|
||||
}
|
||||
@@ -178,9 +153,7 @@ class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Doctrine\DBAL\Schema\Schema $schema
|
||||
*
|
||||
* @return array
|
||||
* @return Schema[]
|
||||
*/
|
||||
private function partitionSchema(Schema $schema)
|
||||
{
|
||||
@@ -191,12 +164,11 @@ class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Doctrine\DBAL\Schema\Schema $schema
|
||||
* @param bool $isFederation
|
||||
* @param bool $isFederation
|
||||
*
|
||||
* @return \Doctrine\DBAL\Schema\Schema
|
||||
* @return Schema
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
private function extractSchemaFederation(Schema $schema, $isFederation)
|
||||
{
|
||||
@@ -213,7 +185,7 @@ class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer
|
||||
foreach ($table->getForeignKeys() as $fk) {
|
||||
$foreignTable = $schema->getTable($fk->getForeignTableName());
|
||||
if ($foreignTable->hasOption(self::FEDERATION_TABLE_FEDERATED) !== $isFederation) {
|
||||
throw new \RuntimeException("Cannot have foreign key between global/federation.");
|
||||
throw new RuntimeException('Cannot have foreign key between global/federation.');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -227,15 +199,12 @@ class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer
|
||||
* perform the given operation on the underlying schema synchronizer given
|
||||
* the different partitioned schema instances.
|
||||
*
|
||||
* @param \Doctrine\DBAL\Schema\Schema $schema
|
||||
* @param \Closure $operation
|
||||
*
|
||||
* @return array
|
||||
* @return string[]
|
||||
*/
|
||||
private function work(Schema $schema, \Closure $operation)
|
||||
private function work(Schema $schema, Closure $operation)
|
||||
{
|
||||
list($global, $federation) = $this->partitionSchema($schema);
|
||||
$sql = [];
|
||||
[$global, $federation] = $this->partitionSchema($schema);
|
||||
$sql = [];
|
||||
|
||||
$this->shardManager->selectGlobal();
|
||||
$globalSql = $operation($this->synchronizer, $global);
|
||||
@@ -251,11 +220,13 @@ class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer
|
||||
$this->shardManager->selectShard($shard['rangeLow']);
|
||||
|
||||
$federationSql = $operation($this->synchronizer, $federation);
|
||||
if ($federationSql) {
|
||||
$sql[] = "-- Work on Federation ID " . $shard['id'] . "\n" .
|
||||
"USE FEDERATION " . $this->shardManager->getFederationName() . " (" . $this->shardManager->getDistributionKey() . " = " . $shard['rangeLow'].") WITH RESET, FILTERING = OFF;";
|
||||
$sql = array_merge($sql, $federationSql);
|
||||
if (! $federationSql) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$sql[] = '-- Work on Federation ID ' . $shard['id'] . "\n" .
|
||||
'USE FEDERATION ' . $this->shardManager->getFederationName() . ' (' . $this->shardManager->getDistributionKey() . ' = ' . $shard['rangeLow'] . ') WITH RESET, FILTERING = OFF;';
|
||||
$sql = array_merge($sql, $federationSql);
|
||||
}
|
||||
|
||||
return $sql;
|
||||
@@ -290,10 +261,10 @@ class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer
|
||||
*/
|
||||
private function getCreateFederationStatement()
|
||||
{
|
||||
$federationType = Type::getType($this->shardManager->getDistributionType());
|
||||
$federationType = Type::getType($this->shardManager->getDistributionType());
|
||||
$federationTypeSql = $federationType->getSQLDeclaration([], $this->conn->getDatabasePlatform());
|
||||
|
||||
return "--Create Federation\n" .
|
||||
"CREATE FEDERATION " . $this->shardManager->getFederationName() . " (" . $this->shardManager->getDistributionKey() . " " . $federationTypeSql ." RANGE)";
|
||||
'CREATE FEDERATION ' . $this->shardManager->getFederationName() . ' (' . $this->shardManager->getDistributionKey() . ' ' . $federationTypeSql . ' RANGE)';
|
||||
}
|
||||
}
|
||||
|
@@ -1,21 +1,4 @@
|
||||
<?php
|
||||
/*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the MIT license. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\DBAL\Sharding\SQLAzure;
|
||||
|
||||
@@ -23,71 +6,56 @@ use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\Sharding\ShardingException;
|
||||
use Doctrine\DBAL\Sharding\ShardManager;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use RuntimeException;
|
||||
use function is_bool;
|
||||
use function is_scalar;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Sharding using the SQL Azure Federations support.
|
||||
*
|
||||
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
||||
*/
|
||||
class SQLAzureShardManager implements ShardManager
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
private $federationName;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
/** @var bool */
|
||||
private $filteringEnabled;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
private $distributionKey;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
private $distributionType;
|
||||
|
||||
/**
|
||||
* @var \Doctrine\DBAL\Connection
|
||||
*/
|
||||
/** @var Connection */
|
||||
private $conn;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
/** @var string|null */
|
||||
private $currentDistributionValue;
|
||||
|
||||
/**
|
||||
* @param \Doctrine\DBAL\Connection $conn
|
||||
*
|
||||
* @throws \Doctrine\DBAL\Sharding\ShardingException
|
||||
* @throws ShardingException
|
||||
*/
|
||||
public function __construct(Connection $conn)
|
||||
{
|
||||
$this->conn = $conn;
|
||||
$params = $conn->getParams();
|
||||
$params = $conn->getParams();
|
||||
|
||||
if ( ! isset($params['sharding']['federationName'])) {
|
||||
if (! isset($params['sharding']['federationName'])) {
|
||||
throw ShardingException::missingDefaultFederationName();
|
||||
}
|
||||
|
||||
if ( ! isset($params['sharding']['distributionKey'])) {
|
||||
if (! isset($params['sharding']['distributionKey'])) {
|
||||
throw ShardingException::missingDefaultDistributionKey();
|
||||
}
|
||||
|
||||
if ( ! isset($params['sharding']['distributionType'])) {
|
||||
if (! isset($params['sharding']['distributionType'])) {
|
||||
throw ShardingException::missingDistributionType();
|
||||
}
|
||||
|
||||
$this->federationName = $params['sharding']['federationName'];
|
||||
$this->distributionKey = $params['sharding']['distributionKey'];
|
||||
$this->federationName = $params['sharding']['federationName'];
|
||||
$this->distributionKey = $params['sharding']['distributionKey'];
|
||||
$this->distributionType = $params['sharding']['distributionType'];
|
||||
$this->filteringEnabled = (bool) ($params['sharding']['filteringEnabled'] ?? false);
|
||||
}
|
||||
@@ -143,7 +111,7 @@ class SQLAzureShardManager implements ShardManager
|
||||
throw ShardingException::activeTransaction();
|
||||
}
|
||||
|
||||
$sql = "USE FEDERATION ROOT WITH RESET";
|
||||
$sql = 'USE FEDERATION ROOT WITH RESET';
|
||||
$this->conn->exec($sql);
|
||||
$this->currentDistributionValue = null;
|
||||
}
|
||||
@@ -157,13 +125,13 @@ class SQLAzureShardManager implements ShardManager
|
||||
throw ShardingException::activeTransaction();
|
||||
}
|
||||
|
||||
if ($distributionValue === null || is_bool($distributionValue) || !is_scalar($distributionValue)) {
|
||||
if ($distributionValue === null || is_bool($distributionValue) || ! is_scalar($distributionValue)) {
|
||||
throw ShardingException::noShardDistributionValue();
|
||||
}
|
||||
|
||||
$platform = $this->conn->getDatabasePlatform();
|
||||
$sql = sprintf(
|
||||
"USE FEDERATION %s (%s = %s) WITH RESET, FILTERING = %s;",
|
||||
$sql = sprintf(
|
||||
'USE FEDERATION %s (%s = %s) WITH RESET, FILTERING = %s;',
|
||||
$platform->quoteIdentifier($this->federationName),
|
||||
$platform->quoteIdentifier($this->distributionKey),
|
||||
$this->conn->quote($distributionValue),
|
||||
@@ -187,13 +155,13 @@ class SQLAzureShardManager implements ShardManager
|
||||
*/
|
||||
public function getShards()
|
||||
{
|
||||
$sql = "SELECT member_id as id,
|
||||
$sql = 'SELECT member_id as id,
|
||||
distribution_name as distribution_key,
|
||||
CAST(range_low AS CHAR) AS rangeLow,
|
||||
CAST(range_high AS CHAR) AS rangeHigh
|
||||
FROM sys.federation_member_distributions d
|
||||
INNER JOIN sys.federations f ON f.federation_id = d.federation_id
|
||||
WHERE f.name = " . $this->conn->quote($this->federationName);
|
||||
WHERE f.name = ' . $this->conn->quote($this->federationName);
|
||||
|
||||
return $this->conn->fetchAll($sql);
|
||||
}
|
||||
@@ -204,11 +172,11 @@ class SQLAzureShardManager implements ShardManager
|
||||
public function queryAll($sql, array $params = [], array $types = [])
|
||||
{
|
||||
$shards = $this->getShards();
|
||||
if (!$shards) {
|
||||
throw new \RuntimeException("No shards found for " . $this->federationName);
|
||||
if (! $shards) {
|
||||
throw new RuntimeException('No shards found for ' . $this->federationName);
|
||||
}
|
||||
|
||||
$result = [];
|
||||
$result = [];
|
||||
$oldDistribution = $this->getCurrentDistributionValue();
|
||||
|
||||
foreach ($shards as $shard) {
|
||||
@@ -238,9 +206,9 @@ class SQLAzureShardManager implements ShardManager
|
||||
{
|
||||
$type = Type::getType($this->distributionType);
|
||||
|
||||
$sql = "ALTER FEDERATION " . $this->getFederationName() . " " .
|
||||
"SPLIT AT (" . $this->getDistributionKey() . " = " .
|
||||
$this->conn->quote($splitDistributionValue, $type->getBindingType()) . ")";
|
||||
$sql = 'ALTER FEDERATION ' . $this->getFederationName() . ' ' .
|
||||
'SPLIT AT (' . $this->getDistributionKey() . ' = ' .
|
||||
$this->conn->quote($splitDistributionValue, $type->getBindingType()) . ')';
|
||||
$this->conn->exec($sql);
|
||||
}
|
||||
}
|
||||
|
@@ -1,31 +1,15 @@
|
||||
<?php
|
||||
/*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the MIT license. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\DBAL\Sharding\SQLAzure\Schema;
|
||||
|
||||
use Doctrine\DBAL\Schema\Visitor\Visitor;
|
||||
use Doctrine\DBAL\Schema\Table;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\DBAL\Schema\Column;
|
||||
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
|
||||
use Doctrine\DBAL\Schema\Sequence;
|
||||
use Doctrine\DBAL\Schema\Index;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\DBAL\Schema\Sequence;
|
||||
use Doctrine\DBAL\Schema\Table;
|
||||
use Doctrine\DBAL\Schema\Visitor\Visitor;
|
||||
use RuntimeException;
|
||||
use function in_array;
|
||||
|
||||
/**
|
||||
@@ -46,24 +30,16 @@ use function in_array;
|
||||
* (otherwise they will affect the same-id rows from other tenants as well).
|
||||
* SQLAzure throws errors when you try to create IDENTIY columns on federated
|
||||
* tables.
|
||||
*
|
||||
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
||||
*/
|
||||
class MultiTenantVisitor implements Visitor
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
/** @var string[] */
|
||||
private $excludedTables = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
private $tenantColumnName;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
private $tenantColumnType = 'integer';
|
||||
|
||||
/**
|
||||
@@ -75,13 +51,13 @@ class MultiTenantVisitor implements Visitor
|
||||
private $distributionName;
|
||||
|
||||
/**
|
||||
* @param array $excludedTables
|
||||
* @param string[] $excludedTables
|
||||
* @param string $tenantColumnName
|
||||
* @param string|null $distributionName
|
||||
*/
|
||||
public function __construct(array $excludedTables = [], $tenantColumnName = 'tenant_id', $distributionName = null)
|
||||
{
|
||||
$this->excludedTables = $excludedTables;
|
||||
$this->excludedTables = $excludedTables;
|
||||
$this->tenantColumnName = $tenantColumnName;
|
||||
$this->distributionName = $distributionName ?: $tenantColumnName;
|
||||
}
|
||||
@@ -96,12 +72,12 @@ class MultiTenantVisitor implements Visitor
|
||||
}
|
||||
|
||||
$table->addColumn($this->tenantColumnName, $this->tenantColumnType, [
|
||||
'default' => "federation_filtering_value('". $this->distributionName ."')",
|
||||
'default' => "federation_filtering_value('" . $this->distributionName . "')",
|
||||
]);
|
||||
|
||||
$clusteredIndex = $this->getClusteredIndex($table);
|
||||
|
||||
$indexColumns = $clusteredIndex->getColumns();
|
||||
$indexColumns = $clusteredIndex->getColumns();
|
||||
$indexColumns[] = $this->tenantColumnName;
|
||||
|
||||
if ($clusteredIndex->isPrimary()) {
|
||||
@@ -115,22 +91,24 @@ class MultiTenantVisitor implements Visitor
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Doctrine\DBAL\Schema\Table $table
|
||||
* @param Table $table
|
||||
*
|
||||
* @return \Doctrine\DBAL\Schema\Index
|
||||
* @return Index
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
private function getClusteredIndex($table)
|
||||
{
|
||||
foreach ($table->getIndexes() as $index) {
|
||||
if ($index->isPrimary() && ! $index->hasFlag('nonclustered')) {
|
||||
return $index;
|
||||
} elseif ($index->hasFlag('clustered')) {
|
||||
}
|
||||
|
||||
if ($index->hasFlag('clustered')) {
|
||||
return $index;
|
||||
}
|
||||
}
|
||||
throw new \RuntimeException("No clustered index found on table " . $table->getName());
|
||||
throw new RuntimeException('No clustered index found on table ' . $table->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user