/**
  * @group DDC-887
  */
 public function testUpdateSchemaWithForeignKeyRenaming()
 {
     if (!$this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
         $this->markTestSkipped('This test is only supported on platforms that have foreign keys.');
     }
     $table = new \Doctrine\DBAL\Schema\Table('test_fk_base');
     $table->addColumn('id', 'integer');
     $table->setPrimaryKey(array('id'));
     $tableFK = new \Doctrine\DBAL\Schema\Table('test_fk_rename');
     $tableFK->setSchemaConfig($this->_sm->createSchemaConfig());
     $tableFK->addColumn('id', 'integer');
     $tableFK->addColumn('fk_id', 'integer');
     $tableFK->setPrimaryKey(array('id'));
     $tableFK->addIndex(array('fk_id'), 'fk_idx');
     $tableFK->addForeignKeyConstraint('test_fk_base', array('fk_id'), array('id'));
     $this->_sm->createTable($table);
     $this->_sm->createTable($tableFK);
     $tableFKNew = new \Doctrine\DBAL\Schema\Table('test_fk_rename');
     $tableFKNew->setSchemaConfig($this->_sm->createSchemaConfig());
     $tableFKNew->addColumn('id', 'integer');
     $tableFKNew->addColumn('rename_fk_id', 'integer');
     $tableFKNew->setPrimaryKey(array('id'));
     $tableFKNew->addIndex(array('rename_fk_id'), 'fk_idx');
     $tableFKNew->addForeignKeyConstraint('test_fk_base', array('rename_fk_id'), array('id'));
     $c = new \Doctrine\DBAL\Schema\Comparator();
     $tableDiff = $c->diffTable($tableFK, $tableFKNew);
     $this->_sm->alterTable($tableDiff);
 }
Пример #2
0
 /**
  * Removes a field from a model. Usually involves deleting a column, but for M2Ms may involve deleting a table.
  *
  * @param Model $model
  * @param Field $field
  *
  * @since 1.1.0
  *
  * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**>
  */
 public function removeField($model, $field)
 {
     $schema = $this->schemaManager->createSchema();
     // Special-case implicit M2M tables
     if ($field->manyToMany && $field->relation->through->meta->autoCreated) {
         $this->deleteModel($field->relation->through);
         return;
     }
     $type = $field->dbType($this->connection);
     $name = $field->getColumnName();
     $fieldOptions = $this->getDoctrineColumnOptions($field);
     // It might not actually have a column behind it
     if (empty($type)) {
         return;
     }
     $table = $model->meta->dbTable;
     $tableDef = clone $schema->getTable($table);
     // Drop any FK constraints, MySQL requires explicit deletion
     if ($field->isRelation && $field->relation !== null) {
         foreach ($this->constraintName($table, $name, ['foreignKey' => true]) as $fkConstraint) {
             $tableDef->removeForeignKey($fkConstraint);
         }
     }
     // remove column
     $tableDef->dropColumn($name);
     $comparator = new Comparator();
     $diff = $comparator->diffTable($schema->getTable($table), $tableDef);
     if ($diff !== false) {
         $this->schemaManager->alterTable($diff);
     }
 }
 /**
  * @group DBAL-1009
  *
  * @dataProvider getAlterColumnComment
  */
 public function testAlterColumnComment($comment1, $expectedComment1, $comment2, $expectedComment2)
 {
     if (!$this->_conn->getDatabasePlatform()->supportsInlineColumnComments() && !$this->_conn->getDatabasePlatform()->supportsCommentOnStatement() && $this->_conn->getDatabasePlatform()->getName() != 'mssql') {
         $this->markTestSkipped('Database does not support column comments.');
     }
     $offlineTable = new Table('alter_column_comment_test');
     $offlineTable->addColumn('comment1', 'integer', array('comment' => $comment1));
     $offlineTable->addColumn('comment2', 'integer', array('comment' => $comment2));
     $offlineTable->addColumn('no_comment1', 'integer');
     $offlineTable->addColumn('no_comment2', 'integer');
     $this->_sm->dropAndCreateTable($offlineTable);
     $onlineTable = $this->_sm->listTableDetails("alter_column_comment_test");
     $this->assertSame($expectedComment1, $onlineTable->getColumn('comment1')->getComment());
     $this->assertSame($expectedComment2, $onlineTable->getColumn('comment2')->getComment());
     $this->assertNull($onlineTable->getColumn('no_comment1')->getComment());
     $this->assertNull($onlineTable->getColumn('no_comment2')->getComment());
     $onlineTable->changeColumn('comment1', array('comment' => $comment2));
     $onlineTable->changeColumn('comment2', array('comment' => $comment1));
     $onlineTable->changeColumn('no_comment1', array('comment' => $comment1));
     $onlineTable->changeColumn('no_comment2', array('comment' => $comment2));
     $comparator = new Comparator();
     $tableDiff = $comparator->diffTable($offlineTable, $onlineTable);
     $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\TableDiff', $tableDiff);
     $this->_sm->alterTable($tableDiff);
     $onlineTable = $this->_sm->listTableDetails("alter_column_comment_test");
     $this->assertSame($expectedComment2, $onlineTable->getColumn('comment1')->getComment());
     $this->assertSame($expectedComment1, $onlineTable->getColumn('comment2')->getComment());
     $this->assertSame($expectedComment1, $onlineTable->getColumn('no_comment1')->getComment());
     $this->assertSame($expectedComment2, $onlineTable->getColumn('no_comment2')->getComment());
 }
Пример #4
0
 /**
  * Computes and executes the changes
  *
  * @return void
  */
 public function executeChanges()
 {
     //create a table diff
     $comparator = new Comparator();
     $diff = $comparator->diffTable($this->fromTable, $this->toTable);
     if ($diff) {
         $this->sm->alterTable($diff);
     }
 }
 public function testAlterTableThrowsExceptionForChangedSpatialType()
 {
     $this->setExpectedException('\\RuntimeException', 'The geometry_type of a spatial column cannot be changed (Requested changing type from "POINT" to "LINESTRING" for column "point_2d" in table "points")');
     $table = $this->sm->listTableDetails('points');
     $tableDiff = new \Doctrine\DBAL\Schema\TableDiff('points');
     $tableDiff->fromTable = $table;
     $tableDiff->changedColumns[] = new ColumnDiff('point_2d', new \Doctrine\DBAL\Schema\Column('point_2d', Type::getType('geometry'), array('customSchemaOptions' => array('geometry_type' => 'LINESTRING'))), array('geometry_type'), $table->getColumn('point_2d'));
     $this->sm->alterTable($tableDiff);
 }
 /**
  * {@inheritdoc}
  */
 public function alterTable(TableDiff $tableDiff)
 {
     if (count($tableDiff->removedColumns) > 0) {
         foreach ($tableDiff->removedColumns as $col) {
             $columnConstraintSql = $this->getColumnConstraintSQL($tableDiff->name, $col->getName());
             foreach ($this->_conn->fetchAll($columnConstraintSql) as $constraint) {
                 $this->_conn->exec("ALTER TABLE {$tableDiff->name} DROP CONSTRAINT " . $constraint['Name']);
             }
         }
     }
     parent::alterTable($tableDiff);
 }
 public function testAlterTableScenario()
 {
     if (!$this->_sm->getDatabasePlatform()->supportsAlterTable()) {
         $this->markTestSkipped('Alter Table is not supported by this platform.');
     }
     $this->createTestTable('alter_table');
     $this->createTestTable('alter_table_foreign');
     $table = $this->_sm->listTableDetails('alter_table');
     $this->assertTrue($table->hasColumn('id'));
     $this->assertTrue($table->hasColumn('test'));
     $this->assertTrue($table->hasColumn('foreign_key_test'));
     $this->assertEquals(0, count($table->getForeignKeys()));
     $this->assertEquals(1, count($table->getIndexes()));
     $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
     $tableDiff->addedColumns['foo'] = new \Doctrine\DBAL\Schema\Column('foo', Type::getType('integer'));
     $tableDiff->removedColumns['test'] = $table->getColumn('test');
     $this->_sm->alterTable($tableDiff);
     $table = $this->_sm->listTableDetails('alter_table');
     $this->assertFalse($table->hasColumn('test'));
     $this->assertTrue($table->hasColumn('foo'));
     $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
     $tableDiff->addedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo'));
     $this->_sm->alterTable($tableDiff);
     $table = $this->_sm->listTableDetails('alter_table');
     $this->assertEquals(2, count($table->getIndexes()));
     $this->assertTrue($table->hasIndex('foo_idx'));
     $this->assertEquals(array('foo'), array_map('strtolower', $table->getIndex('foo_idx')->getColumns()));
     $this->assertFalse($table->getIndex('foo_idx')->isPrimary());
     $this->assertFalse($table->getIndex('foo_idx')->isUnique());
     $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
     $tableDiff->changedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo', 'foreign_key_test'));
     $this->_sm->alterTable($tableDiff);
     $table = $this->_sm->listTableDetails('alter_table');
     $this->assertEquals(2, count($table->getIndexes()));
     $this->assertTrue($table->hasIndex('foo_idx'));
     $this->assertEquals(array('foo', 'foreign_key_test'), array_map('strtolower', $table->getIndex('foo_idx')->getColumns()));
     $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
     $tableDiff->removedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo', 'foreign_key_test'));
     $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('foreign_key_test'), 'alter_table_foreign', array('id'));
     $tableDiff->addedForeignKeys[] = $fk;
     $this->_sm->alterTable($tableDiff);
     $table = $this->_sm->listTableDetails('alter_table');
     // dont check for index size here, some platforms automatically add indexes for foreign keys.
     $this->assertFalse($table->hasIndex('foo_idx'));
     $this->assertEquals(1, count($table->getForeignKeys()));
     $fks = $table->getForeignKeys();
     $foreignKey = current($fks);
     $this->assertEquals('alter_table_foreign', strtolower($foreignKey->getForeignTableName()));
     $this->assertEquals(array('foreign_key_test'), array_map('strtolower', $foreignKey->getColumns()));
     $this->assertEquals(array('id'), array_map('strtolower', $foreignKey->getForeignColumns()));
 }
 /**
  * @group DBAL-44
  */
 public function testColumnDefaultLifecycle()
 {
     $table = new Table("col_def_lifecycle");
     $table->addColumn('id', 'integer', array('primary' => true, 'autoincrement' => true));
     $table->addColumn('column1', 'string', array('default' => null));
     $table->addColumn('column2', 'string', array('default' => false));
     $table->addColumn('column3', 'string', array('default' => true));
     $table->addColumn('column4', 'string', array('default' => 0));
     $table->addColumn('column5', 'string', array('default' => ''));
     $table->addColumn('column6', 'string', array('default' => 'def'));
     $table->setPrimaryKey(array('id'));
     $this->_sm->dropAndCreateTable($table);
     $columns = $this->_sm->listTableColumns('col_def_lifecycle');
     $this->assertNull($columns['id']->getDefault());
     $this->assertNull($columns['column1']->getDefault());
     $this->assertSame('', $columns['column2']->getDefault());
     $this->assertSame('1', $columns['column3']->getDefault());
     $this->assertSame('0', $columns['column4']->getDefault());
     $this->assertSame('', $columns['column5']->getDefault());
     $this->assertSame('def', $columns['column6']->getDefault());
     $diffTable = clone $table;
     $diffTable->changeColumn('column1', array('default' => false));
     $diffTable->changeColumn('column2', array('default' => null));
     $diffTable->changeColumn('column3', array('default' => false));
     $diffTable->changeColumn('column4', array('default' => null));
     $diffTable->changeColumn('column5', array('default' => false));
     $diffTable->changeColumn('column6', array('default' => 666));
     $comparator = new Comparator();
     $this->_sm->alterTable($comparator->diffTable($table, $diffTable));
     $columns = $this->_sm->listTableColumns('col_def_lifecycle');
     $this->assertSame('', $columns['column1']->getDefault());
     $this->assertNull($columns['column2']->getDefault());
     $this->assertSame('', $columns['column3']->getDefault());
     $this->assertNull($columns['column4']->getDefault());
     $this->assertSame('', $columns['column5']->getDefault());
     $this->assertSame('666', $columns['column6']->getDefault());
 }