/** * Add or update an index to the table * * @param $columns * @param $name * @param array $options * * @throws SchemaException */ public function addIndex($columns, $name, $options = array()) { if (!is_array($columns)) { $columns = array($columns); } foreach ($columns as $column) { if (!in_array($column, $this->allowedColumns)) { $columnSchema = $this->table->getColumn($column); $type = $columnSchema->getType(); if ($type instanceof StringType) { $this->allowedColumns[] = $columnSchema->getName(); } } } // Indexes are only allowed on columns that are string $columns = array_intersect($columns, $this->allowedColumns); if (!empty($columns)) { $index = new Index($this->prefix . $name, $columns, false, false, $options); if ($this->table->hasIndex($this->prefix . $name)) { $this->changedIndexes[] = $index; } else { $this->addedIndexes[] = $index; } } }
/** * Adds the missing indexes to the table * * @param \Doctrine\DBAL\Schema\Table $table Table object * @return \Doctrine\DBAL\Schema\Table Updated table object */ protected function addIndexes(\Doctrine\DBAL\Schema\Table $table) { $indexes = array('idx_ezpus_langid' => array('langid'), 'idx_ezpus_status_ln_fn' => array('status', 'lastname', 'firstname'), 'idx_ezpus_status_ad1_ad2' => array('status', 'address1', 'address2'), 'idx_ezpus_status_postal_city' => array('status', 'postal', 'city'), 'idx_ezpus_lastname' => array('lastname'), 'idx_ezpus_address1' => array('address1'), 'idx_ezpus_postal' => array('postal'), 'idx_ezpus_city' => array('city')); foreach ($indexes as $name => $def) { if ($table->hasIndex($name) === false) { $table->addIndex($def, $name); } } return $table; }
/** * Adds the missing indexes to the table * * @param \Doctrine\DBAL\Schema\Table $table Table object * @return \Doctrine\DBAL\Schema\Table Updated table object */ protected function addIndexes(\Doctrine\DBAL\Schema\Table $table) { $indexes = array('idx_msord_sid_cdate' => array('siteid', 'cdate'), 'idx_msord_sid_cmonth' => array('siteid', 'cmonth'), 'idx_msord_sid_cweek' => array('siteid', 'cweek'), 'idx_msord_sid_hour' => array('siteid', 'chour')); foreach ($indexes as $name => $def) { if ($table->hasIndex($name) === false) { $table->addIndex($def, $name); } } return $table; }
/** * @group DBAL-50 */ public function testOverruleIndex() { $table = new Table("bar"); $table->addColumn('baz', 'integer', array()); $table->addIndex(array('baz')); $indexes = $table->getIndexes(); $this->assertEquals(1, count($indexes)); $index = current($indexes); $table->addUniqueIndex(array('baz')); $this->assertEquals(1, count($table->getIndexes())); $this->assertFalse($table->hasIndex($index->getName())); }
public function testAddIndexWithCaseSensitiveColumnProblem() { $table = new Table("foo"); $table->addColumn("id", 'integer'); $table->addIndex(array("ID"), "my_idx"); $this->assertTrue($table->hasIndex('my_idx')); $this->assertEquals(array("ID"), $table->getIndex("my_idx")->getColumns()); }
/** * @group DBAL-224 */ public function testDropIndex() { $table = new Table("test"); $table->addColumn('id', 'integer'); $table->addIndex(array('id'), 'idx'); $this->assertTrue($table->hasIndex('idx')); $table->dropIndex('idx'); $this->assertFalse($table->hasIndex('idx')); }
/** * @param Schema $schema * @param QueryBag $queries * @param string $tableName * @param string $columnName * @param array $options * @param string $className * @param Table $table */ protected function renameIndex(Schema $schema, QueryBag $queries, $tableName, $columnName, $options, $className, $table) { $newColumnName = $options[ExtendOptionsManager::NEW_NAME_OPTION]; $indexName = $this->nameGenerator->generateIndexNameForExtendFieldVisibleInGrid($className, $columnName); if ($table->hasIndex($indexName)) { $table->dropIndex($indexName); $newIndexName = $this->nameGenerator->generateIndexNameForExtendFieldVisibleInGrid($className, $newColumnName); $this->renameExtension->addIndex($schema, $queries, $tableName, [$newColumnName], $newIndexName); } }
/** * 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; }
/** * @dataProvider getNormalizesAssetNames * @group DBAL-831 */ public function testNormalizesColumnNames($assetName) { $table = new Table('test'); $table->addColumn($assetName, 'integer'); $table->addIndex(array($assetName), $assetName); $table->addForeignKeyConstraint('test', array($assetName), array($assetName), array(), $assetName); $this->assertTrue($table->hasColumn($assetName)); $this->assertTrue($table->hasColumn('foo')); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\Column', $table->getColumn($assetName)); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\Column', $table->getColumn('foo')); $this->assertTrue($table->hasIndex($assetName)); $this->assertTrue($table->hasIndex('foo')); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\Index', $table->getIndex($assetName)); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\Index', $table->getIndex('foo')); $this->assertTrue($table->hasForeignKey($assetName)); $this->assertTrue($table->hasForeignKey('foo')); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\ForeignKeyConstraint', $table->getForeignKey($assetName)); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\ForeignKeyConstraint', $table->getForeignKey('foo')); $table->renameIndex($assetName, $assetName); $this->assertTrue($table->hasIndex($assetName)); $this->assertTrue($table->hasIndex('foo')); $table->renameIndex($assetName, 'foo'); $this->assertTrue($table->hasIndex($assetName)); $this->assertTrue($table->hasIndex('foo')); $table->renameIndex('foo', $assetName); $this->assertTrue($table->hasIndex($assetName)); $this->assertTrue($table->hasIndex('foo')); $table->renameIndex($assetName, 'bar'); $this->assertFalse($table->hasIndex($assetName)); $this->assertFalse($table->hasIndex('foo')); $this->assertTrue($table->hasIndex('bar')); $table->renameIndex('bar', $assetName); $table->dropColumn($assetName); $table->dropIndex($assetName); $table->removeForeignKey($assetName); $this->assertFalse($table->hasColumn($assetName)); $this->assertFalse($table->hasColumn('foo')); $this->assertFalse($table->hasIndex($assetName)); $this->assertFalse($table->hasIndex('foo')); $this->assertFalse($table->hasForeignKey($assetName)); $this->assertFalse($table->hasForeignKey('foo')); }
/** * @group DBAL-50 */ public function testOverruleIndex() { $table = new Table("bar"); $table->addColumn('baz', 'integer', array()); $table->addIndex(array('baz')); $this->assertEquals(1, count($table->getIndexes())); $this->assertTrue($table->hasIndex('bar_baz_idx')); $table->addUniqueIndex(array('baz')); $this->assertEquals(1, count($table->getIndexes())); $this->assertFalse($table->hasIndex('bar_baz_idx')); $this->assertTrue($table->hasIndex('bar_baz_uniq')); }
/** * Assert that the column is indexed. * * @return $this */ public function index() { $index = $this->getIndexName(); $this->assertTrue($this->table->hasIndex($index), "The {$this->name} column is not indexed."); $this->assertTrue($this->table->getIndex($index)->isSimpleIndex(), "The {$this->name} column is not a simple index."); }
/** * @group DDC-133 */ public function testAllowImplicitSchemaTableInAutogeneratedIndexNames() { $table = new Table("foo.bar"); $table->addColumn('baz', 'integer', array()); $table->addIndex(array('baz')); $this->assertTrue($table->hasIndex('foo_bar_baz_idx')); }