Example #1
0
 /**
  * Check if this sequence is an autoincrement sequence for a given table.
  *
  * This is used inside the comparator to not report sequences as missing,
  * when the "from" schema implicitly creates the sequences.
  *
  * @param Table $table
  *
  * @return bool
  */
 public function isAutoIncrementsFor(Table $table)
 {
     if (!$table->hasPrimaryKey()) {
         return false;
     }
     $pkColumns = $table->getPrimaryKey()->getColumns();
     if (count($pkColumns) != 1) {
         return false;
     }
     $column = $table->getColumn($pkColumns[0]);
     if (!$column->getAutoincrement()) {
         return false;
     }
     $sequenceName = $this->getShortestName($table->getNamespaceName());
     $tableName = $table->getShortestName($table->getNamespaceName());
     $tableSequenceName = sprintf('%s_%s_seq', $tableName, $pkColumns[0]);
     return $tableSequenceName === $sequenceName;
 }
Example #2
0
 /**
  * @param \Doctrine\DBAL\Schema\Table $table
  *
  * @return string
  */
 private function createTableLabel(Table $table)
 {
     // Start the table
     $label = '<<TABLE CELLSPACING="0" BORDER="1" ALIGN="LEFT">';
     // The title
     $label .= '<TR><TD BORDER="1" COLSPAN="3" ALIGN="CENTER" BGCOLOR="#fcaf3e"><FONT COLOR="#2e3436" FACE="Helvetica" POINT-SIZE="12">' . $table->getName() . '</FONT></TD></TR>';
     // The attributes block
     foreach ($table->getColumns() as $column) {
         $columnLabel = $column->getName();
         $label .= '<TR>';
         $label .= '<TD BORDER="0" ALIGN="LEFT" BGCOLOR="#eeeeec">';
         $label .= '<FONT COLOR="#2e3436" FACE="Helvetica" POINT-SIZE="12">' . $columnLabel . '</FONT>';
         $label .= '</TD><TD BORDER="0" ALIGN="LEFT" BGCOLOR="#eeeeec"><FONT COLOR="#2e3436" FACE="Helvetica" POINT-SIZE="10">' . strtolower($column->getType()) . '</FONT></TD>';
         $label .= '<TD BORDER="0" ALIGN="RIGHT" BGCOLOR="#eeeeec" PORT="col' . $column->getName() . '">';
         if ($table->hasPrimaryKey() && in_array($column->getName(), $table->getPrimaryKey()->getColumns())) {
             $label .= "✷";
         }
         $label .= '</TD></TR>';
     }
     // End the table
     $label .= '</TABLE>>';
     return $label;
 }
 /**
  * @group DBAL-79
  */
 public function testTableHasPrimaryKey()
 {
     $table = new Table("test");
     $this->assertFalse($table->hasPrimaryKey());
     $table->addColumn("foo", "integer");
     $table->setPrimaryKey(array("foo"));
     $this->assertTrue($table->hasPrimaryKey());
 }
Example #4
0
 /**
  * @param \Doctrine\DBAL\Schema\Table $table
  * @param \SimpleXMLElement $xml
  * @throws \DomainException
  */
 private function loadIndex($table, $xml)
 {
     $name = null;
     $fields = array();
     foreach ($xml->children() as $child) {
         /**
          * @var \SimpleXMLElement $child
          */
         switch ($child->getName()) {
             case 'name':
                 $name = (string) $child;
                 break;
             case 'primary':
                 $primary = $this->asBool($child);
                 break;
             case 'unique':
                 $unique = $this->asBool($child);
                 break;
             case 'field':
                 foreach ($child->children() as $field) {
                     /**
                      * @var \SimpleXMLElement $field
                      */
                     switch ($field->getName()) {
                         case 'name':
                             $field_name = (string) $field;
                             $field_name = $this->platform->quoteIdentifier($field_name);
                             $fields[] = $field_name;
                             break;
                         case 'sorting':
                             break;
                         default:
                             throw new \DomainException('Unknown element: ' . $field->getName());
                     }
                 }
                 break;
             default:
                 throw new \DomainException('Unknown element: ' . $child->getName());
         }
     }
     if (!empty($fields)) {
         if (isset($primary) && $primary) {
             if ($table->hasPrimaryKey()) {
                 return;
             }
             $table->setPrimaryKey($fields, $name);
         } else {
             if (isset($unique) && $unique) {
                 $table->addUniqueIndex($fields, $name);
             } else {
                 $table->addIndex($fields, $name);
             }
         }
     } else {
         throw new \DomainException('Empty index definition: ' . $name . ' options:' . print_r($fields, true));
     }
 }
 /**
  * @group DBAL-224
  */
 public function testDropPrimaryKey()
 {
     $table = new Table("test");
     $table->addColumn('id', 'integer');
     $table->setPrimaryKey(array('id'));
     $this->assertTrue($table->hasPrimaryKey());
     $table->dropPrimaryKey();
     $this->assertFalse($table->hasPrimaryKey());
 }
Example #6
0
 /**
  * @param Table $table
  * @return string
  * @throws SchemaException if valid primary key does not exist
  */
 protected function getPrimaryKeyColumnName(Table $table)
 {
     if (!$table->hasPrimaryKey()) {
         throw new SchemaException(sprintf('The table "%s" must have a primary key.', $table->getName()));
     }
     $primaryKeyColumns = $table->getPrimaryKey()->getColumns();
     if (count($primaryKeyColumns) !== 1) {
         throw new SchemaException(sprintf('A primary key of "%s" table must include only one column.', $table->getName()));
     }
     return array_pop($primaryKeyColumns);
 }
Example #7
0
 /**
  * Returns the difference between the tables $table1 and $table2.
  *
  * If there are no differences this method returns the boolean false.
  *
  * @param \Doctrine\DBAL\Schema\Table $table1
  * @param \Doctrine\DBAL\Schema\Table $table2
  *
  * @return boolean|\Doctrine\DBAL\Schema\TableDiff
  */
 public function diffTable(Table $table1, Table $table2)
 {
     $changes = 0;
     $tableDifferences = new TableDiff($table1->getName());
     $tableDifferences->fromTable = $table1;
     $table1Columns = $table1->getColumns();
     $table2Columns = $table2->getColumns();
     /* See if all the fields in table 1 exist in table 2 */
     foreach ($table2Columns as $columnName => $column) {
         if (!$table1->hasColumn($columnName)) {
             $tableDifferences->addedColumns[$columnName] = $column;
             $changes++;
         }
     }
     /* See if there are any removed fields in table 2 */
     foreach ($table1Columns as $columnName => $column) {
         // See if column is removed in table 2.
         if (!$table2->hasColumn($columnName)) {
             $tableDifferences->removedColumns[$columnName] = $column;
             $changes++;
             continue;
         }
         // See if column has changed properties in table 2.
         $changedProperties = $this->diffColumn($column, $table2->getColumn($columnName));
         if (!empty($changedProperties)) {
             $columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties);
             $columnDiff->fromColumn = $column;
             $tableDifferences->changedColumns[$column->getName()] = $columnDiff;
             $changes++;
         }
     }
     $this->detectColumnRenamings($tableDifferences);
     $table1Indexes = $table1->getIndexes();
     $table2Indexes = $table2->getIndexes();
     /* See if all the indexes in table 1 exist in table 2 */
     foreach ($table2Indexes as $indexName => $index) {
         if ($index->isPrimary() && $table1->hasPrimaryKey() || $table1->hasIndex($indexName)) {
             continue;
         }
         $tableDifferences->addedIndexes[$indexName] = $index;
         $changes++;
     }
     /* See if there are any removed indexes in table 2 */
     foreach ($table1Indexes as $indexName => $index) {
         // See if index is removed in table 2.
         if ($index->isPrimary() && !$table2->hasPrimaryKey() || !$index->isPrimary() && !$table2->hasIndex($indexName)) {
             $tableDifferences->removedIndexes[$indexName] = $index;
             $changes++;
             continue;
         }
         // See if index has changed in table 2.
         $table2Index = $index->isPrimary() ? $table2->getPrimaryKey() : $table2->getIndex($indexName);
         if ($this->diffIndex($index, $table2Index)) {
             $tableDifferences->changedIndexes[$indexName] = $table2Index;
             $changes++;
         }
     }
     $this->detectIndexRenamings($tableDifferences);
     $fromFkeys = $table1->getForeignKeys();
     $toFkeys = $table2->getForeignKeys();
     foreach ($fromFkeys as $key1 => $constraint1) {
         foreach ($toFkeys as $key2 => $constraint2) {
             if ($this->diffForeignKey($constraint1, $constraint2) === false) {
                 unset($fromFkeys[$key1]);
                 unset($toFkeys[$key2]);
             } else {
                 if (strtolower($constraint1->getName()) == strtolower($constraint2->getName())) {
                     $tableDifferences->changedForeignKeys[] = $constraint2;
                     $changes++;
                     unset($fromFkeys[$key1]);
                     unset($toFkeys[$key2]);
                 }
             }
         }
     }
     foreach ($fromFkeys as $constraint1) {
         $tableDifferences->removedForeignKeys[] = $constraint1;
         $changes++;
     }
     foreach ($toFkeys as $constraint2) {
         $tableDifferences->addedForeignKeys[] = $constraint2;
         $changes++;
     }
     return $changes ? $tableDifferences : false;
 }
Example #8
0
 /**
  * Assert the table has a primary key.
  *
  * @param \Doctrine\DBAL\Schema\Table $table
  * @return $this
  */
 protected function tableHasPrimary()
 {
     $this->assertTrue($this->table->hasPrimaryKey(), "The table {$this->table->getName()} does not have a primary key.");
     return $this->table;
 }