Completed
Pull Request — master (#3233)
by Sergey
65:15
created

RemoveNamespacedAssets::acceptView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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\DBAL\Schema\View;
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
class RemoveNamespacedAssets extends AbstractVisitor
23
{
24
    /** @var Schema */
25
    private $schema;
26
27
    /**
28
     * {@inheritdoc}
29 57
     */
30
    public function acceptSchema(Schema $schema)
31 57
    {
32 57
        $this->schema = $schema;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37 57
     */
38
    public function acceptTable(Table $table)
39 57
    {
40 57
        if ($table->isInDefaultNamespace($this->schema->getName())) {
41
            return;
42
        }
43 57
44 57
        $this->schema->dropTable($table->getName());
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function acceptSequence(Sequence $sequence)
51
    {
52
        if ($sequence->isInDefaultNamespace($this->schema->getName())) {
53
            return;
54
        }
55
56
        $this->schema->dropSequence($sequence->getName());
57
    }
58
59
    /**
60
     * {@inheritdoc}
61 38
     */
62
    public function acceptView(View $view)
63
    {
64
        if ($view->isInDefaultNamespace($this->schema->getName())) {
65
            return;
66 38
        }
67 19
68 19
        $this->schema->dropView($view->getName());
69
    }
70
71 19
    /**
72 19
     * {@inheritdoc}
73
     */
74
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
75
    {
76 19
        // The table may already be deleted in a previous
77 19
        // RemoveNamespacedAssets#acceptTable call. Removing Foreign keys that
78
        // point to nowhere.
79
        if (! $this->schema->hasTable($fkConstraint->getForeignTableName())) {
80
            $localTable->removeForeignKey($fkConstraint->getName());
81
            return;
82
        }
83
84
        $foreignTable = $this->schema->getTable($fkConstraint->getForeignTableName());
85
        if ($foreignTable->isInDefaultNamespace($this->schema->getName())) {
86
            return;
87
        }
88
89
        $localTable->removeForeignKey($fkConstraint->getName());
90
    }
91
}
92