public function testCommentStringsAreQuoted() { if (!$this->_conn->getDatabasePlatform()->supportsInlineColumnComments() && !$this->_conn->getDatabasePlatform()->supportsCommentOnStatement() && $this->_conn->getDatabasePlatform()->getName() != 'mssql') { $this->markTestSkipped('Database does not support column comments.'); } $table = new Table('my_table'); $table->addColumn('id', 'integer', array('comment' => "It's a comment with a quote")); $table->setPrimaryKey(array('id')); $this->_sm->createTable($table); $columns = $this->_sm->listTableColumns("my_table"); $this->assertEquals("It's a comment with a quote", $columns['id']->getComment()); }
public function testAutoincrementDetection() { if (!$this->_sm->getDatabasePlatform()->supportsIdentityColumns()) { $this->markTestSkipped('This test is only supported on platforms that have autoincrement'); } $table = new \Doctrine\DBAL\Schema\Table('test_autoincrement'); $table->setSchemaConfig($this->_sm->createSchemaConfig()); $table->addColumn('id', 'integer', array('autoincrement' => true)); $table->setPrimaryKey(array('id')); $this->_sm->createTable($table); $inferredTable = $this->_sm->listTableDetails('test_autoincrement'); $this->assertTrue($inferredTable->hasColumn('id')); $this->assertTrue($inferredTable->getColumn('id')->getAutoincrement()); }
/** * Creates database table represented by the model. * * @param Model $model * * @since 1.1.0 * * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**> */ public function createModel($model) { $schema = $this->schemaManager->createSchema(); $tableDef = $schema->createTable($model->meta->dbTable); // this assumes fields set_from_name has been invoked $primaryKeyFields = []; $unique_fields = []; $indexes = []; /** @var $field Field */ /** @var $field ForeignKey */ foreach ($model->meta->localFields as $fname => $field) { $colName = $field->getColumnName(); $type = $field->dbType($this->connection); // if we don't have a type stop if (empty($type)) { continue; } if ($field->primaryKey) { $primaryKeyFields[] = $model->meta->primaryKey->getColumnName(); } elseif ($field->isUnique()) { $unique_fields[] = $colName; } elseif ($field->dbIndex) { $indexes[] = $colName; } $tableDef->addColumn($colName, $type, $this->getDoctrineColumnOptions($field)); if ($field->isRelation && $field->relation && $field->dbConstraint) { $relField = $field->getRelatedField(); $tableDef->addForeignKeyConstraint($relField->scopeModel->meta->dbTable, [$field->getColumnName()], [$relField->getColumnName()]); } } // create the primary key $tableDef->setPrimaryKey($primaryKeyFields); // add index constraint if (!empty($indexes)) { $tableDef->addIndex($indexes); } // add unique constraint if (!empty($unique_fields)) { $tableDef->addUniqueIndex($unique_fields); } $this->schemaManager->createTable($tableDef); // many to many /** @var $relationField ManyToManyField */ foreach ($model->meta->localManyToMany as $name => $relationField) { if ($relationField->manyToMany && $relationField->relation->through->meta->autoCreated) { $this->createModel($relationField->relation->through); } } }
public function testListForeignKeysComposite() { if (!$this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) { $this->markTestSkipped('Does not support foreign key constraints.'); } $this->_sm->createTable($this->getTestTable('test_create_fk3')); $this->_sm->createTable($this->getTestCompositeTable('test_create_fk4')); $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('id', 'foreign_key_test'), 'test_create_fk4', array('id', 'other_id'), 'foreign_key_test_fk'); $this->_sm->createForeignKey($foreignKey, 'test_create_fk3'); $fkeys = $this->_sm->listTableForeignKeys('test_create_fk3'); $this->assertEquals(1, count($fkeys), "Table 'test_create_fk3' has to have one foreign key."); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\ForeignKeyConstraint', $fkeys[0]); $this->assertEquals(array('id', 'foreign_key_test'), array_map('strtolower', $fkeys[0]->getLocalColumns())); $this->assertEquals(array('id', 'other_id'), array_map('strtolower', $fkeys[0]->getForeignColumns())); }
/** * @group DBAL-42 */ public function testAutomaticallyAppendCommentOnMarkedColumns() { if (!$this->_conn->getDatabasePlatform()->supportsInlineColumnComments() && !$this->_conn->getDatabasePlatform()->supportsCommentOnStatement()) { $this->markTestSkipped('Database does not support column comments.'); } $table = new \Doctrine\DBAL\Schema\Table('column_comment_test2'); $table->addColumn('id', 'integer', array('comment' => 'This is a comment')); $table->addColumn('obj', 'object', array('comment' => 'This is a comment')); $table->addColumn('arr', 'array', array('comment' => 'This is a comment')); $table->setPrimaryKey(array('id')); $this->_sm->createTable($table); $columns = $this->_sm->listTableColumns("column_comment_test2"); $this->assertEquals(3, count($columns)); $this->assertEquals('This is a comment', $columns['id']->getComment()); $this->assertEquals('This is a comment', $columns['obj']->getComment(), "The Doctrine2 Typehint should be stripped from comment."); $this->assertInstanceOf('Doctrine\\DBAL\\Types\\ObjectType', $columns['obj']->getType(), "The Doctrine2 should be detected from comment hint."); $this->assertEquals('This is a comment', $columns['arr']->getComment(), "The Doctrine2 Typehint should be stripped from comment."); $this->assertInstanceOf('Doctrine\\DBAL\\Types\\ArrayType', $columns['arr']->getType(), "The Doctrine2 should be detected from comment hint."); }
public function testListTableIndexes() { $table = $this->getTestTable('list_table_indexes_test'); $table->addUniqueIndex(array('test'), 'test_index_name'); $table->addIndex(array('id', 'test'), 'test_composite_idx'); $this->_sm->createTable($table); $tableIndexes = $this->_sm->listTableIndexes('list_table_indexes_test'); $this->assertEquals(3, count($tableIndexes)); $this->assertEquals(array('id'), array_map('strtolower', $tableIndexes['primary']->getColumns())); $this->assertTrue($tableIndexes['primary']->isUnique()); $this->assertTrue($tableIndexes['primary']->isPrimary()); $this->assertEquals('test_index_name', $tableIndexes['test_index_name']->getName()); $this->assertEquals(array('test'), array_map('strtolower', $tableIndexes['test_index_name']->getColumns())); $this->assertTrue($tableIndexes['test_index_name']->isUnique()); $this->assertFalse($tableIndexes['test_index_name']->isPrimary()); $this->assertEquals('test_composite_idx', $tableIndexes['test_composite_idx']->getName()); $this->assertEquals(array('id', 'test'), array_map('strtolower', $tableIndexes['test_composite_idx']->getColumns())); $this->assertFalse($tableIndexes['test_composite_idx']->isUnique()); $this->assertFalse($tableIndexes['test_composite_idx']->isPrimary()); }
/** * Creates a new database table. * * @param string $table * @param \Closure $callback */ public function createTable($table, \Closure $callback) { $table = $this->schema->createTable($this->replacePrefix($table)); $callback($table); $this->manager->createTable($table); }
private function createTable() { $table = new Table(self::TABLE, array(new Column($this->keyColumn, Type::getType(Type::STRING)), new Column($this->valueColumn, Type::getType(Type::TARRAY)))); $table->setPrimaryKey(array($this->keyColumn)); $this->schemaManager->createTable($table); }
/** * @param AbstractSchemaManager $schemaM * @param Schema $schema * * @since 1.1.0 * * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**> */ public function createTable($schemaM, $schema) { if ($schemaM->tablesExist('artists')) { $schemaM->dropTable('artists'); } if ($schemaM->tablesExist('user')) { $schemaM->dropTable('user'); } echo 'Tables :: '; $UTable = $schema->createTable('user'); $UTable->addColumn('id', 'integer', ['unsigned' => true, 'autoincrement' => true]); $UTable->addColumn('name', 'string', ['length' => 60]); $UTable->setPrimaryKey(['id']); $myTable = $schema->createTable('artists'); $myTable->addColumn('id', 'integer', ['unsigned' => true, 'autoincrement' => true]); $myTable->addColumn('user_id', 'integer', ['unsigned' => true]); $myTable->addColumn('name', 'string', ['length' => 60]); $myTable->setPrimaryKey(['id']); $myTable->addForeignKeyConstraint($UTable, array('user_id'), array('id'), array('onUpdate' => 'CASCADE')); $schemaM->createTable($UTable); $schemaM->createTable($myTable); }