private function reverseEngineerMappingFromDatabase() { if ($this->tables !== null) { return; } foreach ($this->_sm->listTableNames() as $tableName) { $tables[strtolower($tableName)] = $this->_sm->listTableDetails($tableName); } $this->tables = array(); foreach ($tables as $name => $table) { /* @var $table Table */ if ($this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) { $foreignKeys = $table->getForeignKeys(); } else { $foreignKeys = array(); } $allForeignKeyColumns = array(); foreach ($foreignKeys as $foreignKey) { $allForeignKeyColumns = array_merge($allForeignKeyColumns, $foreignKey->getLocalColumns()); } $pkColumns = $table->getPrimaryKey()->getColumns(); sort($pkColumns); sort($allForeignKeyColumns); if ($pkColumns == $allForeignKeyColumns) { if (count($table->getForeignKeys()) > 2) { throw new \InvalidArgumentException("ManyToMany table '" . $name . "' with more or less than two foreign keys are not supported by the Database Reverese Engineering Driver."); } $this->manyToManyTables[$name] = $table; } else { $className = Inflector::classify($name); $this->tables[$name] = $table; $this->classes[$className] = $name; } } }
public function __construct($tableName) { $this->tableName = $tableName; $this->schema = DB::getDoctrineSchemaManager($tableName); $this->table = $this->schema->listTableDetails($tableName); $this->analyzeIndexes(); }
/** * @param $name * * @throws SchemaException */ public function setName($name) { if (!$this->sm->tablesExist($this->prefix . $name)) { throw new SchemaException("Table {$name} does not exist!"); } $this->table = $this->sm->listTableDetails($this->prefix . $name); }
private function reverseEngineerMappingFromDatabase() { if ($this->tables !== null) { return; } foreach ($this->_sm->listTableNames() as $tableName) { $tables[$tableName] = $this->_sm->listTableDetails($tableName); } $this->tables = $this->manyToManyTables = $this->classToTableNames = array(); foreach ($tables as $tableName => $table) { /* @var $table Table */ if ($this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) { $foreignKeys = $table->getForeignKeys(); } else { $foreignKeys = array(); } $allForeignKeyColumns = array(); foreach ($foreignKeys as $foreignKey) { $allForeignKeyColumns = array_merge($allForeignKeyColumns, $foreignKey->getLocalColumns()); } $pkColumns = $table->getPrimaryKey()->getColumns(); sort($pkColumns); sort($allForeignKeyColumns); if ($pkColumns == $allForeignKeyColumns && count($foreignKeys) == 2) { $this->manyToManyTables[$tableName] = $table; } else { // lower-casing is necessary because of Oracle Uppercase Tablenames, // assumption is lower-case + underscore separated. $className = Inflector::classify(strtolower($tableName)); $this->tables[$tableName] = $table; $this->classToTableNames[$className] = $tableName; } } }
/** * Set the table to be manipulated * * @param string $table * @param bool $addPrefix */ public function setName($table, $addPrefix = true) { $this->tableName = $addPrefix ? $this->prefix . $table : $table; //make sure the table exists $this->checkTableExists($this->tableName, true); //use the to schema to get table details so that changes will be calculated $this->fromTable = $this->sm->listTableDetails($this->tableName); $this->toTable = clone $this->fromTable; }
/** * @group DBAL-1009 * * @dataProvider getAlterColumnComment */ public function testAlterColumnComment($comment1, $expectedComment1, $comment2, $expectedComment2) { if (!$this->_conn->getDatabasePlatform()->supportsInlineColumnComments() && !$this->_conn->getDatabasePlatform()->supportsCommentOnStatement() && $this->_conn->getDatabasePlatform()->getName() != 'mssql') { $this->markTestSkipped('Database does not support column comments.'); } $offlineTable = new Table('alter_column_comment_test'); $offlineTable->addColumn('comment1', 'integer', array('comment' => $comment1)); $offlineTable->addColumn('comment2', 'integer', array('comment' => $comment2)); $offlineTable->addColumn('no_comment1', 'integer'); $offlineTable->addColumn('no_comment2', 'integer'); $this->_sm->dropAndCreateTable($offlineTable); $onlineTable = $this->_sm->listTableDetails("alter_column_comment_test"); $this->assertSame($expectedComment1, $onlineTable->getColumn('comment1')->getComment()); $this->assertSame($expectedComment2, $onlineTable->getColumn('comment2')->getComment()); $this->assertNull($onlineTable->getColumn('no_comment1')->getComment()); $this->assertNull($onlineTable->getColumn('no_comment2')->getComment()); $onlineTable->changeColumn('comment1', array('comment' => $comment2)); $onlineTable->changeColumn('comment2', array('comment' => $comment1)); $onlineTable->changeColumn('no_comment1', array('comment' => $comment1)); $onlineTable->changeColumn('no_comment2', array('comment' => $comment2)); $comparator = new Comparator(); $tableDiff = $comparator->diffTable($offlineTable, $onlineTable); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\TableDiff', $tableDiff); $this->_sm->alterTable($tableDiff); $onlineTable = $this->_sm->listTableDetails("alter_column_comment_test"); $this->assertSame($expectedComment2, $onlineTable->getColumn('comment1')->getComment()); $this->assertSame($expectedComment1, $onlineTable->getColumn('comment2')->getComment()); $this->assertSame($expectedComment1, $onlineTable->getColumn('no_comment1')->getComment()); $this->assertSame($expectedComment2, $onlineTable->getColumn('no_comment2')->getComment()); }
/** * @return void * * @throws \Doctrine\ORM\Mapping\MappingException */ private function reverseEngineerMappingFromDatabase() { if ($this->tables !== null) { return; } $tables = array(); foreach ($this->_sm->listTableNames() as $tableName) { $tables[$tableName] = $this->_sm->listTableDetails($tableName); } $this->tables = $this->manyToManyTables = $this->classToTableNames = array(); foreach ($tables as $tableName => $table) { $foreignKeys = $this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints() ? $table->getForeignKeys() : array(); $allForeignKeyColumns = array(); foreach ($foreignKeys as $foreignKey) { $allForeignKeyColumns = array_merge($allForeignKeyColumns, $foreignKey->getLocalColumns()); } if (!$table->hasPrimaryKey()) { throw new MappingException("Table " . $table->getName() . " has no primary key. Doctrine does not " . "support reverse engineering from tables that don't have a primary key."); } $pkColumns = $table->getPrimaryKey()->getColumns(); sort($pkColumns); sort($allForeignKeyColumns); if ($pkColumns == $allForeignKeyColumns && count($foreignKeys) == 2) { $this->manyToManyTables[$tableName] = $table; } else { // lower-casing is necessary because of Oracle Uppercase Tablenames, // assumption is lower-case + underscore separated. $className = $this->getClassNameForTable($tableName); $this->tables[$tableName] = $table; $this->classToTableNames[$className] = $tableName; } } }
public function testAlterTableThrowsExceptionForChangedSpatialType() { $this->setExpectedException('\\RuntimeException', 'The geometry_type of a spatial column cannot be changed (Requested changing type from "POINT" to "LINESTRING" for column "point_2d" in table "points")'); $table = $this->sm->listTableDetails('points'); $tableDiff = new \Doctrine\DBAL\Schema\TableDiff('points'); $tableDiff->fromTable = $table; $tableDiff->changedColumns[] = new ColumnDiff('point_2d', new \Doctrine\DBAL\Schema\Column('point_2d', Type::getType('geometry'), array('customSchemaOptions' => array('geometry_type' => 'LINESTRING'))), array('geometry_type'), $table->getColumn('point_2d')); $this->sm->alterTable($tableDiff); }
/** * @group DBAL-197 */ public function testListTableWithBlob() { $table = new \Doctrine\DBAL\Schema\Table('test_blob_table'); $table->addColumn('id', 'integer', array('comment' => 'This is a comment')); $table->addColumn('binarydata', 'blob', array()); $table->setPrimaryKey(array('id')); $this->_sm->createTable($table); $blobTable = $this->_sm->listTableDetails('test_blob_table'); }
/** * Get the primary key from the table. * * @param string $table * @return string|null */ public function getPrimaryKey($table) { //todo try //return $this->schema->listTableDetails($table)->getPrimaryKey(); try { $primaryKeys = $this->schema->listTableDetails($table)->getPrimaryKeyColumns(); return reset($primaryKeys); } catch (DBALException $e) { return false; } }
public function testAlterTableScenario() { if (!$this->_sm->getDatabasePlatform()->supportsAlterTable()) { $this->markTestSkipped('Alter Table is not supported by this platform.'); } $this->createTestTable('alter_table'); $this->createTestTable('alter_table_foreign'); $table = $this->_sm->listTableDetails('alter_table'); $this->assertTrue($table->hasColumn('id')); $this->assertTrue($table->hasColumn('test')); $this->assertTrue($table->hasColumn('foreign_key_test')); $this->assertEquals(0, count($table->getForeignKeys())); $this->assertEquals(1, count($table->getIndexes())); $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table"); $tableDiff->addedColumns['foo'] = new \Doctrine\DBAL\Schema\Column('foo', Type::getType('integer')); $tableDiff->removedColumns['test'] = $table->getColumn('test'); $this->_sm->alterTable($tableDiff); $table = $this->_sm->listTableDetails('alter_table'); $this->assertFalse($table->hasColumn('test')); $this->assertTrue($table->hasColumn('foo')); $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table"); $tableDiff->addedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo')); $this->_sm->alterTable($tableDiff); $table = $this->_sm->listTableDetails('alter_table'); $this->assertEquals(2, count($table->getIndexes())); $this->assertTrue($table->hasIndex('foo_idx')); $this->assertEquals(array('foo'), array_map('strtolower', $table->getIndex('foo_idx')->getColumns())); $this->assertFalse($table->getIndex('foo_idx')->isPrimary()); $this->assertFalse($table->getIndex('foo_idx')->isUnique()); $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table"); $tableDiff->changedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo', 'foreign_key_test')); $this->_sm->alterTable($tableDiff); $table = $this->_sm->listTableDetails('alter_table'); $this->assertEquals(2, count($table->getIndexes())); $this->assertTrue($table->hasIndex('foo_idx')); $this->assertEquals(array('foo', 'foreign_key_test'), array_map('strtolower', $table->getIndex('foo_idx')->getColumns())); $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table"); $tableDiff->removedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo', 'foreign_key_test')); $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('foreign_key_test'), 'alter_table_foreign', array('id')); $tableDiff->addedForeignKeys[] = $fk; $this->_sm->alterTable($tableDiff); $table = $this->_sm->listTableDetails('alter_table'); // dont check for index size here, some platforms automatically add indexes for foreign keys. $this->assertFalse($table->hasIndex('foo_idx')); $this->assertEquals(1, count($table->getForeignKeys())); $fks = $table->getForeignKeys(); $foreignKey = current($fks); $this->assertEquals('alter_table_foreign', strtolower($foreignKey->getForeignTableName())); $this->assertEquals(array('foreign_key_test'), array_map('strtolower', $foreignKey->getColumns())); $this->assertEquals(array('id'), array_map('strtolower', $foreignKey->getForeignColumns())); }
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()); }
/** * @param EloquentModel $model * @return $this */ protected function setFields(EloquentModel $model) { $tableDetails = $this->manager->listTableDetails($model->getTableName()); $primaryColumnNames = $tableDetails->getPrimaryKey()->getColumns(); $columnNames = []; foreach ($tableDetails->getColumns() as $column) { $model->addProperty(new VirtualPropertyModel($column->getName(), $this->resolveType($column->getType()->getName()))); if (!in_array($column->getName(), $primaryColumnNames)) { $columnNames[] = $column->getName(); } } $fillableProperty = new PropertyModel('fillable'); $fillableProperty->setAccess('protected')->setValue($columnNames)->setDocBlock(new DocBlockModel('@var array')); $model->addProperty($fillableProperty); return $this; }
/** * Create an empty Doctrine DBAL TableDiff from the Blueprint. * * @param Schema_Blueprint $blueprint * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema * @return \Doctrine\DBAL\Schema\TableDiff */ protected function _get_doctrine_table_diff(Schema_Blueprint $blueprint, SchemaManager $schema) { $table = $this->get_table_prefix() . $blueprint->get_table(); $table_diff = new TableDiff($table); $table_diff->fromTable = $schema->listTableDetails($table); return $table_diff; }
/** * Create an empty Doctrine DBAL TableDiff from the Blueprint. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema * @return \Doctrine\DBAL\Schema\TableDiff */ protected function getDoctrineTableDiff(Blueprint $blueprint, SchemaManager $schema) { $table = $this->getTablePrefix() . $blueprint->getTable(); $tableDiff = new TableDiff($table); $tableDiff->fromTable = $schema->listTableDetails($table); return $tableDiff; }
/** * Get the Doctrine table difference for the given changes. * @param \Notadd\Foundation\Database\Schema\Blueprint $blueprint * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema * @return \Doctrine\DBAL\Schema\TableDiff|bool */ protected function getChangedDiff(Blueprint $blueprint, SchemaManager $schema) { $table = $schema->listTableDetails($this->getTablePrefix() . $blueprint->getTable()); return (new Comparator())->diffTable($table, $this->getTableWithColumnChanges($blueprint, $table)); }
/** * {@see AbstractSchemaManager::listTableDetails} */ public function listTableDetails($tableName) { return $this->manager->listTableDetails($this->replacePrefix($tableName)); }