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