/**
  * Generates code for creating or updating a database table.
  * @param Doctrine\DBAL\Schema\Table $updatedTable Specifies the updated table schema.
  * @param Doctrine\DBAL\Schema\Table $existingTable Specifies the existing table schema, if applicable.
  * @param string $newTableName An updated name of the theme. 
  * @return string|boolean Returns the migration up() and down() methods code. 
  * Returns false if there the table was not changed.
  */
 public function createOrUpdateTable($updatedTable, $existingTable, $newTableName)
 {
     $tableDiff = false;
     if ($existingTable !== null) {
         // The table already exists
         //
         $comparator = new Comparator();
         $tableDiff = $comparator->diffTable($existingTable, $updatedTable);
         if ($newTableName !== $existingTable->getName()) {
             if (!$tableDiff) {
                 $tableDiff = new TableDiff($existingTable->getName());
             }
             $tableDiff->newName = $newTableName;
         }
     } else {
         // The table doesn't exist
         //
         $tableDiff = new TableDiff($updatedTable->getName(), $updatedTable->getColumns(), [], [], $updatedTable->getIndexes());
         $tableDiff->fromTable = $updatedTable;
     }
     if (!$tableDiff) {
         return false;
     }
     if (!$this->tableHasNameOrColumnChanges($tableDiff) && !$this->tableHasPrimaryKeyChanges($tableDiff)) {
         return false;
     }
     return $this->generateCreateOrUpdateCode($tableDiff, !$existingTable, $updatedTable);
 }
 public function getSchemaDiff()
 {
     $diff = new SchemaDiff();
     $comparator = new Comparator();
     $tableDiff = $comparator->diffTable($this->conn->getSchemaManager()->createSchema()->getTable($this->tableName), $this->schema->getTable($this->tableName));
     if (false !== $tableDiff) {
         $diff->changedTables[$this->tableName] = $tableDiff;
     }
     return $diff;
 }
Exemplo n.º 3
0
 public function testSearchPathSchemaChanges()
 {
     $table = new Table("dbal510tbl");
     $table->addColumn('id', 'integer');
     $table->setPrimaryKey(array('id'));
     $this->_conn->getSchemaManager()->createTable($table);
     $onlineTable = $this->_conn->getSchemaManager()->listTableDetails('dbal510tbl');
     $comparator = new Comparator();
     $diff = $comparator->diffTable($onlineTable, $table);
     $this->assertFalse($diff);
 }
Exemplo n.º 4
0
 public function up(Schema $schema)
 {
     \Database::query('UPDATE Config SET configNamespace="" WHERE configNamespace IS NULL');
     $config = $schema->getTable('Config');
     $fromConfig = clone $config;
     $db = \Database::get();
     $platform = $db->getDatabasePlatform();
     $config->dropPrimaryKey();
     $config->setPrimaryKey(array('configNamespace', 'configGroup', 'configItem'));
     $comparator = new Comparator();
     $diff = $comparator->diffTable($fromConfig, $config);
     $sql = $platform->getAlterTableSQL($diff);
     if (is_array($sql) && count($sql)) {
         foreach ($sql as $q) {
             $db->query($q);
         }
     }
 }
Exemplo n.º 5
0
 /**
  * Returns the difference between the tables $fromTable and $toTable.
  *
  * If there are no differences this method returns the boolean false.
  *
  * @param \Doctrine\DBAL\Schema\Table $fromTable
  * @param \Doctrine\DBAL\Schema\Table $toTable
  * @return bool|\Doctrine\DBAL\Schema\TableDiff|\TYPO3\CMS\Core\Database\Schema\TableDiff
  * @throws \InvalidArgumentException
  */
 public function diffTable(Table $fromTable, Table $toTable)
 {
     $newTableOptions = array_merge($fromTable->getOptions(), $toTable->getOptions());
     $optionDiff = array_diff_assoc($newTableOptions, $fromTable->getOptions());
     $tableDifferences = parent::diffTable($fromTable, $toTable);
     // No changed table options, return parent result
     if (count($optionDiff) === 0) {
         return $tableDifferences;
     }
     if ($tableDifferences === false) {
         $tableDifferences = GeneralUtility::makeInstance(TableDiff::class, $fromTable->getName());
         $tableDifferences->fromTable = $fromTable;
     } else {
         // Rebuild TableDiff with enhanced TYPO3 TableDiff class
         $tableDifferences = GeneralUtility::makeInstance(TableDiff::class, $tableDifferences->name, $tableDifferences->addedColumns, $tableDifferences->changedColumns, $tableDifferences->removedColumns, $tableDifferences->addedIndexes, $tableDifferences->changedIndexes, $tableDifferences->removedIndexes, $tableDifferences->fromTable);
     }
     // Set the table options to be parsed in the AlterTable event.
     $tableDifferences->setTableOptions($optionDiff);
     return $tableDifferences;
 }
 /**
  * @group DBAL-472
  * @group DBAL-1001
  */
 public function testAlterTableColumnNotNull()
 {
     $comparator = new Schema\Comparator();
     $tableName = 'list_table_column_notnull';
     $table = new Schema\Table($tableName);
     $table->addColumn('id', 'integer');
     $table->addColumn('foo', 'integer');
     $table->addColumn('bar', 'string');
     $table->setPrimaryKey(array('id'));
     $this->_sm->dropAndCreateTable($table);
     $columns = $this->_sm->listTableColumns($tableName);
     $this->assertTrue($columns['id']->getNotnull());
     $this->assertTrue($columns['foo']->getNotnull());
     $this->assertTrue($columns['bar']->getNotnull());
     $diffTable = clone $table;
     $diffTable->changeColumn('foo', array('notnull' => false));
     $diffTable->changeColumn('bar', array('length' => 1024));
     $this->_sm->alterTable($comparator->diffTable($table, $diffTable));
     $columns = $this->_sm->listTableColumns($tableName);
     $this->assertTrue($columns['id']->getNotnull());
     $this->assertFalse($columns['foo']->getNotnull());
     $this->assertTrue($columns['bar']->getNotnull());
 }
 public function testDetectChangeIdentifierType()
 {
     $this->markTestSkipped('DBAL-2 was reopened, this test cannot work anymore.');
     $tableA = new Table("foo");
     $tableA->addColumn('id', 'integer', array('autoincrement' => false));
     $tableB = new Table("foo");
     $tableB->addColumn('id', 'integer', array('autoincrement' => true));
     $c = new Comparator();
     $tableDiff = $c->diffTable($tableA, $tableB);
     $this->assertType('Doctrine\\DBAL\\Schema\\TableDiff', $tableDiff);
     $this->assertArrayHasKey('id', $tableDiff->changedColumns);
 }
Exemplo n.º 8
0
 /**
  * @group DBAL-464
  */
 public function testDropPrimaryKeyWithAutoincrementColumn()
 {
     $table = new Table("drop_primary_key");
     $table->addColumn('id', 'integer', array('primary' => true, 'autoincrement' => true));
     $table->addColumn('foo', 'integer', array('primary' => true));
     $table->addColumn('bar', 'integer');
     $table->setPrimaryKey(array('id', 'foo'));
     $comparator = new Comparator();
     $diffTable = clone $table;
     $diffTable->dropPrimaryKey();
     $this->assertEquals(array('ALTER TABLE drop_primary_key MODIFY id INT NOT NULL', 'ALTER TABLE drop_primary_key DROP PRIMARY KEY'), $this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable)));
 }
Exemplo n.º 9
0
 /**
  * @group DBAL-586
  */
 public function testAddAutoIncrementPrimaryKey()
 {
     $keyTable = new Table("foo");
     $keyTable->addColumn("id", "integer", array('autoincrement' => true));
     $keyTable->addColumn("baz", "string");
     $keyTable->setPrimaryKey(array("id"));
     $oldTable = new Table("foo");
     $oldTable->addColumn("baz", "string");
     $c = new \Doctrine\DBAL\Schema\Comparator();
     $diff = $c->diffTable($oldTable, $keyTable);
     $sql = $this->_platform->getAlterTableSQL($diff);
     $this->assertEquals(array("ALTER TABLE foo ADD id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)"), $sql);
 }
 /**
  * @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());
 }
Exemplo n.º 11
0
 public function testDetectRenameColumn()
 {
     $tableA = new Table("foo");
     $tableA->addColumn('foo', 'integer');
     $tableB = new Table("foo");
     $tableB->addColumn('bar', 'integer');
     $c = new Comparator();
     $tableDiff = $c->diffTable($tableA, $tableB);
     $this->assertEquals(0, count($tableDiff->addedColumns));
     $this->assertEquals(0, count($tableDiff->removedColumns));
     $this->assertArrayHasKey('foo', $tableDiff->renamedColumns);
     $this->assertEquals('bar', $tableDiff->renamedColumns['foo']->getName());
 }
Exemplo n.º 12
0
 /**
  * @group DBAL-423
  */
 public function testDiffListGuidTableColumn()
 {
     $offlineTable = new Table('list_guid_table_column');
     $offlineTable->addColumn('col_guid', 'guid');
     $this->_sm->dropAndCreateTable($offlineTable);
     $onlineTable = $this->_sm->listTableDetails('list_guid_table_column');
     $comparator = new Comparator();
     $this->assertFalse($comparator->diffTable($offlineTable, $onlineTable), "No differences should be detected with the offline vs online schema.");
 }
Exemplo n.º 13
0
 /**
  * @group DBAL-105
  */
 public function testDiff()
 {
     $table = new \Doctrine\DBAL\Schema\Table('twitter_users');
     $table->addColumn('id', 'integer', array('autoincrement' => true));
     $table->addColumn('twitterId', 'integer', array('nullable' => false));
     $table->addColumn('displayName', 'string', array('nullable' => false));
     $table->setPrimaryKey(array('id'));
     $newtable = new \Doctrine\DBAL\Schema\Table('twitter_users');
     $newtable->addColumn('id', 'integer', array('autoincrement' => true));
     $newtable->addColumn('twitter_id', 'integer', array('nullable' => false));
     $newtable->addColumn('display_name', 'string', array('nullable' => false));
     $newtable->addColumn('logged_in_at', 'datetime', array('nullable' => true));
     $newtable->setPrimaryKey(array('id'));
     $c = new Comparator();
     $tableDiff = $c->diffTable($table, $newtable);
     $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\TableDiff', $tableDiff);
     $this->assertEquals(array('twitterid', 'displayname'), array_keys($tableDiff->renamedColumns));
     $this->assertEquals(array('logged_in_at'), array_keys($tableDiff->addedColumns));
     $this->assertEquals(0, count($tableDiff->removedColumns));
 }
Exemplo n.º 14
0
 /**
  * @group DBAL-400
  */
 public function testAlterTableAddPrimaryKey()
 {
     $table = new Table('alter_table_add_pk');
     $table->addColumn('id', 'integer');
     $table->addColumn('foo', 'integer');
     $table->addIndex(array('id'), 'idx_id');
     $this->_sm->createTable($table);
     $comparator = new Comparator();
     $diffTable = clone $table;
     $diffTable->dropIndex('idx_id');
     $diffTable->setPrimaryKey(array('id'));
     $this->_sm->alterTable($comparator->diffTable($table, $diffTable));
     $table = $this->_sm->listTableDetails("alter_table_add_pk");
     $this->assertFalse($table->hasIndex('idx_id'));
     $this->assertTrue($table->hasPrimaryKey());
 }
Exemplo n.º 15
0
 /**
  * @group DBAL-835
  */
 public function testQuotesAlterTableChangeColumnLength()
 {
     $fromTable = new Table('mytable');
     $fromTable->addColumn('unquoted1', 'string', array('comment' => 'Unquoted 1', 'length' => 10));
     $fromTable->addColumn('unquoted2', 'string', array('comment' => 'Unquoted 2', 'length' => 10));
     $fromTable->addColumn('unquoted3', 'string', array('comment' => 'Unquoted 3', 'length' => 10));
     $fromTable->addColumn('create', 'string', array('comment' => 'Reserved keyword 1', 'length' => 10));
     $fromTable->addColumn('table', 'string', array('comment' => 'Reserved keyword 2', 'length' => 10));
     $fromTable->addColumn('select', 'string', array('comment' => 'Reserved keyword 3', 'length' => 10));
     $toTable = new Table('mytable');
     $toTable->addColumn('unquoted1', 'string', array('comment' => 'Unquoted 1', 'length' => 255));
     $toTable->addColumn('unquoted2', 'string', array('comment' => 'Unquoted 2', 'length' => 255));
     $toTable->addColumn('unquoted3', 'string', array('comment' => 'Unquoted 3', 'length' => 255));
     $toTable->addColumn('create', 'string', array('comment' => 'Reserved keyword 1', 'length' => 255));
     $toTable->addColumn('table', 'string', array('comment' => 'Reserved keyword 2', 'length' => 255));
     $toTable->addColumn('select', 'string', array('comment' => 'Reserved keyword 3', 'length' => 255));
     $comparator = new Comparator();
     $this->assertEquals($this->getQuotedAlterTableChangeColumnLengthSQL(), $this->_platform->getAlterTableSQL($comparator->diffTable($fromTable, $toTable)));
 }
 /**
  * @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());
 }
Exemplo n.º 17
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);
     }
 }
Exemplo n.º 18
0
 /**
  * @dataProvider getDiffListIntegerAutoincrementTableColumnsData
  * @group DBAL-924
  */
 public function testDiffListIntegerAutoincrementTableColumns($integerType, $unsigned, $expectedComparatorDiff)
 {
     $tableName = 'test_int_autoincrement_table';
     $offlineTable = new \Doctrine\DBAL\Schema\Table($tableName);
     $offlineTable->addColumn('id', $integerType, array('autoincrement' => true, 'unsigned' => $unsigned));
     $offlineTable->setPrimaryKey(array('id'));
     $this->_sm->dropAndCreateTable($offlineTable);
     $onlineTable = $this->_sm->listTableDetails($tableName);
     $comparator = new Schema\Comparator();
     $diff = $comparator->diffTable($offlineTable, $onlineTable);
     if ($expectedComparatorDiff) {
         $this->assertEmpty($this->_sm->getDatabasePlatform()->getAlterTableSQL($diff));
     } else {
         $this->assertFalse($diff);
     }
 }
Exemplo n.º 19
0
 /**
  * Updates table for sync jobs. Returns NULL if table is up-to-date.
  *
  * @param Connection|null $connection
  *
  * @return bool|null
  */
 public function updateTable($connection = null)
 {
     $connection = $connection ?: $this->connection;
     $schemaManager = $connection->getSchemaManager();
     if (!$schemaManager->tablesExist([$this->tableName])) {
         return false;
     }
     $table = new Table($this->tableName);
     $this->buildTable($table);
     $oldTable = $schemaManager->listTableDetails($this->tableName);
     $comparator = new Comparator();
     $diff = $comparator->diffTable($oldTable, $table);
     if (!$diff) {
         return null;
     }
     $schemaManager->alterTable($diff);
     return true;
 }
Exemplo n.º 20
0
 private function curateTable($vanilla)
 {
     if ($existing = $this->getExisting($vanilla->getName())) {
         $comparator = new Schema\Comparator();
         if ($diff = $comparator->diffTable($existing, $vanilla)) {
             $this->schema->alterTable($diff);
         }
     } else {
         $this->schema->createTable($vanilla);
     }
 }
 /**
  * @group DBAL-1033
  */
 public function testPartialIndexes()
 {
     $offlineTable = new Schema\Table('person');
     $offlineTable->addColumn('id', 'integer');
     $offlineTable->addColumn('name', 'string');
     $offlineTable->addColumn('email', 'string');
     $offlineTable->addUniqueIndex(array('id', 'name'), 'simple_partial_index', array('where' => '(id IS NULL)'));
     $offlineTable->addIndex(array('id', 'name'), 'complex_partial_index', array(), array('where' => '(((id IS NOT NULL) AND (name IS NULL)) AND (email IS NULL))'));
     $this->_sm->dropAndCreateTable($offlineTable);
     $onlineTable = $this->_sm->listTableDetails('person');
     $comparator = new Schema\Comparator();
     $this->assertFalse($comparator->diffTable($offlineTable, $onlineTable));
     $this->assertTrue($onlineTable->hasIndex('simple_partial_index'));
     $this->assertTrue($onlineTable->hasIndex('complex_partial_index'));
     $this->assertTrue($onlineTable->getIndex('simple_partial_index')->hasOption('where'));
     $this->assertTrue($onlineTable->getIndex('complex_partial_index')->hasOption('where'));
     $this->assertSame('(id IS NULL)', $onlineTable->getIndex('simple_partial_index')->getOption('where'));
     $this->assertSame('(((id IS NOT NULL) AND (name IS NULL)) AND (email IS NULL))', $onlineTable->getIndex('complex_partial_index')->getOption('where'));
 }
 public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes()
 {
     $table = new Table("text_blob_default_value");
     $table->addColumn('def_text', 'text', array('default' => 'def'));
     $table->addColumn('def_text_null', 'text', array('notnull' => false, 'default' => 'def'));
     $table->addColumn('def_blob', 'blob', array('default' => 'def'));
     $table->addColumn('def_blob_null', 'blob', array('notnull' => false, 'default' => 'def'));
     $this->assertSame(array('CREATE TABLE text_blob_default_value (def_text LONGTEXT NOT NULL, def_text_null LONGTEXT DEFAULT NULL, def_blob LONGBLOB NOT NULL, def_blob_null LONGBLOB DEFAULT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'), $this->_platform->getCreateTableSQL($table));
     $diffTable = clone $table;
     $diffTable->changeColumn('def_text', array('default' => null));
     $diffTable->changeColumn('def_text_null', array('default' => null));
     $diffTable->changeColumn('def_blob', array('default' => null));
     $diffTable->changeColumn('def_blob_null', array('default' => null));
     $comparator = new Comparator();
     $this->assertEmpty($this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable)));
 }
Exemplo n.º 23
0
 /**
  * Check and repair tables.
  *
  * @return CheckResponse
  */
 public function repairTables()
 {
     // When repairing tables we want to start with an empty flashbag. Otherwise we get another
     // 'repair your DB'-notice, right after we're done repairing.
     $this->app['logger.flash']->clear();
     $response = new CheckResponse();
     $currentTables = $this->getTableObjects();
     /** @var $schemaManager AbstractSchemaManager */
     $schemaManager = $this->app['db']->getSchemaManager();
     $comparator = new Comparator();
     $tables = $this->getTablesSchema();
     /** @var $table Table */
     foreach ($tables as $table) {
         $tableName = $table->getName();
         // Create the users table.
         if (!isset($currentTables[$tableName])) {
             /** @var $platform AbstractPlatform */
             $platform = $this->app['db']->getDatabasePlatform();
             $queries = $platform->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES | AbstractPlatform::CREATE_FOREIGNKEYS);
             foreach ($queries as $query) {
                 $this->app['db']->query($query);
             }
             $response->addTitle($tableName, sprintf('Created table `%s`.', $tableName));
         } else {
             $diff = $comparator->diffTable($currentTables[$tableName], $table);
             if ($diff) {
                 $diff = $this->cleanupTableDiff($diff);
                 // diff may be just deleted columns which we have reset above
                 // only exec and add output if does really alter anything
                 if ($this->app['db']->getDatabasePlatform()->getAlterTableSQL($diff)) {
                     $schemaManager->alterTable($diff);
                     $response->addTitle($tableName, sprintf('Updated `%s` table to match current schema.', $tableName));
                 }
             }
         }
     }
     return $response;
 }
 /**
  * @group DBAL-365
  */
 public function testDroppingConstraintsBeforeColumns()
 {
     $newTable = new Table('mytable');
     $newTable->addColumn('id', 'integer');
     $newTable->setPrimaryKey(array('id'));
     $oldTable = clone $newTable;
     $oldTable->addColumn('parent_id', 'integer');
     $oldTable->addUnnamedForeignKeyConstraint('mytable', array('parent_id'), array('id'));
     $comparator = new \Doctrine\DBAL\Schema\Comparator();
     $tableDiff = $comparator->diffTable($oldTable, $newTable);
     $sql = $this->_platform->getAlterTableSQL($tableDiff);
     $expectedSql = array('ALTER TABLE mytable DROP CONSTRAINT FK_6B2BD609727ACA70', 'DROP INDEX IDX_6B2BD609727ACA70', 'ALTER TABLE mytable DROP parent_id');
     $this->assertEquals($expectedSql, $sql);
 }
Exemplo n.º 25
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);
     }
 }
 /**
  * @group DBAL-1004
  */
 public function testAltersTableColumnCommentWithExplicitlyQuotedIdentifiers()
 {
     $table1 = new Table('"foo"', array(new Column('"bar"', Type::getType('integer'))));
     $table2 = new Table('"foo"', array(new Column('"bar"', Type::getType('integer'), array('comment' => 'baz'))));
     $comparator = new Comparator();
     $tableDiff = $comparator->diffTable($table1, $table2);
     $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\TableDiff', $tableDiff);
     $this->assertSame(array('COMMENT ON COLUMN "foo"."bar" IS \'baz\''), $this->_platform->getAlterTableSQL($tableDiff));
 }
Exemplo n.º 27
0
 /**
  * Check and repair tables
  *
  * @return array
  */
 public function repairTables()
 {
     // When repairing tables we want to start with an empty flashbag. Otherwise we get another
     // 'repair your DB'-notice, right after we're done repairing.
     $this->app['session']->getFlashBag()->clear();
     $output = array();
     $currentTables = $this->getTableObjects();
     /** @var $schemaManager AbstractSchemaManager */
     $schemaManager = $this->app['db']->getSchemaManager();
     $comparator = new Comparator();
     $baseTables = $this->getBoltTablesNames();
     $tables = $this->getTablesSchema();
     /** @var $table Table */
     foreach ($tables as $table) {
         // Create the users table..
         if (!isset($currentTables[$table->getName()])) {
             /** @var $platform AbstractPlatform */
             $platform = $this->app['db']->getDatabasePlatform();
             $queries = $platform->getCreateTableSQL($table);
             foreach ($queries as $query) {
                 $this->app['db']->query($query);
             }
             $output[] = "Created table `" . $table->getName() . "`.";
         } else {
             $diff = $comparator->diffTable($currentTables[$table->getName()], $table);
             if ($diff) {
                 $diff = $this->cleanupTableDiff($diff);
                 // diff may be just deleted columns which we have reset above
                 // only exec and add output if does really alter anything
                 if ($this->app['db']->getDatabasePlatform()->getAlterTableSQL($diff)) {
                     $schemaManager->alterTable($diff);
                     $output[] = "Updated `" . $table->getName() . "` table to match current schema.";
                 }
             }
         }
     }
     return $output;
 }
 /**
  * @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);
 }