public function testValidateReturnsTrueWhenTwoTablesHaveSamePhpNameInDifferentNamespaces() { $column1 = new Column('id'); $column1->setPrimaryKey(true); $table1 = new Table('foo'); $table1->addColumn($column1); $table1->setNamespace('Foo'); $column2 = new Column('id'); $column2->setPrimaryKey(true); $table2 = new Table('bar'); $table2->addColumn($column2); $table2->setPhpName('Foo'); $table2->setNamespace('Bar'); $database = new Database(); $database->addTable($table1); $database->addTable($table2); $appData = new AppData(); $appData->addDatabase($database); $validator = new PropelSchemaValidator($appData); $this->assertTrue($validator->validate()); }
public function testColumnIsFKAndPK() { $column = new Column(); $column->setName('id'); $column->setPrimaryKey(true); $column->setAutoIncrement(true); $column->setType('integer'); $table = new Table(); $table->setCommonName('table_one'); $table->addColumn($column); $db = new Database(); $db->setName('MultipleTables'); $db->addTable($table); $column = new Column(); $column->setName('id'); $column->setPrimaryKey(true); $column->setAutoIncrement(true); $column->setType('integer'); $c2 = new Column(); $c2->setPrimaryKey(true); $c2->setName('foreign_id'); $c2->setType('integer'); $table = new Table(); $table->setCommonName('table_two'); $table->addColumn($column); $table->addColumn($c2); $fk = new ForeignKey(); $fk->setName('FK_1'); $fk->addReference('foreign_id', 'id'); $fk->setForeignTableCommonName('table_one'); $table->addForeignKey($fk); $db->addTable($table); $expected = implode("\n", array('digraph G {', 'nodetable_one [label="{<table>table_one|<cols>id (integer) [PK]\\l}", shape=record];', 'nodetable_two [label="{<table>table_two|<cols>id (integer) [PK]\\lforeign_id (integer) [FK] [PK]\\l}", shape=record];', 'nodetable_two:cols -> nodetable_one:table [label="foreign_id=id"];', '}', '')); $this->assertEquals($expected, PropelDotGenerator::create($db)); }
public function testGetPrimaryKeyDDLCompositeKey() { $table = new Table('foo'); $column1 = new Column('bar1'); $column1->setPrimaryKey(true); $table->addColumn($column1); $column2 = new Column('bar2'); $column2->setPrimaryKey(true); $table->addColumn($column2); $expected = 'PRIMARY KEY (`bar1`,`bar2`)'; $this->assertEquals($expected, $this->getPlatform()->getPrimaryKeyDDL($table)); }
public function testCompareSeveralPrimaryKeyDifferences() { $t1 = new Table(); $c1 = new Column('col1'); $c1->getDomain()->copy($this->platform->getDomainForType('VARCHAR')); $c1->getDomain()->replaceSize(255); $c1->setNotNull(false); $t1->addColumn($c1); $c2 = new Column('col2'); $c2->getDomain()->copy($this->platform->getDomainForType('INTEGER')); $c2->setNotNull(true); $c2->setPrimaryKey(true); $t1->addColumn($c2); $c3 = new Column('col3'); $c3->getDomain()->copy($this->platform->getDomainForType('VARCHAR')); $c3->getDomain()->replaceSize(255); $c3->setPrimaryKey(true); $t1->addColumn($c3); $t2 = new Table(); $c4 = new Column('col1'); $c4->getDomain()->copy($this->platform->getDomainForType('DOUBLE')); $c4->getDomain()->replaceScale(2); $c4->getDomain()->replaceSize(3); $c4->setNotNull(true); $c4->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE)); $t2->addColumn($c4); $c5 = new Column('col22'); $c5->getDomain()->copy($this->platform->getDomainForType('INTEGER')); $c5->setNotNull(true); $c5->setPrimaryKey(true); $t2->addColumn($c5); $c6 = new Column('col4'); $c6->getDomain()->copy($this->platform->getDomainForType('LONGVARCHAR')); $c6->getDomain()->setDefaultValue(new ColumnDefaultValue('123', ColumnDefaultValue::TYPE_VALUE)); $c6->setPrimaryKey(true); $t2->addColumn($c6); // col2 was renamed, col3 was removed, col4 was added $tc = new PropelTableComparator(); $tc->setFromTable($t1); $tc->setToTable($t2); $nbDiffs = $tc->comparePrimaryKeys(); $tableDiff = $tc->getTableDiff(); $this->assertEquals(3, $nbDiffs); $this->assertEquals(array(array($c2, $c5)), $tableDiff->getRenamedPkColumns()); $this->assertEquals(array('col4' => $c6), $tableDiff->getAddedPkColumns()); $this->assertEquals(array('col3' => $c3), $tableDiff->getRemovedPkColumns()); }
/** * Adds Columns to the specified table. * * @param Table $table The Table model class to add columns to. * @param int $oid The table OID * @param string $version The database version. */ protected function addColumns(Table $table) { $stmt = $this->dbh->query("PRAGMA table_info('" . $table->getName() . "')"); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $name = $row['name']; $fulltype = $row['type']; $size = null; $precision = null; $scale = null; if (preg_match('/^([^\\(]+)\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)$/', $fulltype, $matches)) { $type = $matches[1]; $precision = $matches[2]; $scale = $matches[3]; // aka precision } elseif (preg_match('/^([^\\(]+)\\(\\s*(\\d+)\\s*\\)$/', $fulltype, $matches)) { $type = $matches[1]; $size = $matches[2]; } else { $type = $fulltype; } // If column is primary key and of type INTEGER, it is auto increment // See: http://sqlite.org/faq.html#q1 $autoincrement = $row['pk'] == 1 && strtolower($type) == 'integer'; $not_null = $row['notnull']; $default = $row['dflt_value']; $propelType = $this->getMappedPropelType($type); if (!$propelType) { $propelType = Column::DEFAULT_TYPE; $this->warn("Column [" . $table->getName() . "." . $name . "] has a column type (" . $type . ") that Propel does not support."); } $column = new Column($name); $column->setTable($table); $column->setDomainForType($propelType); // We may want to provide an option to include this: // $column->getDomain()->replaceSqlType($type); $column->getDomain()->replaceSize($size); $column->getDomain()->replaceScale($scale); if ($default !== null) { $column->getDomain()->setDefaultValue(new ColumnDefaultValue($default, ColumnDefaultValue::TYPE_VALUE)); } $column->setAutoIncrement($autoincrement); $column->setNotNull($not_null); if ($row['pk'] == 1 || strtolower($type) == 'integer') { $column->setPrimaryKey(true); } $table->addColumn($column); } }
public function testCompareSeveralRenamedSamePrimaryKeys() { $t1 = new Table(); $c1 = new Column('col1'); $c1->getDomain()->copy($this->platform->getDomainForType('INTEGER')); $c1->setNotNull(true); $c1->setPrimaryKey(true); $t1->addColumn($c1); $c2 = new Column('col2'); $c2->getDomain()->copy($this->platform->getDomainForType('INTEGER')); $c2->setNotNull(true); $c2->setPrimaryKey(true); $t1->addColumn($c2); $c3 = new Column('col3'); $c3->getDomain()->copy($this->platform->getDomainForType('INTEGER')); $c3->setNotNull(true); $c3->setPrimaryKey(true); $t1->addColumn($c3); $t2 = new Table(); $c4 = new Column('col4'); $c4->getDomain()->copy($this->platform->getDomainForType('INTEGER')); $c4->setNotNull(true); $c4->setPrimaryKey(true); $t2->addColumn($c4); $c5 = new Column('col5'); $c5->getDomain()->copy($this->platform->getDomainForType('INTEGER')); $c5->setNotNull(true); $c5->setPrimaryKey(true); $t2->addColumn($c5); $c6 = new Column('col3'); $c6->getDomain()->copy($this->platform->getDomainForType('INTEGER')); $c6->setNotNull(true); $c6->setPrimaryKey(true); $t2->addColumn($c6); // col1 and col2 were renamed $tc = new PropelTableComparator(); $tc->setFromTable($t1); $tc->setToTable($t2); $nbDiffs = $tc->comparePrimaryKeys(); $tableDiff = $tc->getTableDiff(); $this->assertEquals(2, $nbDiffs); $this->assertEquals(array(array($c1, $c4), array($c2, $c5)), $tableDiff->getRenamedPkColumns()); $this->assertEquals(array(), $tableDiff->getAddedPkColumns()); $this->assertEquals(array(), $tableDiff->getRemovedPkColumns()); }
public function providerForTestPrimaryKeyDDL() { $table = new Table('foo'); $column = new Column('bar'); $column->setPrimaryKey(true); $table->addColumn($column); return array(array($table)); }