| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Doctrine\DBAL\Schema\Visitor;
|
|
|
4 |
|
|
|
5 |
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
|
|
|
6 |
use Doctrine\DBAL\Schema\Schema;
|
|
|
7 |
use Doctrine\DBAL\Schema\Sequence;
|
|
|
8 |
use Doctrine\DBAL\Schema\Table;
|
|
|
9 |
use Doctrine\Deprecations\Deprecation;
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* Removes assets from a schema that are not in the default namespace.
|
|
|
13 |
*
|
|
|
14 |
* Some databases such as MySQL support cross databases joins, but don't
|
|
|
15 |
* allow to call DDLs to a database from another connected database.
|
|
|
16 |
* Before a schema is serialized into SQL this visitor can cleanup schemas with
|
|
|
17 |
* non default namespaces.
|
|
|
18 |
*
|
|
|
19 |
* This visitor filters all these non-default namespaced tables and sequences
|
|
|
20 |
* and removes them from the Schema instance.
|
|
|
21 |
*
|
|
|
22 |
* @deprecated Do not use namespaces if the target database platform doesn't support them.
|
|
|
23 |
*/
|
|
|
24 |
class RemoveNamespacedAssets extends AbstractVisitor
|
|
|
25 |
{
|
|
|
26 |
private ?Schema $schema = null;
|
|
|
27 |
|
|
|
28 |
public function __construct()
|
|
|
29 |
{
|
|
|
30 |
Deprecation::trigger(
|
|
|
31 |
'doctrine/dbal',
|
|
|
32 |
'https://github.com/doctrine/dbal/pull/5432',
|
|
|
33 |
'RemoveNamespacedAssets is deprecated. Do not use namespaces'
|
|
|
34 |
. " if the target database platform doesn't support them.",
|
|
|
35 |
);
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* {@inheritdoc}
|
|
|
40 |
*/
|
|
|
41 |
public function acceptSchema(Schema $schema)
|
|
|
42 |
{
|
|
|
43 |
$this->schema = $schema;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* {@inheritdoc}
|
|
|
48 |
*/
|
|
|
49 |
public function acceptTable(Table $table)
|
|
|
50 |
{
|
|
|
51 |
if ($this->schema === null) {
|
|
|
52 |
return;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
if ($table->isInDefaultNamespace($this->schema->getName())) {
|
|
|
56 |
return;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
$this->schema->dropTable($table->getName());
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* {@inheritdoc}
|
|
|
64 |
*/
|
|
|
65 |
public function acceptSequence(Sequence $sequence)
|
|
|
66 |
{
|
|
|
67 |
if ($this->schema === null) {
|
|
|
68 |
return;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
if ($sequence->isInDefaultNamespace($this->schema->getName())) {
|
|
|
72 |
return;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
$this->schema->dropSequence($sequence->getName());
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* {@inheritdoc}
|
|
|
80 |
*/
|
|
|
81 |
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
|
|
|
82 |
{
|
|
|
83 |
if ($this->schema === null) {
|
|
|
84 |
return;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
// The table may already be deleted in a previous
|
|
|
88 |
// RemoveNamespacedAssets#acceptTable call. Removing Foreign keys that
|
|
|
89 |
// point to nowhere.
|
|
|
90 |
if (! $this->schema->hasTable($fkConstraint->getForeignTableName())) {
|
|
|
91 |
$localTable->removeForeignKey($fkConstraint->getName());
|
|
|
92 |
|
|
|
93 |
return;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
$foreignTable = $this->schema->getTable($fkConstraint->getForeignTableName());
|
|
|
97 |
if ($foreignTable->isInDefaultNamespace($this->schema->getName())) {
|
|
|
98 |
return;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
$localTable->removeForeignKey($fkConstraint->getName());
|
|
|
102 |
}
|
|
|
103 |
}
|