/** * @param StandardSearchIndexerInterface $category * @param Key $key * @param $previousHandle */ public function updateSearchIndexKeyColumns(CategoryInterface $category, AttributeKeyInterface $key, $previousHandle = null) { $controller = $key->getController(); /* * Added this for some backward compatibility reason – but it's obviously not * right because it makes it so no search index columns get created. if (!$previousHandle) { $previousHandle = $key->getAttributeKeyHandle(); }*/ if ($key->getAttributeKeyHandle() == $previousHandle || $key->isAttributeKeySearchable() == false || $category->getIndexedSearchTable() == false || $controller->getSearchIndexFieldDefinition() == false) { return false; } $fields = array(); $dropColumns = array(); $definition = $controller->getSearchIndexFieldDefinition(); $sm = $this->connection->getSchemaManager(); $toTable = $sm->listTableDetails($category->getIndexedSearchTable()); if ($previousHandle) { if (isset($definition['type'])) { $dropColumns[] = 'ak_' . $previousHandle; } else { foreach ($definition as $name => $column) { $dropColumns[] = 'ak_' . $previousHandle . '_' . $name; } } } if (isset($definition['type'])) { if (!$toTable->hasColumn('ak_' . $key->getAttributeKeyHandle())) { $fields[] = array('name' => 'ak_' . $key->getAttributeKeyHandle(), 'type' => $definition['type'], 'options' => $definition['options']); } } else { foreach ($definition as $name => $column) { if (!$toTable->hasColumn('ak_' . $key->getAttributeKeyHandle() . '_' . $name)) { $fields[] = array('name' => 'ak_' . $key->getAttributeKeyHandle() . '_' . $name, 'type' => $column['type'], 'options' => $column['options']); } } } $fromTable = $sm->listTableDetails($category->getIndexedSearchTable()); $parser = new \Concrete\Core\Database\Schema\Parser\ArrayParser(); $comparator = new \Doctrine\DBAL\Schema\Comparator(); if ($previousHandle != false) { foreach ($dropColumns as $column) { $toTable->dropColumn($column); } } $toTable = $parser->addColumns($toTable, $fields); $diff = $comparator->diffTable($fromTable, $toTable); if ($diff !== false) { $sql = $this->connection->getDatabasePlatform()->getAlterTableSQL($diff); $arr = array(); foreach ($sql as $q) { $arr[] = $q; $this->connection->exec($q); } } }
public function testUniquePrimaryKey() { $keyTable = new Table("foo"); $keyTable->addColumn("bar", "integer"); $keyTable->addColumn("baz", "string"); $keyTable->setPrimaryKey(array("bar")); $keyTable->addUniqueIndex(array("baz")); $oldTable = new Table("foo"); $oldTable->addColumn("bar", "integer"); $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 PRIMARY KEY (bar)", "CREATE UNIQUE INDEX UNIQ_8C73652178240498 ON foo (baz)"), $sql); }
/** * @group DBAL-37 */ public function testAlterTableAutoIncrementDrop() { $tableFrom = new \Doctrine\DBAL\Schema\Table('autoinc_table_drop'); $column = $tableFrom->addColumn('id', 'integer'); $column->setAutoincrement(true); $this->_sm->createTable($tableFrom); $tableFrom = $this->_sm->listTableDetails('autoinc_table_drop'); $this->assertTrue($tableFrom->getColumn('id')->getAutoincrement()); $tableTo = new \Doctrine\DBAL\Schema\Table('autoinc_table_drop'); $column = $tableTo->addColumn('id', 'integer'); $c = new \Doctrine\DBAL\Schema\Comparator(); $diff = $c->diffTable($tableFrom, $tableTo); $this->assertType('Doctrine\\DBAL\\Schema\\TableDiff', $diff, "There should be a difference and not false being returned from the table comparison"); $this->assertEquals(array("ALTER TABLE autoinc_table_drop ALTER id DROP DEFAULT"), $this->_conn->getDatabasePlatform()->getAlterTableSQL($diff)); $this->_sm->alterTable($diff); $tableFinal = $this->_sm->listTableDetails('autoinc_table_drop'); $this->assertFalse($tableFinal->getColumn('id')->getAutoincrement()); }
public function testDiffTableBug() { $schema = new Schema(); $table = $schema->createTable('diffbug_routing_translations'); $table->addColumn('id', 'integer'); $table->addColumn('route', 'string'); $table->addColumn('locale', 'string'); $table->addColumn('attribute', 'string'); $table->addColumn('localized_value', 'string'); $table->addColumn('original_value', 'string'); $table->setPrimaryKey(array('id')); $table->addUniqueIndex(array('route', 'locale', 'attribute')); $table->addIndex(array('localized_value')); // this is much more selective than the unique index $this->_sm->createTable($table); $tableFetched = $this->_sm->listTableDetails("diffbug_routing_translations"); $comparator = new \Doctrine\DBAL\Schema\Comparator(); $diff = $comparator->diffTable($tableFetched, $table); $this->assertFalse($diff, "no changes expected."); }
/** * Installs the database tables for all entity classes contained within the * $metadatas array. Returns true if new tables were created or existing * ones altered. Otherwise this will return false if there were no database * migrations needed. * * @param array $metadatas * * @return bool */ public function installDatabaseFor(array $metadatas) { if (count($metadatas) > 0) { // We need to create the SchemaDiff manually here because we want // to avoid calling the execution for two separate SchemaDiff // objects (one for missing tables and one for new ones). // Also, while $tool->createSchema($missingEntities) works great // for new tables, $tool->updateSchema($updateEntities) would // actually delete all the DB tables that the DB contains and are // not part of the entity tables passed to the function. Therefore, // we do this manually here. $em = $this->getEntityManager(); $conn = $em->getConnection(); $sm = $conn->getSchemaManager(); $cmf = $em->getMetadataFactory(); $tool = new \Doctrine\ORM\Tools\SchemaTool($em); $comparator = new \Doctrine\DBAL\Schema\Comparator(); // NOTE: $newSchema != $toSchema because $toSchema would actually // contain each and every table in the database. We'll only need // to traverse the $newSchema for the purposes of the desired // functionality but we also need $fromSchema to check whether // the table already exists and also to get the current schema // for that table to figure out the changes to the new table. $fromSchema = $sm->createSchema(); $newSchema = $tool->getSchemaFromMetadata($metadatas); $newTables = array(); $changedTables = array(); foreach ($newSchema->getTables() as $newTable) { // Check if the table already exists if ($fromSchema->hasTable($newTable->getName())) { $diff = $comparator->diffTable($fromSchema->getTable($newTable->getName()), $newTable); if ($diff) { $changedTables[] = $diff; } } else { $newTables[] = $newTable; } } if (count($newTables) > 0 || count($changedTables) > 0) { // If we have new or changed tables (or both), we'll gather // these DB changes into a SchemaDiff object and get all the // necessary DB migration queries for that diff object. // Finally, those queries are executed against the DB. $schemaDiff = new SchemaDiff($newTables, $changedTables); $platform = $conn->getDatabasePlatform(); $migrateSql = $schemaDiff->toSql($platform); foreach ($migrateSql as $sql) { $conn->executeQuery($sql); } return true; } } return false; }
/** * @group DDC-2843 */ public function testBooleanDefault() { $table = new \Doctrine\DBAL\Schema\Table('ddc2843_bools'); $table->addColumn('id', 'integer'); $table->addColumn('checked', 'boolean', array('default' => false)); $this->_sm->createTable($table); $databaseTable = $this->_sm->listTableDetails($table->getName()); $c = new \Doctrine\DBAL\Schema\Comparator(); $diff = $c->diffTable($table, $databaseTable); $this->assertFalse($diff); }
public function delete() { $at = $this->getAttributeType(); $at->controller->setAttributeKey($this); $at->controller->deleteKey(); $cnt = $this->getController(); $db = Loader::db(); $db->Execute('delete from AttributeKeys where akID = ?', array($this->getAttributeKeyID())); $db->Execute('delete from AttributeSetKeys where akID = ?', array($this->getAttributeKeyID())); if ($this->getIndexedSearchTable()) { $definition = $cnt->getSearchIndexFieldDefinition(); $prefix = $this->akHandle; $sm = $db->getSchemaManager(); $platform = $db->getDatabasePlatform(); $fromTable = $sm->listTableDetails($this->getIndexedSearchTable()); $toTable = $sm->listTableDetails($this->getIndexedSearchTable()); $dropColumns = array(); if (isset($definition['type'])) { $dropColumns[] = 'ak_' . $prefix; } else { if (is_array($definition)) { foreach ($definition as $name => $column) { $dropColumns[] = 'ak_' . $prefix . '_' . $name; } } } $comparator = new \Doctrine\DBAL\Schema\Comparator(); foreach ($dropColumns as $dc) { $toTable->dropColumn($dc); } $diff = $comparator->diffTable($fromTable, $toTable); if ($diff) { $sql = $platform->getAlterTableSQL($diff); foreach ($sql as $q) { $db->exec($q); } } } }
/** * @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); }
/** * @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); }
public function testDiffListTableColumns() { $offlineTable = $this->createTableSchema(); $onlineTable = $this->sm->listTableDetails('points'); $comparator = new \Doctrine\DBAL\Schema\Comparator(); $diff = $comparator->diffTable($offlineTable, $onlineTable); $this->assertFalse($diff, 'No differences should be detected with the offline vs online schema.'); }
public function testDiffListTableColumns() { if ($this->_sm->getDatabasePlatform()->getName() == 'oracle') { $this->markTestSkipped('Does not work with Oracle, since it cannot detect DateTime, Date and Time differenecs (at the moment).'); } $offlineTable = $this->createListTableColumns(); $onlineTable = $this->_sm->listTableDetails('list_table_columns'); $comparator = new \Doctrine\DBAL\Schema\Comparator(); $diff = $comparator->diffTable($offlineTable, $onlineTable); $this->assertFalse($diff, "No differences should be detected with the offline vs online schema."); }