public function testCompareDifferentOrder() { $c1 = new Column('Foo'); $c2 = new Column('Bar'); $i1 = new Index('Foo_Bar_Index'); $i1->addColumn($c1); $i1->addColumn($c2); $c3 = new Column('Foo'); $c4 = new Column('Bar'); $i2 = new Index('Foo_Bar_Index'); $i2->addColumn($c4); $i2->addColumn($c3); $this->assertTrue(IndexComparator::computeDiff($i1, $i2)); }
public function testAddIndex() { $table = new Table(); $index = new Index(); $index->addColumn(['name' => 'bla']); $table->addIndex($index); $this->assertCount(1, $table->getIndices()); }
/** * Adds Indexes to the specified table. * * @param Table $table The Table model class to add columns to. */ protected function addIndexes(Table $table) { $stmt = $this->dbh->query("SELECT COLUMN_NAME, INDEX_NAME FROM USER_IND_COLUMNS WHERE TABLE_NAME = '" . $table->getName() . "' ORDER BY COLUMN_NAME"); $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); $indices = array(); foreach ($rows as $row) { $indices[$row['INDEX_NAME']][] = $row['COLUMN_NAME']; } foreach ($indices as $indexName => $columnNames) { $index = new Index($indexName); foreach ($columnNames as $columnName) { // Oracle deals with complex indices using an internal reference, so... // let's ignore this kind of index if ($table->hasColumn($columnName)) { $index->addColumn($table->getColumn($columnName)); } } // since some of the columns are pruned above, we must only add an index if it has columns if ($index->hasColumns()) { $table->addIndex($index); } } }
public function testGetIndexDDLFulltext() { $table = new Table('foo'); $table->setIdentifierQuoting(true); $column1 = new Column('bar1'); $column1->getDomain()->copy($this->getPlatform()->getDomainForType('LONGVARCHAR')); $table->addColumn($column1); $index = new Index('bar_index'); $index->addColumn($column1); $vendor = new VendorInfo('mysql'); $vendor->setParameter('Index_type', 'FULLTEXT'); $index->addVendorInfo($vendor); $table->addIndex($index); $expected = 'FULLTEXT INDEX `bar_index` (`bar1`)'; $this->assertEquals($expected, $this->getPlatform()->getIndexDDL($index)); }
protected function addArchiveTable() { $table = $this->getTable(); $database = $table->getDatabase(); $archiveTableName = $this->getParameter('archive_table') ? $this->getParameter('archive_table') : $this->getTable()->getName() . '_archive'; if (!$database->hasTable($archiveTableName)) { // create the version table $archiveTable = $database->addTable(array('name' => $archiveTableName, 'package' => $table->getPackage(), 'schema' => $table->getSchema(), 'namespace' => $table->getNamespace() ? '\\' . $table->getNamespace() : null)); $archiveTable->isArchiveTable = true; // copy all the columns foreach ($table->getColumns() as $column) { $columnInArchiveTable = clone $column; if ($columnInArchiveTable->hasReferrers()) { $columnInArchiveTable->clearReferrers(); } if ($columnInArchiveTable->isAutoincrement()) { $columnInArchiveTable->setAutoIncrement(false); } $archiveTable->addColumn($columnInArchiveTable); } // add archived_at column if ($this->getParameter('log_archived_at') == 'true') { $archiveTable->addColumn(array('name' => $this->getParameter('archived_at_column'), 'type' => 'TIMESTAMP')); } // do not copy foreign keys // copy the indices foreach ($table->getIndices() as $index) { $copiedIndex = clone $index; $copiedIndex->setName(''); $archiveTable->addIndex($copiedIndex); } // copy unique indices to indices // see https://github.com/propelorm/Propel/issues/175 for details foreach ($table->getUnices() as $unique) { $index = new Index(); foreach ($unique->getColumns() as $columnName) { if ($size = $unique->getColumnSize($columnName)) { $index->addColumn(array('name' => $columnName, 'size' => $size)); } else { $index->addColumn(array('name' => $columnName)); } } $archiveTable->addIndex($index); } // every behavior adding a table should re-execute database behaviors foreach ($database->getBehaviors() as $behavior) { $behavior->modifyDatabase(); } $this->archiveTable = $archiveTable; } else { $this->archiveTable = $database->getTable($archiveTableName); } }
/** * Adds a new index to the indices list and set the * parent table of the column to the current table. * * @param Index|array $index * @return Index * * @throw InvalidArgumentException */ public function addIndex($index) { if ($index instanceof Index) { if ($this->hasIndex($index->getName())) { throw new InvalidArgumentException(sprintf('Index "%s" already exist.', $index->getName())); } if (!$index->getColumns()) { throw new InvalidArgumentException(sprintf('Index "%s" has no columns.', $index->getName())); } $index->setTable($this); // force the name to be created if empty. $this->indices[] = $index; return $index; } $idx = new Index(); $idx->loadMapping($index); foreach ((array) @$index['columns'] as $column) { $idx->addColumn($column); } return $this->addIndex($idx); }
public function testCompareModifiedIndices() { $t1 = new Table(); $c1 = new Column('Foo'); $c1->getDomain()->copy($this->platform->getDomainForType('VARCHAR')); $c1->getDomain()->replaceSize(255); $c1->setNotNull(false); $t1->addColumn($c1); $i1 = new Index('Foo_Index'); $i1->addColumn($c1); $t1->addIndex($i1); $t2 = new Table(); $c2 = new Column('Foo'); $c2->getDomain()->copy($this->platform->getDomainForType('DOUBLE')); $c2->getDomain()->replaceScale(2); $c2->getDomain()->replaceSize(3); $c2->setNotNull(true); $c2->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE)); $t2->addColumn($c2); $i2 = new Unique('Foo_Index'); $i2->addColumn($c2); $t2->addIndex($i2); $tc = new PropelTableComparator(); $tc->setFromTable($t1); $tc->setToTable($t2); $nbDiffs = $tc->compareIndices(); $tableDiff = $tc->getTableDiff(); $this->assertEquals(1, $nbDiffs); $this->assertEquals(1, count($tableDiff->getModifiedIndices())); $this->assertEquals(array('Foo_Index' => array($i1, $i2)), $tableDiff->getModifiedIndices()); }
private function addSnapshotTable() { $table = $this->getTable(); $primaryKeyColumn = $table->getFirstPrimaryKeyColumn(); $database = $table->getDatabase(); $snapshotTableName = $this->getParameter(self::PARAMETER_SNAPSHOT_TABLE) ?: $this->getDefaultSnapshotTableName(); if ($database->hasTable($snapshotTableName)) { $this->snapshotTable = $database->getTable($snapshotTableName); return; } $snapshotTable = $database->addTable(['name' => $snapshotTableName, 'phpName' => $this->getParameter(self::PARAMETER_SNAPSHOT_PHPNAME), 'package' => $table->getPackage(), 'schema' => $table->getSchema(), 'namespace' => $table->getNamespace() ? '\\' . $table->getNamespace() : null]); $addSnapshotAt = true; $hasTimestampableBehavior = 0 < count(array_filter($database->getBehaviors(), function (Behavior $behavior) { return 'timestampable' === $behavior->getName(); })); if ($hasTimestampableBehavior) { $addSnapshotAt = false; $timestampableBehavior = clone $database->getBehavior('timestampable'); $timestampableBehavior->setParameters(array_merge($timestampableBehavior->getParameters(), ['create_column' => $this->getParameter(self::PARAMETER_SNAPSHOT_AT_COLUMN), 'disable_updated_at' => 'true'])); $snapshotTable->addBehavior($timestampableBehavior); } $snapshotTable->isSnapshotTable = true; $idColumn = $snapshotTable->addColumn(['name' => 'id', 'type' => 'INTEGER']); $idColumn->setAutoIncrement(true); $idColumn->setPrimaryKey(true); $idColumn->setNotNull(true); $columns = $table->getColumns(); foreach ($columns as $column) { if ($column->isPrimaryKey()) { continue; } $columnInSnapshotTable = clone $column; $columnInSnapshotTable->setNotNull(false); if ($columnInSnapshotTable->hasReferrers()) { $columnInSnapshotTable->clearReferrers(); } if ($columnInSnapshotTable->isAutoincrement()) { $columnInSnapshotTable->setAutoIncrement(false); } $snapshotTable->addColumn($columnInSnapshotTable); } $foreignKeyColumn = $snapshotTable->addColumn(['name' => $this->getParameter(self::PARAMETER_REFERENCE_COLUMN), 'type' => $primaryKeyColumn->getType(), 'size' => $primaryKeyColumn->getSize()]); $index = new Index(); $index->setName($this->getParameter(self::PARAMETER_REFERENCE_COLUMN)); if ($primaryKeyColumn->getSize()) { $index->addColumn(['name' => $this->getParameter(self::PARAMETER_REFERENCE_COLUMN), 'size' => $primaryKeyColumn->getSize()]); } else { $index->addColumn(['name' => $this->getParameter(self::PARAMETER_REFERENCE_COLUMN)]); } $snapshotTable->addIndex($index); $foreignKey = new ForeignKey(); $foreignKey->setName(vsprintf('fk_%s_%s', [$snapshotTable->getOriginCommonName(), $this->getParameter(self::PARAMETER_REFERENCE_COLUMN)])); $foreignKey->setOnUpdate('CASCADE'); $foreignKey->setOnDelete('SET NULL'); $foreignKey->setForeignTableCommonName($this->getTable()->getCommonName()); $foreignKey->addReference($foreignKeyColumn, $primaryKeyColumn); $snapshotTable->addForeignKey($foreignKey); if ($this->getParameter(self::PARAMETER_LOG_SNAPSHOT_AT) == 'true' && $addSnapshotAt) { $snapshotTable->addColumn(['name' => $this->getParameter(self::PARAMETER_SNAPSHOT_AT_COLUMN), 'type' => 'TIMESTAMP']); } $indices = $table->getIndices(); foreach ($indices as $index) { $copiedIndex = clone $index; $snapshotTable->addIndex($copiedIndex); } // copy unique indices to indices // see https://github.com/propelorm/Propel/issues/175 for details $unices = $table->getUnices(); foreach ($unices as $unique) { $index = new Index(); $index->setName($unique->getName()); $columns = $unique->getColumns(); foreach ($columns as $columnName) { if ($size = $unique->getColumnSize($columnName)) { $index->addColumn(['name' => $columnName, 'size' => $size]); } else { $index->addColumn(['name' => $columnName]); } } $snapshotTable->addIndex($index); } $behaviors = $database->getBehaviors(); foreach ($behaviors as $behavior) { $behavior->modifyDatabase(); } $this->snapshotTable = $snapshotTable; }
public function providerForTestGetIndexDDL() { $table = new Table('foo'); $table->setIdentifierQuoting(true); $column1 = new Column('bar1'); $column1->getDomain()->copy(new Domain('FOOTYPE')); $table->addColumn($column1); $column2 = new Column('bar2'); $column2->getDomain()->copy(new Domain('BARTYPE')); $table->addColumn($column2); $index = new Index('babar'); $index->addColumn($column1); $index->addColumn($column2); $table->addIndex($index); return array(array($index)); }
public function testHasColumnAtFirstPosition() { $index = new Index(); $index->addColumn($this->getColumnMock('foo', array('size' => 0))); $this->assertTrue($index->hasColumnAtPosition(0, 'foo')); }
public function startElement($parser, $name, $attributes) { $parentTag = $this->peekCurrentSchemaTag(); if (false === $parentTag) { switch ($name) { case 'database': if ($this->isExternalSchema()) { $this->currentPackage = isset($attributes['package']) ? $attributes['package'] : null; if (null === $this->currentPackage) { $this->currentPackage = $this->defaultPackage; } } else { $this->currDB = $this->schema->addDatabase($attributes); } break; default: $this->_throwInvalidTagException($parser, $name); } } elseif ('database' === $parentTag) { switch ($name) { case 'external-schema': $xmlFile = isset($attributes['filename']) ? $attributes['filename'] : null; // 'referenceOnly' attribute is valid in the main schema XML file only, // and it's ignored in the nested external-schemas if (!$this->isExternalSchema()) { $isForRefOnly = isset($attributes['referenceOnly']) ? $attributes['referenceOnly'] : null; $this->isForReferenceOnly = null !== $isForRefOnly ? 'true' === strtolower($isForRefOnly) : true; // defaults to TRUE } if ('/' !== $xmlFile[0]) { $xmlFile = realpath(dirname($this->currentXmlFile) . DIRECTORY_SEPARATOR . $xmlFile); if (!file_exists($xmlFile)) { throw new SchemaException(sprintf('Unknown include external "%s"', $xmlFile)); } } $this->parseFile($xmlFile); break; case 'domain': $this->currDB->addDomain($attributes); break; case 'table': if (!isset($attributes['schema']) && $this->currDB->getSchema() && $this->currDB->getPlatform()->supportsSchemas() && false === strpos($attributes['name'], $this->currDB->getPlatform()->getSchemaDelimiter())) { $attributes['schema'] = $this->currDB->getSchema(); } $this->currTable = $this->currDB->addTable($attributes); if ($this->isExternalSchema()) { $this->currTable->setForReferenceOnly($this->isForReferenceOnly); $this->currTable->setPackage($this->currentPackage); } break; case 'vendor': $this->currVendorObject = $this->currDB->addVendorInfo($attributes); break; case 'behavior': $this->currBehavior = $this->currDB->addBehavior($attributes); break; default: $this->_throwInvalidTagException($parser, $name); } } elseif ('table' === $parentTag) { switch ($name) { case 'column': $this->currColumn = $this->currTable->addColumn($attributes); break; case 'foreign-key': $this->currFK = $this->currTable->addForeignKey($attributes); break; case 'index': $this->currIndex = new Index(); $this->currIndex->setTable($this->currTable); $this->currIndex->loadMapping($attributes); break; case 'unique': $this->currUnique = new Unique(); $this->currUnique->setTable($this->currTable); $this->currUnique->loadMapping($attributes); break; case 'vendor': $this->currVendorObject = $this->currTable->addVendorInfo($attributes); break; case 'id-method-parameter': $this->currTable->addIdMethodParameter($attributes); break; case 'behavior': $this->currBehavior = $this->currTable->addBehavior($attributes); break; default: $this->_throwInvalidTagException($parser, $name); } } elseif ('column' === $parentTag) { switch ($name) { case 'inheritance': $this->currColumn->addInheritance($attributes); break; case 'vendor': $this->currVendorObject = $this->currColumn->addVendorInfo($attributes); break; default: $this->_throwInvalidTagException($parser, $name); } } elseif ('foreign-key' === $parentTag) { switch ($name) { case 'reference': $this->currFK->addReference($attributes); break; case 'vendor': $this->currVendorObject = $this->currUnique->addVendorInfo($attributes); break; default: $this->_throwInvalidTagException($parser, $name); } } elseif ('index' === $parentTag) { switch ($name) { case 'index-column': $this->currIndex->addColumn($attributes); break; case 'vendor': $this->currVendorObject = $this->currIndex->addVendorInfo($attributes); break; default: $this->_throwInvalidTagException($parser, $name); } } elseif ('unique' === $parentTag) { switch ($name) { case 'unique-column': $this->currUnique->addColumn($attributes); break; case 'vendor': $this->currVendorObject = $this->currUnique->addVendorInfo($attributes); break; default: $this->_throwInvalidTagException($parser, $name); } } elseif ($parentTag == 'behavior') { switch ($name) { case 'parameter': $this->currBehavior->addParameter($attributes); break; default: $this->_throwInvalidTagException($parser, $name); } } elseif ('vendor' === $parentTag) { switch ($name) { case 'parameter': $this->currVendorObject->setParameter($attributes['name'], $attributes['value']); break; default: $this->_throwInvalidTagException($parser, $name); } } else { // it must be an invalid tag $this->_throwInvalidTagException($parser, $name); } $this->pushCurrentSchemaTag($name); }
protected function addColumn(Table $table, $name) { if (!$table->hasColumn($name)) { $column = new Column($name); // don't know how to define unsigned :( $domain = new Domain('TINYINT', 'tinyint(3) unsigned'); $column->setDomain($domain); $table->addColumn($column); $column_idx_name = $name . '_idx'; if (!$table->hasIndex($column_idx_name)) { $column_idx = new Index($column_idx_name); $column_idx->addColumn(['name' => $column->getName()]); $table->addIndex($column_idx); } } }