예제 #1
0
 /**
  * Gets the alter table column SQL queries.
  * @param \UniversalDb\Common\Schema\Diff\ColumnDiff $columnDiff The column diff.
  * @param string                                     $table      The table name.
  * @return array The alter table column SQL queries.
  */
 public function getAlterColumnSQLQueries(ColumnDiff $columnDiff, $table) : array
 {
     return [$this->getAlterTableSQLQuery($table, 'CHANGE COLUMN', $columnDiff->getOldAsset()->getName() . ' ' . $this->getColumnSQLDeclaration($columnDiff->getNewAsset()))];
 }
예제 #2
0
 /**
  * Gets the alter table column SQL queries.
  * @param \UniversalDb\Common\Schema\Diff\ColumnDiff $columnDiff The column diff.
  * @param string                                     $table      The table name.
  * @return array The alter table column SQL queries.
  */
 public function getAlterColumnSQLQueries(ColumnDiff $columnDiff, $table) : array
 {
     $queries = [];
     if ($columnDiff->getOldAsset()->getName() !== $columnDiff->getNewAsset()->getName()) {
         $queries[] = $this->getAlterTableSQLQuery($table, 'RENAME COLUMN', $columnDiff->getOldAsset()->getName() . ' TO ' . $columnDiff->getNewAsset()->getName());
     }
     $alterColumnSQLQuerySnippet = $this->getAlterTableSQLQuery($table, 'ALTER COLUMN', $columnDiff->getNewAsset()->getName());
     if (in_array('type', $columnDiff->getDifferences()) || in_array('length', $columnDiff->getDifferences()) || in_array('precision', $columnDiff->getDifferences()) || in_array('scale', $columnDiff->getDifferences())) {
         $typeDeclaration = $columnDiff->getNewAsset()->getType()->getSQLDeclaration($this, $columnDiff->getNewAsset()->toArray());
         $queries[] = $alterColumnSQLQuerySnippet . ' TYPE ' . $typeDeclaration;
     }
     if (in_array('not_null', $columnDiff->getDifferences())) {
         if ($columnDiff->getNewAsset()->isNotNull()) {
             $notNullDeclaration = ' SET NOT NULL';
         } else {
             $notNullDeclaration = ' DROP NOT NULL';
         }
         $queries[] = $alterColumnSQLQuerySnippet . $notNullDeclaration;
     }
     if (in_array('default', $columnDiff->getDifferences())) {
         if ($columnDiff->getNewAsset()->getDefault() !== null) {
             $defaultDeclaration = ' SET DEFAULT ' . $this->quote($columnDiff->getNewAsset()->getDefault());
         } else {
             $defaultDeclaration = ' DROP DEFAULT';
         }
         $queries[] = $alterColumnSQLQuerySnippet . $defaultDeclaration;
     }
     if (in_array('comment', $columnDiff->getDifferences()) || in_array('type', $columnDiff->getDifferences()) && $this->hasCustomType($columnDiff->getNewAsset()->getType()->getName())) {
         $queries[] = $this->getCreateColumnCommentSQLQuery($columnDiff->getNewAsset(), $table);
     }
     return $queries;
 }