1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Schema\Visitor; |
21
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Schema\Table; |
23
|
|
|
use Doctrine\DBAL\Schema\Schema; |
24
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
25
|
|
|
use Doctrine\DBAL\Schema\Sequence; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Removes assets from a schema that are not in the default namespace. |
29
|
|
|
* |
30
|
|
|
* Some databases such as MySQL support cross databases joins, but don't |
31
|
|
|
* allow to call DDLs to a database from another connected database. |
32
|
|
|
* Before a schema is serialized into SQL this visitor can cleanup schemas with |
33
|
|
|
* non default namespaces. |
34
|
|
|
* |
35
|
|
|
* This visitor filters all these non-default namespaced tables and sequences |
36
|
|
|
* and removes them from the SChema instance. |
37
|
|
|
* |
38
|
|
|
* @author Benjamin Eberlei <[email protected]> |
39
|
|
|
* @since 2.2 |
40
|
|
|
*/ |
41
|
|
|
class RemoveNamespacedAssets extends AbstractVisitor |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var \Doctrine\DBAL\Schema\Schema |
45
|
|
|
*/ |
46
|
|
|
private $schema; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
3 |
|
public function acceptSchema(Schema $schema) |
52
|
|
|
{ |
53
|
3 |
|
$this->schema = $schema; |
54
|
3 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
3 |
|
public function acceptTable(Table $table) |
60
|
|
|
{ |
61
|
3 |
|
if ( ! $table->isInDefaultNamespace($this->schema->getName())) { |
62
|
3 |
|
$this->schema->dropTable($table->getName()); |
63
|
|
|
} |
64
|
3 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function acceptSequence(Sequence $sequence) |
70
|
|
|
{ |
71
|
|
|
if ( ! $sequence->isInDefaultNamespace($this->schema->getName())) { |
72
|
|
|
$this->schema->dropSequence($sequence->getName()); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
2 |
|
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) |
80
|
|
|
{ |
81
|
|
|
// The table may already be deleted in a previous |
82
|
|
|
// RemoveNamespacedAssets#acceptTable call. Removing Foreign keys that |
83
|
|
|
// point to nowhere. |
84
|
2 |
|
if ( ! $this->schema->hasTable($fkConstraint->getForeignTableName())) { |
85
|
1 |
|
$localTable->removeForeignKey($fkConstraint->getName()); |
86
|
1 |
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
$foreignTable = $this->schema->getTable($fkConstraint->getForeignTableName()); |
90
|
1 |
|
if ( ! $foreignTable->isInDefaultNamespace($this->schema->getName())) { |
91
|
1 |
|
$localTable->removeForeignKey($fkConstraint->getName()); |
92
|
|
|
} |
93
|
1 |
|
} |
94
|
|
|
} |
95
|
|
|
|