protected function addClosureColumn($name, Table $ct_table, Column $column) { $table = $this->getTable(); $id_fieldname = $column->getName(); $domain = $column->getDomain(); if (!$ct_table->hasColumn($name)) { $column = new Column($name); $column->setDomain($domain); $column->setPrimaryKey(true); $ct_table->addColumn($column); } else { $column = $ct_table->getColumn($name); } $ct_tablename_normalized = str_replace('_', '', $ct_table->getName()); $fk_name = $ct_tablename_normalized . '_' . $name . '_fk'; if (!$ct_table->getColumnForeignKeys($name)) { $column_fk = new ForeignKey($fk_name); $column_fk->addReference($name, $table->getColumn($id_fieldname)->getName()); $column_fk->setForeignTableCommonName($table->getName()); $column_fk->setOnUpdate('cascade'); $column_fk->setOnDelete('restrict'); $ct_table->addForeignKey($column_fk); } $column_idx_name = $fk_name . '_idx'; if (!$ct_table->hasIndex($column_idx_name)) { $column_idx = new Index($column_idx_name); $column_idx->addColumn(['name' => $column->getName()]); $ct_table->addIndex($column_idx); } }
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)); }
/** * Adds Columns to the specified table. * * @param Table $table The Table model class to add columns to. */ 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 = 1 == $row['pk'] && 'integer' === strtolower($type); $notNull = $row['notnull']; $default = $row['dflt_value']; $propelType = $this->getMappedPropelType(strtolower($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 (null !== $default) { $column->getDomain()->setDefaultValue(new ColumnDefaultValue($default, ColumnDefaultValue::TYPE_VALUE)); } $column->setAutoIncrement($autoincrement); $column->setNotNull($notNull); if (1 == $row['pk'] || 'integer' === strtolower($type)) { $column->setPrimaryKey(true); } $table->addColumn($column); } }
/** * Adds Columns to the specified table. * * @param Table $table The Table model class to add columns to. */ protected function addColumns(Table $table) { $tableName = $table->getName(); // var_dump("PRAGMA table_info('$tableName') //"); $stmt = $this->dbh->query("PRAGMA table_info('{$tableName}')"); while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $name = $row['name']; $fulltype = $row['type']; $size = null; $scale = null; if (preg_match('/^([^\\(]+)\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)$/', $fulltype, $matches)) { $type = $matches[1]; $size = $matches[2]; $scale = $matches[3]; } elseif (preg_match('/^([^\\(]+)\\(\\s*(\\d+)\\s*\\)$/', $fulltype, $matches)) { $type = $matches[1]; $size = $matches[2]; } else { $type = $fulltype; } $notNull = $row['notnull']; $default = $row['dflt_value']; $propelType = $this->getMappedPropelType(strtolower($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 (null !== $default) { if ("'" !== substr($default, 0, 1) && strpos($default, '(')) { $defaultType = ColumnDefaultValue::TYPE_EXPR; if ('datetime(CURRENT_TIMESTAMP, \'localtime\')' === $default) { $default = 'CURRENT_TIMESTAMP'; } } else { $defaultType = ColumnDefaultValue::TYPE_VALUE; $default = str_replace("'", '', $default); } $column->getDomain()->setDefaultValue(new ColumnDefaultValue($default, $defaultType)); } $column->setNotNull($notNull); if (0 < $row['pk'] + 0) { $column->setPrimaryKey(true); } if ($column->isPrimaryKey()) { // check if autoIncrement $autoIncrementStmt = $this->dbh->prepare(' SELECT tbl_name FROM sqlite_master WHERE tbl_name = ? AND sql LIKE "%AUTOINCREMENT%" '); $autoIncrementStmt->execute([$table->getName()]); $autoincrementRow = $autoIncrementStmt->fetch(\PDO::FETCH_ASSOC); if ($autoincrementRow && $autoincrementRow['tbl_name'] == $table->getName()) { $column->setAutoIncrement(true); } } $table->addColumn($column); } }
$column41 = new Column('id', 'integer', 7); $column41->setAutoIncrement(); $column41->setNotNull(); $column41->setPrimaryKey(); $column42 = new Column('name', 'varchar', 40); $column42->setNotNull(); $column51 = new Column('post_id', 'integer', 7); $column51->setNotNull(); $column51->setPrimaryKey(); $column52 = new Column('tag_id', 'integer', 7); $column52->setNotNull(); $column52->setPrimaryKey(); $column61 = new Column('id', 'integer', 5); $column61->setNotNull(); $column61->setAutoIncrement(); $column61->setPrimaryKey(); $column62 = new Column('title', 'varchar', 150); $column62->setNotNull(); $column63 = new Column('content', 'clob'); $column63->addVendorInfo(new VendorInfo('mysql', ['Charset' => 'latin1', 'Collate' => 'latin1_general_ci'])); $column64 = new Column('is_published', 'boolean'); $column64->setNotNull(); $column64->setDefaultValue('false'); /* Foreign Keys */ $fkAuthorPost = new ForeignKey('fk_post_has_author'); $fkAuthorPost->addReference('author_id', 'id'); $fkAuthorPost->setForeignTableCommonName('blog_author'); $fkAuthorPost->setRefPhpName('Posts'); $fkAuthorPost->setPhpName('Author'); $fkAuthorPost->setDefaultJoin('Criteria::LEFT_JOIN'); $fkAuthorPost->setOnDelete('CASCADE');
public function testReverseDiffHasRenamedPkColumn() { $fromColumn = new Column('post_id', 'integer'); $fromColumn->setPrimaryKey(); $toColumn = new Column('id', 'integer'); $toColumn->setPrimaryKey(); $diff = $this->createTableDiff(); $diff->addRenamedPkColumn($fromColumn, $toColumn); $reverseDiff = $diff->getReverseDiff(); $this->assertTrue($reverseDiff->hasRenamedPkColumns()); $this->assertSame([[$toColumn, $fromColumn]], $reverseDiff->getRenamedPkColumns()); }
public function providerForTestPrimaryKeyDDL() { $table = new Table('foo'); $table->setIdentifierQuoting(true); $column = new Column('bar'); $column->setPrimaryKey(true); $table->addColumn($column); return array(array($table)); }
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 TableComparator(); $tc->setFromTable($t1); $tc->setToTable($t2); $nbDiffs = $tc->comparePrimaryKeys(); $tableDiff = $tc->getTableDiff(); $this->assertEquals(2, $nbDiffs); $this->assertEquals([[$c1, $c4], [$c2, $c5]], $tableDiff->getRenamedPkColumns()); $this->assertEquals([], $tableDiff->getAddedPkColumns()); $this->assertEquals([], $tableDiff->getRemovedPkColumns()); }
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()); }
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); $schema = new Schema(); $schema->addDatabase($database); $validator = new SchemaValidator($schema); $this->assertTrue($validator->validate()); }