/**
  * Get the SQL Statements to drop the given schema from underlying db.
  *
  * @param Schema $dropSchema
  * @return array
  */
 public function getDropSchema(Schema $dropSchema)
 {
     $visitor = new DropSchemaSqlCollector($this->platform);
     $sm = $this->conn->getSchemaManager();
     $fullSchema = $sm->createSchema();
     foreach ($fullSchema->getTables() as $table) {
         if ($dropSchema->hasTable($table->getName())) {
             $visitor->acceptTable($table);
         }
         foreach ($table->getForeignKeys() as $foreignKey) {
             if (!$dropSchema->hasTable($table->getName())) {
                 continue;
             }
             if (!$dropSchema->hasTable($foreignKey->getForeignTableName())) {
                 continue;
             }
             $visitor->acceptForeignKey($table, $foreignKey);
         }
     }
     if (!$this->platform->supportsSequences()) {
         return $visitor->getQueries();
     }
     foreach ($dropSchema->getSequences() as $sequence) {
         $visitor->acceptSequence($sequence);
     }
     foreach ($dropSchema->getTables() as $table) {
         /* @var $sequence Table */
         if (!$table->hasPrimaryKey()) {
             continue;
         }
         $columns = $table->getPrimaryKey()->getColumns();
         if (count($columns) > 1) {
             continue;
         }
         $checkSequence = $table->getName() . "_" . $columns[0] . "_seq";
         if ($fullSchema->hasSequence($checkSequence)) {
             $visitor->acceptSequence($fullSchema->getSequence($checkSequence));
         }
     }
     return $visitor->getQueries();
 }
Ejemplo n.º 2
0
 /**
  * Gets SQL to drop the tables defined by the passed classes.
  *
  * @param array $classes
  *
  * @return array
  */
 public function getDropSchemaSQL(array $classes)
 {
     $visitor = new DropSchemaSqlCollector($this->platform);
     $schema = $this->getSchemaFromMetadata($classes);
     $sm = $this->em->getConnection()->getSchemaManager();
     $fullSchema = $sm->createSchema();
     foreach ($fullSchema->getTables() as $table) {
         if (!$schema->hasTable($table->getName())) {
             foreach ($table->getForeignKeys() as $foreignKey) {
                 /* @var $foreignKey \Doctrine\DBAL\Schema\ForeignKeyConstraint */
                 if ($schema->hasTable($foreignKey->getForeignTableName())) {
                     $visitor->acceptForeignKey($table, $foreignKey);
                 }
             }
         } else {
             $visitor->acceptTable($table);
             foreach ($table->getForeignKeys() as $foreignKey) {
                 $visitor->acceptForeignKey($table, $foreignKey);
             }
         }
     }
     if ($this->platform->supportsSequences()) {
         foreach ($schema->getSequences() as $sequence) {
             $visitor->acceptSequence($sequence);
         }
         foreach ($schema->getTables() as $table) {
             /* @var $sequence Table */
             if ($table->hasPrimaryKey()) {
                 $columns = $table->getPrimaryKey()->getColumns();
                 if (count($columns) == 1) {
                     $checkSequence = $table->getName() . "_" . $columns[0] . "_seq";
                     if ($fullSchema->hasSequence($checkSequence)) {
                         $visitor->acceptSequence($fullSchema->getSequence($checkSequence));
                     }
                 }
             }
         }
     }
     return $visitor->getQueries();
 }
Ejemplo n.º 3
0
 /**
  * Drop all tables.
  *
  * @param array $metadata
  * @return array
  */
 public function drop(array $metadata)
 {
     $visitor = new DropSchemaSqlCollector($this->platform);
     $schema = $this->getSchemaFromMetadata($metadata);
     $fullSchema = $this->schemaManager->createSchema();
     foreach ($fullSchema->getTables() as $table) {
         if ($schema->hasTable($table->getName())) {
             $visitor->acceptTable($table);
         }
     }
     $statements = $visitor->getQueries();
     $this->build($statements);
     return $statements;
 }