Example #1
0
 /**
  * @group DBAL-1013
  */
 public function testReturnsNewName()
 {
     $tableDiff = new TableDiff('foo');
     $this->assertFalse($tableDiff->getNewName());
     $tableDiff->newName = 'bar';
     $this->assertEquals(new Identifier('bar'), $tableDiff->getNewName());
 }
Example #2
0
 /**
  * {@inheritDoc}
  */
 public function getAlterTableSQL(TableDiff $diff)
 {
     $columnSql = array();
     $queryParts = array();
     if ($diff->newName !== false) {
         $queryParts[] = 'RENAME TO ' . $diff->getNewName()->getQuotedName($this);
     }
     foreach ($diff->addedColumns as $column) {
         if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
             continue;
         }
         $columnArray = $column->toArray();
         $columnArray['comment'] = $this->getColumnComment($column);
         $queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
     }
     foreach ($diff->removedColumns as $column) {
         if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
             continue;
         }
         $queryParts[] = 'DROP ' . $column->getQuotedName($this);
     }
     foreach ($diff->changedColumns as $columnDiff) {
         if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
             continue;
         }
         /* @var $columnDiff \Doctrine\DBAL\Schema\ColumnDiff */
         $column = $columnDiff->column;
         $columnArray = $column->toArray();
         // Don't propagate default value changes for unsupported column types.
         if ($columnDiff->hasChanged('default') && count($columnDiff->changedProperties) === 1 && ($columnArray['type'] instanceof TextType || $columnArray['type'] instanceof BlobType)) {
             continue;
         }
         $columnArray['comment'] = $this->getColumnComment($column);
         $queryParts[] = 'CHANGE ' . $columnDiff->getOldColumnName()->getQuotedName($this) . ' ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
     }
     foreach ($diff->renamedColumns as $oldColumnName => $column) {
         if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
             continue;
         }
         $oldColumnName = new Identifier($oldColumnName);
         $columnArray = $column->toArray();
         $columnArray['comment'] = $this->getColumnComment($column);
         $queryParts[] = 'CHANGE ' . $oldColumnName->getQuotedName($this) . ' ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
     }
     if (isset($diff->addedIndexes['primary'])) {
         $keyColumns = array_unique(array_values($diff->addedIndexes['primary']->getColumns()));
         $queryParts[] = 'ADD PRIMARY KEY (' . implode(', ', $keyColumns) . ')';
         unset($diff->addedIndexes['primary']);
     }
     $sql = array();
     $tableSql = array();
     if (!$this->onSchemaAlterTable($diff, $tableSql)) {
         if (count($queryParts) > 0) {
             $sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . implode(", ", $queryParts);
         }
         $sql = array_merge($this->getPreAlterTableIndexForeignKeySQL($diff), $sql, $this->getPostAlterTableIndexForeignKeySQL($diff));
     }
     return array_merge($sql, $tableSql, $columnSql);
 }
 /**
  * {@inheritDoc}
  */
 protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff)
 {
     if (!$diff->fromTable instanceof Table) {
         throw new DBALException('Sqlite platform requires for alter table the table diff with reference to original table schema');
     }
     $sql = array();
     $tableName = $diff->newName ? $diff->getNewName() : $diff->getName($this);
     foreach ($this->getIndexesInAlteredTable($diff) as $index) {
         if ($index->isPrimary()) {
             continue;
         }
         $sql[] = $this->getCreateIndexSQL($index, $tableName->getQuotedName($this));
     }
     return $sql;
 }
Example #4
0
 /**
  * @param \Doctrine\DBAL\Schema\TableDiff $diff
  *
  * @return array
  */
 protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff)
 {
     $tableName = false !== $diff->newName ? $diff->getNewName()->getQuotedName($this) : $diff->getName()->getQuotedName($this);
     $sql = array();
     if ($this->supportsForeignKeyConstraints()) {
         foreach ($diff->addedForeignKeys as $foreignKey) {
             $sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName);
         }
         foreach ($diff->changedForeignKeys as $foreignKey) {
             $sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName);
         }
     }
     foreach ($diff->addedIndexes as $index) {
         $sql[] = $this->getCreateIndexSQL($index, $tableName);
     }
     foreach ($diff->changedIndexes as $index) {
         $sql[] = $this->getCreateIndexSQL($index, $tableName);
     }
     foreach ($diff->renamedIndexes as $oldIndexName => $index) {
         $oldIndexName = new Identifier($oldIndexName);
         $sql = array_merge($sql, $this->getRenameIndexSQL($oldIndexName->getQuotedName($this), $index, $tableName));
     }
     return $sql;
 }
 /**
  * @param TableDiff $diff The table diff to gather the SQL for.
  *
  * @return array
  */
 protected function getPostAlterTableRenameIndexForeignKeySQL(TableDiff $diff)
 {
     $sql = array();
     $tableName = false !== $diff->newName ? $diff->getNewName()->getQuotedName($this) : $diff->getName($this)->getQuotedName($this);
     foreach ($this->getRemainingForeignKeyConstraintsRequiringRenamedIndexes($diff) as $foreignKey) {
         if (!in_array($foreignKey, $diff->changedForeignKeys, true)) {
             $sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName);
         }
     }
     return $sql;
 }
 /**
  * {@inheritDoc}
  */
 public function getAlterTableSQL(TableDiff $diff)
 {
     $columnSql = array();
     $queryParts = array();
     if ($diff->newName !== false) {
         $queryParts[] = 'RENAME TO ' . $diff->getNewName()->getQuotedName($this);
     }
     foreach ($diff->addedColumns as $column) {
         if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
             continue;
         }
         $columnArray = $column->toArray();
         $columnArray['comment'] = $this->getColumnComment($column);
         $queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
     }
     foreach ($diff->removedColumns as $column) {
         if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
             continue;
         }
         $queryParts[] = 'DROP ' . $column->getQuotedName($this);
     }
     foreach ($diff->changedColumns as $columnDiff) {
         if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
             continue;
         }
         /* @var $columnDiff \Doctrine\DBAL\Schema\ColumnDiff */
         $column = $columnDiff->column;
         $columnArray = $column->toArray();
         // Do not generate column alteration clause if type is binary and only fixed property has changed.
         // Drizzle only supports binary type columns with variable length.
         // Avoids unnecessary table alteration statements.
         if ($columnArray['type'] instanceof BinaryType && $columnDiff->hasChanged('fixed') && count($columnDiff->changedProperties) === 1) {
             continue;
         }
         $columnArray['comment'] = $this->getColumnComment($column);
         $queryParts[] = 'CHANGE ' . $columnDiff->getOldColumnName()->getQuotedName($this) . ' ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
     }
     foreach ($diff->renamedColumns as $oldColumnName => $column) {
         if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
             continue;
         }
         $oldColumnName = new Identifier($oldColumnName);
         $columnArray = $column->toArray();
         $columnArray['comment'] = $this->getColumnComment($column);
         $queryParts[] = 'CHANGE ' . $oldColumnName->getQuotedName($this) . ' ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
     }
     $sql = array();
     $tableSql = array();
     if (!$this->onSchemaAlterTable($diff, $tableSql)) {
         if (count($queryParts) > 0) {
             $sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . implode(", ", $queryParts);
         }
         $sql = array_merge($this->getPreAlterTableIndexForeignKeySQL($diff), $sql, $this->getPostAlterTableIndexForeignKeySQL($diff));
     }
     return array_merge($sql, $tableSql, $columnSql);
 }
Example #7
0
 /**
  * {@inheritDoc}
  */
 protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff)
 {
     $tableName = false !== $diff->newName ? $diff->getNewName()->getQuotedName($this) : $diff->getName($this)->getQuotedName($this);
     $sql = array();
     foreach ($diff->addedIndexes as $index) {
         $sql[] = $this->getCreateIndexSQL($index, $tableName);
     }
     $diff->addedIndexes = array();
     foreach ($diff->changedIndexes as $index) {
         $sql[] = $this->getCreateIndexSQL($index, $tableName);
     }
     $diff->changedIndexes = array();
     $sql = array_merge($sql, parent::getPostAlterTableIndexForeignKeySQL($diff));
     return $sql;
 }