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(PropelIndexComparator::computeDiff($i1, $i2));
 }
Exemplo n.º 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);
     }
 }
Exemplo n.º 3
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);
         }
     }
 }
 public function testGetIndexDDLFulltext()
 {
     $table = new Table('foo');
     $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));
 }
 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());
 }
Exemplo n.º 6
0
 /**
  * Load indexes for this table
  */
 protected function addIndexes(Table $table)
 {
     $stmt = $this->dbh->query("PRAGMA index_list('" . $table->getName() . "')");
     while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
         $name = $row['name'];
         $index = new Index($name);
         $stmt2 = $this->dbh->query("PRAGMA index_info('" . $name . "')");
         while ($row2 = $stmt2->fetch(\PDO::FETCH_ASSOC)) {
             $colname = $row2['name'];
             $index->addColumn($table->getColumn($colname));
         }
         $table->addIndex($index);
     }
 }
Exemplo n.º 7
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");
		/* @var stmt PDOStatement */
		$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
		if (count($rows) > 0) {
			$index = new Index($rows[0]['INDEX_NAME']);
			foreach($rows AS $row) {
				$index->addColumn($row['COLUMN_NAME']);
			}
			$table->addIndex($index);
		}
	}
Exemplo n.º 8
0
 public function providerForTestGetIndexDDL()
 {
     $table = new Table('foo');
     $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));
 }
Exemplo n.º 9
0
 /**
  * Load indexes for this table
  */
 protected function addIndexes(Table $table)
 {
     $stmt = $this->dbh->query("SELECT INDEX_NAME, COLUMN_NAME FROM USER_IND_COLUMNS WHERE TABLE_NAME = '" . $table->getName() . "'");
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         $name = $row['INDEX_NAME'];
         $index = new Index($name);
         $colname = $row['COLUMN_NAME'];
         $index->addColumn($table->getColumn($colname));
         $table->addIndex($index);
     }
 }