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.
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>.
19 namespace Doctrine\DBAL\Schema\Synchronizer;
21 use Doctrine\DBAL\Connection;
22 use Doctrine\DBAL\Schema\Schema;
23 use Doctrine\DBAL\Schema\Comparator;
24 use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
27 * Schema Synchronizer for Default DBAL Connection
29 * @author Benjamin Eberlei <kontakt@beberlei.de>
31 class SingleDatabaseSynchronizer extends AbstractSchemaSynchronizer
34 * @var Doctrine\DBAL\Platforms\AbstractPlatform
38 public function __construct(Connection $conn)
40 parent::__construct($conn);
41 $this->platform = $conn->getDatabasePlatform();
45 * Get the SQL statements that can be executed to create the schema.
47 * @param Schema $createSchema
50 public function getCreateSchema(Schema $createSchema)
52 return $createSchema->toSql($this->platform);
56 * Get the SQL Statements to update given schema with the underlying db.
58 * @param Schema $toSchema
59 * @param bool $noDrops
62 public function getUpdateSchema(Schema $toSchema, $noDrops = false)
64 $comparator = new Comparator();
65 $sm = $this->conn->getSchemaManager();
67 $fromSchema = $sm->createSchema();
68 $schemaDiff = $comparator->compare($fromSchema, $toSchema);
71 return $schemaDiff->toSaveSql($this->platform);
74 return $schemaDiff->toSql($this->platform);
78 * Get the SQL Statements to drop the given schema from underlying db.
80 * @param Schema $dropSchema
83 public function getDropSchema(Schema $dropSchema)
85 $visitor = new DropSchemaSqlCollector($this->platform);
86 $sm = $this->conn->getSchemaManager();
88 $fullSchema = $sm->createSchema();
90 foreach ($fullSchema->getTables() as $table) {
91 if ( $dropSchema->hasTable($table->getName())) {
92 $visitor->acceptTable($table);
95 foreach ($table->getForeignKeys() as $foreignKey) {
96 if ( ! $dropSchema->hasTable($table->getName())) {
100 if ( ! $dropSchema->hasTable($foreignKey->getForeignTableName())) {
104 $visitor->acceptForeignKey($table, $foreignKey);
108 if ( ! $this->platform->supportsSequences()) {
109 return $visitor->getQueries();
112 foreach ($dropSchema->getSequences() as $sequence) {
113 $visitor->acceptSequence($sequence);
116 foreach ($dropSchema->getTables() as $table) {
117 /* @var $sequence Table */
118 if ( ! $table->hasPrimaryKey()) {
122 $columns = $table->getPrimaryKey()->getColumns();
123 if (count($columns) > 1) {
127 $checkSequence = $table->getName() . "_" . $columns[0] . "_seq";
128 if ($fullSchema->hasSequence($checkSequence)) {
129 $visitor->acceptSequence($fullSchema->getSequence($checkSequence));
133 return $visitor->getQueries();
137 * Get the SQL statements to drop all schema assets from underlying db.
141 public function getDropAllSchema()
143 $sm = $this->conn->getSchemaManager();
144 $visitor = new DropSchemaSqlCollector($this->platform);
146 /* @var $schema \Doctrine\DBAL\Schema\Schema */
147 $schema = $sm->createSchema();
148 $schema->visit($visitor);
150 return $visitor->getQueries();
156 * @param Schema $createSchema
159 public function createSchema(Schema $createSchema)
161 $this->processSql($this->getCreateSchema($createSchema));
165 * Update the Schema to new schema version.
167 * @param Schema $toSchema
168 * @param bool $noDrops
171 public function updateSchema(Schema $toSchema, $noDrops = false)
173 $this->processSql($this->getUpdateSchema($toSchema, $noDrops));
177 * Drop the given database schema from the underlying db.
179 * @param Schema $dropSchema
182 public function dropSchema(Schema $dropSchema)
184 $this->processSqlSafely($this->getDropSchema($dropSchema));
188 * Drop all assets from the underyling db.
192 public function dropAllSchema()
194 $this->processSql($this->getDropAllSchema());