|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Sharding\SQLAzure\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\Column; |
|
6
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
|
7
|
|
|
use Doctrine\DBAL\Schema\Index; |
|
8
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
9
|
|
|
use Doctrine\DBAL\Schema\Sequence; |
|
10
|
|
|
use Doctrine\DBAL\Schema\Table; |
|
11
|
|
|
use Doctrine\DBAL\Schema\Visitor\Visitor; |
|
12
|
|
|
use RuntimeException; |
|
13
|
|
|
use function in_array; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Converts a single tenant schema into a multi-tenant schema for SQL Azure |
|
17
|
|
|
* Federations under the following assumptions: |
|
18
|
|
|
* |
|
19
|
|
|
* - Every table is part of the multi-tenant application, only explicitly |
|
20
|
|
|
* excluded tables are non-federated. The behavior of the tables being in |
|
21
|
|
|
* global or federated database is undefined. It depends on you selecting a |
|
22
|
|
|
* federation before DDL statements or not. |
|
23
|
|
|
* - Every Primary key of a federated table is extended by another column |
|
24
|
|
|
* 'tenant_id' with a default value of the SQLAzure function |
|
25
|
|
|
* `federation_filtering_value('tenant_id')`. |
|
26
|
|
|
* - You always have to work with `filtering=On` when using federations with this |
|
27
|
|
|
* multi-tenant approach. |
|
28
|
|
|
* - Primary keys are either using globally unique ids (GUID, Table Generator) |
|
29
|
|
|
* or you explicitly add the tenant_id in every UPDATE or DELETE statement |
|
30
|
|
|
* (otherwise they will affect the same-id rows from other tenants as well). |
|
31
|
|
|
* SQLAzure throws errors when you try to create IDENTIY columns on federated |
|
32
|
|
|
* tables. |
|
33
|
|
|
* |
|
34
|
|
|
* @deprecated |
|
35
|
|
|
*/ |
|
36
|
|
|
class MultiTenantVisitor implements Visitor |
|
37
|
|
|
{ |
|
38
|
|
|
/** @var string[] */ |
|
39
|
|
|
private $excludedTables = []; |
|
40
|
|
|
|
|
41
|
|
|
/** @var string */ |
|
42
|
|
|
private $tenantColumnName; |
|
43
|
|
|
|
|
44
|
|
|
/** @var string */ |
|
45
|
|
|
private $tenantColumnType = 'integer'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Name of the federation distribution, defaulting to the tenantColumnName |
|
49
|
|
|
* if not specified. |
|
50
|
|
|
* |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
private $distributionName; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string[] $excludedTables |
|
57
|
|
|
* @param string $tenantColumnName |
|
58
|
|
|
* @param string|null $distributionName |
|
59
|
|
|
*/ |
|
60
|
48 |
|
public function __construct(array $excludedTables = [], $tenantColumnName = 'tenant_id', $distributionName = null) |
|
61
|
|
|
{ |
|
62
|
48 |
|
$this->excludedTables = $excludedTables; |
|
63
|
48 |
|
$this->tenantColumnName = $tenantColumnName; |
|
64
|
48 |
|
$this->distributionName = $distributionName ?: $tenantColumnName; |
|
65
|
48 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
48 |
|
public function acceptTable(Table $table) |
|
71
|
|
|
{ |
|
72
|
48 |
|
if (in_array($table->getName(), $this->excludedTables)) { |
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
48 |
|
$table->addColumn($this->tenantColumnName, $this->tenantColumnType, [ |
|
77
|
48 |
|
'default' => "federation_filtering_value('" . $this->distributionName . "')", |
|
78
|
|
|
]); |
|
79
|
|
|
|
|
80
|
48 |
|
$clusteredIndex = $this->getClusteredIndex($table); |
|
81
|
|
|
|
|
82
|
48 |
|
$indexColumns = $clusteredIndex->getColumns(); |
|
83
|
48 |
|
$indexColumns[] = $this->tenantColumnName; |
|
84
|
|
|
|
|
85
|
48 |
|
if ($clusteredIndex->isPrimary()) { |
|
86
|
47 |
|
$table->dropPrimaryKey(); |
|
87
|
47 |
|
$table->setPrimaryKey($indexColumns); |
|
88
|
|
|
} else { |
|
89
|
24 |
|
$table->dropIndex($clusteredIndex->getName()); |
|
90
|
24 |
|
$table->addIndex($indexColumns, $clusteredIndex->getName()); |
|
91
|
24 |
|
$table->getIndex($clusteredIndex->getName())->addFlag('clustered'); |
|
92
|
|
|
} |
|
93
|
48 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param Table $table |
|
97
|
|
|
* |
|
98
|
|
|
* @return Index |
|
99
|
|
|
* |
|
100
|
|
|
* @throws RuntimeException |
|
101
|
|
|
*/ |
|
102
|
48 |
|
private function getClusteredIndex($table) |
|
103
|
|
|
{ |
|
104
|
48 |
|
foreach ($table->getIndexes() as $index) { |
|
105
|
48 |
|
if ($index->isPrimary() && ! $index->hasFlag('nonclustered')) { |
|
106
|
47 |
|
return $index; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
24 |
|
if ($index->hasFlag('clustered')) { |
|
110
|
24 |
|
return $index; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
throw new RuntimeException('No clustered index found on table ' . $table->getName()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* {@inheritdoc} |
|
119
|
|
|
*/ |
|
120
|
48 |
|
public function acceptSchema(Schema $schema) |
|
121
|
|
|
{ |
|
122
|
48 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* {@inheritdoc} |
|
126
|
|
|
*/ |
|
127
|
48 |
|
public function acceptColumn(Table $table, Column $column) |
|
128
|
|
|
{ |
|
129
|
48 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* {@inheritdoc} |
|
133
|
|
|
*/ |
|
134
|
|
|
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) |
|
135
|
|
|
{ |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* {@inheritdoc} |
|
140
|
|
|
*/ |
|
141
|
48 |
|
public function acceptIndex(Table $table, Index $index) |
|
142
|
|
|
{ |
|
143
|
48 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* {@inheritdoc} |
|
147
|
|
|
*/ |
|
148
|
|
|
public function acceptSequence(Sequence $sequence) |
|
149
|
|
|
{ |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|