示例#1
0
 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));
 }
示例#2
0
 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);
     }
 }
示例#3
0
 /**
  * Computes the difference between two index objects.
  *
  * @param  Index   $fromIndex
  * @param  Index   $toIndex
  * @param  boolean $caseInsensitive
  * @return boolean
  */
 public static function computeDiff(Index $fromIndex, Index $toIndex, $caseInsensitive = false)
 {
     // Check for removed index columns in $toIndex
     $fromIndexColumns = $fromIndex->getColumns();
     $max = count($fromIndexColumns);
     for ($i = 0; $i < $max; $i++) {
         $indexColumn = $fromIndexColumns[$i];
         if (!$toIndex->hasColumnAtPosition($i, $indexColumn, $fromIndex->getColumnSize($indexColumn), $caseInsensitive)) {
             return true;
         }
     }
     // Check for new index columns in $toIndex
     $toIndexColumns = $toIndex->getColumns();
     $max = count($toIndexColumns);
     for ($i = 0; $i < $max; $i++) {
         $indexColumn = $toIndexColumns[$i];
         if (!$fromIndex->hasColumnAtPosition($i, $indexColumn, $toIndex->getColumnSize($indexColumn), $caseInsensitive)) {
             return true;
         }
     }
     // Check for difference in unicity
     return $fromIndex->isUnique() !== $toIndex->isUnique();
 }
示例#4
0
$fkCategoryPost->setDefaultJoin('Criteria::INNER_JOIN');
$fkCategoryPost->setOnDelete('SETNULL');
$fkPostTag = new ForeignKey('fk_post_has_tags');
$fkPostTag->addReference('post_id', 'id');
$fkPostTag->setForeignTableCommonName('blog_post');
$fkPostTag->setPhpName('Post');
$fkPostTag->setDefaultJoin('Criteria::LEFT_JOIN');
$fkPostTag->setOnDelete('CASCADE');
$fkTagPost = new ForeignKey('fk_tag_has_posts');
$fkTagPost->addReference('tag_id', 'id');
$fkTagPost->setForeignTableCommonName('blog_tag');
$fkTagPost->setPhpName('Tag');
$fkTagPost->setDefaultJoin('Criteria::LEFT_JOIN');
$fkTagPost->setOnDelete('CASCADE');
/* Regular Indexes */
$pageContentFulltextIdx = new Index('page_content_fulltext_idx');
$pageContentFulltextIdx->setColumns([['name' => 'content']]);
$pageContentFulltextIdx->addVendorInfo(new VendorInfo('mysql', ['Index_type' => 'FULLTEXT']));
/* Unique Indexes */
$authorUsernameUnique = new Unique('author_password_unique_idx');
$authorUsernameUnique->setColumns([['name' => 'username', 'size' => '8']]);
/* Behaviors */
$timestampableBehavior = new TimestampableBehavior();
$timestampableBehavior->setName('timestampable');
$sluggableBehavior = new SluggableBehavior();
$sluggableBehavior->setName('sluggable');
/* Tables */
$table1 = new Table('blog_post');
$table1->setDescription('The list of posts');
$table1->setNamespace('Blog');
$table1->setPackage('Acme.Blog');
示例#5
0
文件: Table.php 项目: disider/Propel2
 /**
  * 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);
 }
示例#6
0
 /**
  * Builds the DDL SQL to add an Index.
  *
  * @param  Index  $index
  * @return string
  */
 public function getAddIndexDDL(Index $index)
 {
     // don't create index form primary key
     if ($this->getPrimaryKeyName($index->getTable()) == $this->quoteIdentifier($index->getName())) {
         return '';
     }
     $pattern = "\nCREATE %sINDEX %s ON %s (%s)%s;\n";
     return sprintf($pattern, $index->isUnique() ? 'UNIQUE ' : '', $this->quoteIdentifier($index->getName()), $this->quoteIdentifier($index->getTable()->getName()), $this->getColumnListDDL($index->getColumnObjects()), $this->generateBlockStorage($index));
 }
 public function testIsIndex()
 {
     $table = new Table();
     $column1 = new Column('category_id');
     $column2 = new Column('type');
     $table->addColumn($column1);
     $table->addColumn($column2);
     $index = new Index('test_index');
     $index->setColumns([$column1, $column2]);
     $table->addIndex($index);
     $this->assertTrue($table->isIndex(['category_id', 'type']));
     $this->assertTrue($table->isIndex(['type', 'category_id']));
     $this->assertFalse($table->isIndex(['category_id', 'type2']));
     $this->assertFalse($table->isIndex(['asd']));
 }
示例#8
0
 /**
  * Overrides the implementation from DefaultPlatform
  *
  * @author     Niklas Närhinen <*****@*****.**>
  * @return     string
  * @see        DefaultPlatform::getDropIndexDDL
  */
 public function getDropIndexDDL(Index $index)
 {
     if ($index instanceof Unique) {
         $pattern = "\n    ALTER TABLE %s DROP CONSTRAINT %s;\n    ";
         return sprintf($pattern, $this->quoteIdentifier($index->getTable()->getName()), $this->quoteIdentifier($index->getName()));
     } else {
         return parent::getDropIndexDDL($index);
     }
 }
 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));
 }
示例#11
0
 public function testHasColumnAtFirstPosition()
 {
     $index = new Index();
     $index->addColumn($this->getColumnMock('foo', array('size' => 0)));
     $this->assertTrue($index->hasColumnAtPosition(0, 'foo'));
 }
示例#12
0
 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);
         }
     }
 }
 /**
  * Builds the DDL SQL for an Index object.
  *
  * @param  Index  $index
  * @return string
  */
 public function getIndexDDL(Index $index)
 {
     return sprintf('%sINDEX %s (%s)', $index->isUnique() ? 'UNIQUE ' : '', $this->quoteIdentifier($index->getName()), $this->getColumnListDDL($index->getColumnObjects()));
 }
 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());
 }
 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));
 }
示例#17
0
 /**
  * Appends a generice <index> or <unique> XML node to its parent node.
  *
  * @param string   $nodeType   The node type (index or unique)
  * @param Index    $index      The Index model instance
  * @param \DOMNode $parentNode The parent DOMNode object
  */
 private function appendGenericIndexNode($nodeType, Index $index, \DOMNode $parentNode)
 {
     $indexNode = $parentNode->appendChild($this->document->createElement($nodeType));
     $indexNode->setAttribute('name', $index->getName());
     foreach ($index->getColumns() as $columnName) {
         $indexColumnNode = $indexNode->appendChild($this->document->createElement($nodeType . '-column'));
         $indexColumnNode->setAttribute('name', $columnName);
         if ($size = $index->getColumnSize($columnName)) {
             $indexColumnNode->setAttribute('size', $size);
         }
     }
     foreach ($index->getVendorInformation() as $vendorInformation) {
         $this->appendVendorInformationNode($vendorInformation, $indexNode);
     }
 }
示例#18
0
 /**
  * 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);
         }
     }
 }
示例#19
0
 /**
  * Adds a new index to the indices list and set the
  * parent table of the column to the current table.
  *
  * @param  Index $index
  * @return Index
  */
 public function addIndex($index)
 {
     if ($index instanceof Index) {
         $index->setTable($this);
         // force the name to be created if empty.
         $index->getName();
         $this->indices[] = $index;
         return $index;
     }
     $idx = new Index();
     $idx->loadMapping($index);
     return $this->addIndex($idx);
 }
示例#20
0
文件: Table.php 项目: rouffj/Propel2
 /**
  * Adds a new index to the index list and set the
  * parent table of the column to the current table
  */
 public function addIndex($idxdata)
 {
     if ($idxdata instanceof Index) {
         $index = $idxdata;
         $index->setTable($this);
         $index->getName();
         // we call this method so that the name is created now if it doesn't already exist.
         $this->indices[] = $index;
         return $index;
     } else {
         $index = new Index($this);
         $index->loadFromXML($idxdata);
         return $this->addIndex($index);
     }
 }
示例#21
0
    public function testToString()
    {
        $tableA = new Table('A');
        $tableB = new Table('B');
        $diff = new TableDiff($tableA, $tableB);
        $diff->addAddedColumn('id', new Column('id', 'integer'));
        $diff->addRemovedColumn('category_id', new Column('category_id', 'integer'));
        $colFoo = new Column('foo', 'integer');
        $colBar = new Column('bar', 'integer');
        $tableA->addColumn($colFoo);
        $tableA->addColumn($colBar);
        $diff->addRenamedColumn($colFoo, $colBar);
        $columnDiff = new ColumnDiff($colFoo, $colBar);
        $diff->addModifiedColumn('foo', $columnDiff);
        $fk = new ForeignKey('category');
        $fk->setTable($tableA);
        $fk->setForeignTableCommonName('B');
        $fk->addReference('category_id', 'id');
        $fkChanged = clone $fk;
        $fkChanged->setForeignTableCommonName('C');
        $fkChanged->addReference('bla', 'id2');
        $fkChanged->setOnDelete('cascade');
        $fkChanged->setOnUpdate('cascade');
        $diff->addAddedFk('category', $fk);
        $diff->addModifiedFk('category', $fk, $fkChanged);
        $diff->addRemovedFk('category', $fk);
        $index = new Index('test_index');
        $index->setTable($tableA);
        $index->setColumns([$colFoo]);
        $indexChanged = clone $index;
        $indexChanged->setColumns([$colBar]);
        $diff->addAddedIndex('test_index', $index);
        $diff->addModifiedIndex('test_index', $index, $indexChanged);
        $diff->addRemovedIndex('test_index', $index);
        $string = (string) $diff;
        $expected = '  A:
    addedColumns:
      - id
    removedColumns:
      - category_id
    modifiedColumns:
      A.FOO:
        modifiedProperties:
    renamedColumns:
      foo: bar
    addedIndices:
      - test_index
    removedIndices:
      - test_index
    modifiedIndices:
      - test_index
    addedFks:
      - category
    removedFks:
      - category
    modifiedFks:
      category:
          localColumns: from ["category_id"] to ["category_id","bla"]
          foreignColumns: from ["id"] to ["id","id2"]
          onUpdate: from  to CASCADE
          onDelete: from  to CASCADE
';
        $this->assertEquals($expected, $string);
    }
 protected function getIndexType(Index $index)
 {
     $type = '';
     $vendorInfo = $index->getVendorInfoForType($this->getDatabaseType());
     if ($vendorInfo && $vendorInfo->getParameter('Index_type')) {
         $type = $vendorInfo->getParameter('Index_type') . ' ';
     } elseif ($index->isUnique()) {
         $type = 'UNIQUE ';
     }
     return $type;
 }
示例#23
0
 public function testReverseDiffHasModifiedIndices()
 {
     $table = new Table();
     $table->setDatabase(new Database('foo', new DefaultPlatform()));
     $fromIndex = new Index('i1');
     $fromIndex->setTable($table);
     $toIndex = new Index('i1');
     $toIndex->setTable($table);
     $diff = $this->createTableDiff();
     $diff->addModifiedIndex('i1', $fromIndex, $toIndex);
     $reverseDiff = $diff->getReverseDiff();
     $this->assertTrue($reverseDiff->hasModifiedIndices());
     $this->assertSame(['i1' => [$toIndex, $fromIndex]], $reverseDiff->getModifiedIndices());
 }