MultiTenantVisitor.php 4.18 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Sharding\SQLAzure\Schema;

Benjamin Morel's avatar
Benjamin Morel committed
5 6 7
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
8 9 10 11 12
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Visitor\Visitor;
use RuntimeException;
13
use function in_array;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

/**
 * Converts a single tenant schema into a multi-tenant schema for SQL Azure
 * Federations under the following assumptions:
 *
 * - Every table is part of the multi-tenant application, only explicitly
 *   excluded tables are non-federated. The behavior of the tables being in
 *   global or federated database is undefined. It depends on you selecting a
 *   federation before DDL statements or not.
 * - Every Primary key of a federated table is extended by another column
 *   'tenant_id' with a default value of the SQLAzure function
 *   `federation_filtering_value('tenant_id')`.
 * - You always have to work with `filtering=On` when using federations with this
 *   multi-tenant approach.
 * - Primary keys are either using globally unique ids (GUID, Table Generator)
Possum's avatar
Possum committed
29 30
 *   or you explicitly add the tenant_id in every UPDATE or DELETE statement
 *   (otherwise they will affect the same-id rows from other tenants as well).
31 32 33 34 35
 *   SQLAzure throws errors when you try to create IDENTIY columns on federated
 *   tables.
 */
class MultiTenantVisitor implements Visitor
{
36
    /** @var string[] */
37
    private $excludedTables = [];
38

39
    /** @var string */
40 41
    private $tenantColumnName;

42
    /** @var string */
43 44 45 46 47 48 49 50 51 52
    private $tenantColumnType = 'integer';

    /**
     * Name of the federation distribution, defaulting to the tenantColumnName
     * if not specified.
     *
     * @var string
     */
    private $distributionName;

Benjamin Morel's avatar
Benjamin Morel committed
53
    /**
54
     * @param string[]    $excludedTables
Benjamin Morel's avatar
Benjamin Morel committed
55 56 57
     * @param string      $tenantColumnName
     * @param string|null $distributionName
     */
58
    public function __construct(array $excludedTables = [], $tenantColumnName = 'tenant_id', $distributionName = null)
59
    {
60
        $this->excludedTables   = $excludedTables;
61 62 63 64 65
        $this->tenantColumnName = $tenantColumnName;
        $this->distributionName = $distributionName ?: $tenantColumnName;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
66
     * {@inheritdoc}
67 68 69 70 71 72 73
     */
    public function acceptTable(Table $table)
    {
        if (in_array($table->getName(), $this->excludedTables)) {
            return;
        }

74
        $table->addColumn($this->tenantColumnName, $this->tenantColumnType, [
75
            'default' => "federation_filtering_value('" . $this->distributionName . "')",
76
        ]);
77 78 79

        $clusteredIndex = $this->getClusteredIndex($table);

80
        $indexColumns   = $clusteredIndex->getColumns();
81 82 83 84 85 86 87 88 89 90 91 92
        $indexColumns[] = $this->tenantColumnName;

        if ($clusteredIndex->isPrimary()) {
            $table->dropPrimaryKey();
            $table->setPrimaryKey($indexColumns);
        } else {
            $table->dropIndex($clusteredIndex->getName());
            $table->addIndex($indexColumns, $clusteredIndex->getName());
            $table->getIndex($clusteredIndex->getName())->addFlag('clustered');
        }
    }

Benjamin Morel's avatar
Benjamin Morel committed
93
    /**
94
     * @param Table $table
Benjamin Morel's avatar
Benjamin Morel committed
95
     *
96
     * @return Index
Benjamin Morel's avatar
Benjamin Morel committed
97
     *
98
     * @throws RuntimeException
Benjamin Morel's avatar
Benjamin Morel committed
99
     */
100 101 102 103 104
    private function getClusteredIndex($table)
    {
        foreach ($table->getIndexes() as $index) {
            if ($index->isPrimary() && ! $index->hasFlag('nonclustered')) {
                return $index;
105 106 107
            }

            if ($index->hasFlag('clustered')) {
108 109 110
                return $index;
            }
        }
Grégoire Paris's avatar
Grégoire Paris committed
111

112
        throw new RuntimeException('No clustered index found on table ' . $table->getName());
113 114 115
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
116
     * {@inheritdoc}
117 118 119 120 121 122
     */
    public function acceptSchema(Schema $schema)
    {
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
123
     * {@inheritdoc}
124 125 126 127 128 129
     */
    public function acceptColumn(Table $table, Column $column)
    {
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
130
     * {@inheritdoc}
131 132 133 134 135 136
     */
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
    {
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
137
     * {@inheritdoc}
138 139 140 141 142 143
     */
    public function acceptIndex(Table $table, Index $index)
    {
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
144
     * {@inheritdoc}
145 146 147 148 149
     */
    public function acceptSequence(Sequence $sequence)
    {
    }
}