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
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Removes assets from a schema that are not in the default namespace. |
12
|
|
|
* |
13
|
|
|
* Some databases such as MySQL support cross databases joins, but don't |
14
|
|
|
* allow to call DDLs to a database from another connected database. |
15
|
|
|
* Before a schema is serialized into SQL this visitor can cleanup schemas with |
16
|
|
|
* non default namespaces. |
17
|
|
|
* |
18
|
|
|
* This visitor filters all these non-default namespaced tables and sequences |
19
|
|
|
* and removes them from the SChema instance. |
20
|
|
|
*/ |
21
|
|
|
class RemoveNamespacedAssets extends AbstractVisitor |
22
|
|
|
{ |
23
|
|
|
/** @var Schema */ |
24
|
|
|
private $schema; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
57 |
|
public function acceptSchema(Schema $schema) |
30
|
|
|
{ |
31
|
57 |
|
$this->schema = $schema; |
32
|
57 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
57 |
|
public function acceptTable(Table $table) |
38
|
|
|
{ |
39
|
57 |
|
if ($table->isInDefaultNamespace($this->schema->getName())) { |
40
|
57 |
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
57 |
|
$this->schema->dropTable($table->getName()); |
44
|
57 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function acceptSequence(Sequence $sequence) |
50
|
|
|
{ |
51
|
|
|
if ($sequence->isInDefaultNamespace($this->schema->getName())) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->schema->dropSequence($sequence->getName()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
38 |
|
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) |
62
|
|
|
{ |
63
|
|
|
// The table may already be deleted in a previous |
64
|
|
|
// RemoveNamespacedAssets#acceptTable call. Removing Foreign keys that |
65
|
|
|
// point to nowhere. |
66
|
38 |
|
if (! $this->schema->hasTable($fkConstraint->getForeignTableName())) { |
67
|
19 |
|
$localTable->removeForeignKey($fkConstraint->getName()); |
68
|
19 |
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
19 |
|
$foreignTable = $this->schema->getTable($fkConstraint->getForeignTableName()); |
72
|
19 |
|
if ($foreignTable->isInDefaultNamespace($this->schema->getName())) { |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
19 |
|
$localTable->removeForeignKey($fkConstraint->getName()); |
77
|
19 |
|
} |
78
|
|
|
} |
79
|
|
|
|