Failed Conditions
Pull Request — master (#3233)
by Sergey
10:50
created

RemoveNamespacedAssets::acceptSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
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
use Doctrine\DBAL\Schema\View;
27
28
/**
29
 * Removes assets from a schema that are not in the default namespace.
30
 *
31
 * Some databases such as MySQL support cross databases joins, but don't
32
 * allow to call DDLs to a database from another connected database.
33
 * Before a schema is serialized into SQL this visitor can cleanup schemas with
34
 * non default namespaces.
35
 *
36
 * This visitor filters all these non-default namespaced tables and sequences
37
 * and removes them from the SChema instance.
38
 *
39
 * @author Benjamin Eberlei <[email protected]>
40
 * @since  2.2
41
 */
42
class RemoveNamespacedAssets extends AbstractVisitor
43
{
44
    /**
45
     * @var \Doctrine\DBAL\Schema\Schema
46
     */
47
    private $schema;
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 60
    public function acceptSchema(Schema $schema)
53
    {
54 60
        $this->schema = $schema;
55 60
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 60
    public function acceptTable(Table $table)
61
    {
62 60
        if ( ! $table->isInDefaultNamespace($this->schema->getName())) {
63 60
            $this->schema->dropTable($table->getName());
64
        }
65 60
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function acceptSequence(Sequence $sequence)
71
    {
72
        if ( ! $sequence->isInDefaultNamespace($this->schema->getName())) {
73
            $this->schema->dropSequence($sequence->getName());
74
        }
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function acceptView(View $view)
81
    {
82
        if ( ! $view->isInDefaultNamespace($this->schema->getName())) {
83
            $this->schema->dropView($view->getName());
84
        }
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 40
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
91
    {
92
        // The table may already be deleted in a previous
93
        // RemoveNamespacedAssets#acceptTable call. Removing Foreign keys that
94
        // point to nowhere.
95 40
        if ( ! $this->schema->hasTable($fkConstraint->getForeignTableName())) {
96 20
            $localTable->removeForeignKey($fkConstraint->getName());
97 20
            return;
98
        }
99
100 20
        $foreignTable = $this->schema->getTable($fkConstraint->getForeignTableName());
101 20
        if ( ! $foreignTable->isInDefaultNamespace($this->schema->getName())) {
102 20
            $localTable->removeForeignKey($fkConstraint->getName());
103
        }
104 20
    }
105
}
106