SingleDatabaseSynchronizer.php 3.86 KB
Newer Older
1
<?php
Benjamin Morel's avatar
Benjamin Morel committed
2

3
namespace Doctrine\DBAL\Schema\Synchronizer;
4 5

use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
7
use Doctrine\DBAL\Schema\Comparator;
8
use Doctrine\DBAL\Schema\Schema;
9
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
10

11
use function count;
12 13

/**
Benjamin Morel's avatar
Benjamin Morel committed
14
 * Schema Synchronizer for Default DBAL Connection.
15
 */
16
class SingleDatabaseSynchronizer extends AbstractSchemaSynchronizer
17
{
18
    /** @var AbstractPlatform */
19 20 21 22
    private $platform;

    public function __construct(Connection $conn)
    {
23
        parent::__construct($conn);
24 25 26 27
        $this->platform = $conn->getDatabasePlatform();
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
28
     * {@inheritdoc}
29 30 31 32 33 34 35
     */
    public function getCreateSchema(Schema $createSchema)
    {
        return $createSchema->toSql($this->platform);
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
36
     * {@inheritdoc}
37 38 39 40 41 42 43 44 45 46 47 48
     */
    public function getUpdateSchema(Schema $toSchema, $noDrops = false)
    {
        $comparator = new Comparator();
        $sm         = $this->conn->getSchemaManager();

        $fromSchema = $sm->createSchema();
        $schemaDiff = $comparator->compare($fromSchema, $toSchema);

        if ($noDrops) {
            return $schemaDiff->toSaveSql($this->platform);
        }
49 50

        return $schemaDiff->toSql($this->platform);
51 52 53
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
54
     * {@inheritdoc}
55 56 57
     */
    public function getDropSchema(Schema $dropSchema)
    {
58 59
        $visitor = new DropSchemaSqlCollector($this->platform);
        $sm      = $this->conn->getSchemaManager();
60 61 62 63

        $fullSchema = $sm->createSchema();

        foreach ($fullSchema->getTables() as $table) {
Steve Müller's avatar
Steve Müller committed
64
            if ($dropSchema->hasTable($table->getName())) {
65 66 67 68
                $visitor->acceptTable($table);
            }

            foreach ($table->getForeignKeys() as $foreignKey) {
69
                if (! $dropSchema->hasTable($table->getName())) {
70 71 72
                    continue;
                }

73
                if (! $dropSchema->hasTable($foreignKey->getForeignTableName())) {
74 75 76 77 78 79 80
                    continue;
                }

                $visitor->acceptForeignKey($table, $foreignKey);
            }
        }

81
        if (! $this->platform->supportsSequences()) {
82 83 84 85 86 87 88 89
            return $visitor->getQueries();
        }

        foreach ($dropSchema->getSequences() as $sequence) {
            $visitor->acceptSequence($sequence);
        }

        foreach ($dropSchema->getTables() as $table) {
Sergei Morozov's avatar
Sergei Morozov committed
90 91 92
            $primaryKey = $table->getPrimaryKey();

            if ($primaryKey === null) {
93 94 95
                continue;
            }

Sergei Morozov's avatar
Sergei Morozov committed
96 97
            $columns = $primaryKey->getColumns();

98 99 100 101
            if (count($columns) > 1) {
                continue;
            }

102 103 104
            $checkSequence = $table->getName() . '_' . $columns[0] . '_seq';
            if (! $fullSchema->hasSequence($checkSequence)) {
                continue;
105
            }
106 107

            $visitor->acceptSequence($fullSchema->getSequence($checkSequence));
108 109 110 111 112 113
        }

        return $visitor->getQueries();
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
114
     * {@inheritdoc}
115 116 117 118
     */
    public function getDropAllSchema()
    {
        $sm      = $this->conn->getSchemaManager();
119
        $visitor = new DropSchemaSqlCollector($this->platform);
120

121
        $schema = $sm->createSchema();
122 123 124 125 126 127
        $schema->visit($visitor);

        return $visitor->getQueries();
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
128
     * {@inheritdoc}
129 130 131 132 133 134 135
     */
    public function createSchema(Schema $createSchema)
    {
        $this->processSql($this->getCreateSchema($createSchema));
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
136
     * {@inheritdoc}
137 138 139 140 141 142 143
     */
    public function updateSchema(Schema $toSchema, $noDrops = false)
    {
        $this->processSql($this->getUpdateSchema($toSchema, $noDrops));
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
144
     * {@inheritdoc}
145 146 147 148 149 150 151
     */
    public function dropSchema(Schema $dropSchema)
    {
        $this->processSqlSafely($this->getDropSchema($dropSchema));
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
152
     * {@inheritdoc}
153 154 155 156 157 158
     */
    public function dropAllSchema()
    {
        $this->processSql($this->getDropAllSchema());
    }
}