Commit e3314dae authored by beberlei's avatar beberlei

[2.0] DC-169 - New method AbstractSchemaManager::createSchema() which creates...

[2.0] DC-169 - New method AbstractSchemaManager::createSchema() which creates a schema instance with full tables and sequences, additionally listTables() was rewritten to return a full Table instance instead of just the table names.
parent b7652f1f
......@@ -367,7 +367,7 @@ class MySqlPlatform extends AbstractPlatform
public function getListTablesSql()
{
return 'SHOW TABLES';
return 'SHOW FULL TABLES WHERE Table_type = "BASE TABLE"';
}
public function getListTableColumnsSql($table)
......
......@@ -43,14 +43,14 @@ abstract class AbstractSchemaManager
/**
* Holds instance of the Doctrine connection for this schema manager
*
* @var object \Doctrine\DBAL\Connection
* @var \Doctrine\DBAL\Connection
*/
protected $_conn;
/**
* Holds instance of the database platform used for this schema manager
*
* @var string
* @var \Doctrine\DBAL\Platform\AbstractPlatform
*/
protected $_platform;
......@@ -228,7 +228,7 @@ abstract class AbstractSchemaManager
/**
* List the tables for this connection
*
* @return array $tables
* @return array(int => Table)
*/
public function listTables()
{
......@@ -236,7 +236,28 @@ abstract class AbstractSchemaManager
$tables = $this->_conn->fetchAll($sql);
return $this->_getPortableTablesList($tables);
$tableNames = $this->_getPortableTablesList($tables);
$tables = array();
foreach ($tableNames AS $tableName) {
$columns = $this->listTableColumns($tableName);
$foreignKeys = array();
if ($this->_platform->supportsForeignKeyConstraints()) {
$foreignKeys = $this->listTableForeignKeys($tableName);
}
$indexes = $this->listTableIndexes($tableName);
$idGeneratorType = Table::ID_NONE;
foreach ($columns AS $column) {
if ($column->hasPlatformOption('autoincrement') && $column->getPlatformOption('autoincrement')) {
$idGeneratorType = Table::ID_IDENTITY;
}
}
$tables[] = new Table($tableName, $columns, $indexes, $foreignKeys, $idGeneratorType, array());
}
return $tables;
}
/**
......@@ -1028,4 +1049,20 @@ abstract class AbstractSchemaManager
$this->_conn->executeUpdate($query);
}
}
/**
* Create a schema instance for the current database.
*
* @return Schema
*/
public function createSchema()
{
$sequences = array();
if($this->_platform->supportsSequences()) {
$sequences = $this->listSequences();
}
$tables = $this->listTables();
return new Schema($tables, $sequences);
}
}
\ No newline at end of file
......@@ -44,7 +44,7 @@ class MySqlSchemaManager extends AbstractSchemaManager
protected function _getPortableTableDefinition($table)
{
return end($table);
return array_shift($table);
}
protected function _getPortableUserDefinition($user)
......
......@@ -201,25 +201,30 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
$scale = null;
$dbType = strtolower($tableColumn['type']);
$autoincrement = false;
switch ($dbType) {
case 'smallint':
case 'int2':
$type = 'smallint';
$length = null;
break;
case 'serial':
case 'serial4':
$autoincrement = true;
// break missing intentionally
case 'int':
case 'int4':
case 'integer':
case 'serial':
case 'serial4':
$type = 'integer';
$length = null;
break;
case 'bigint':
case 'int8':
case 'bigserial':
case 'serial8':
$autoincrement = true;
// break missing intentionally
case 'bigint':
case 'int8':
$type = 'bigint';
$length = null;
break;
......@@ -320,7 +325,9 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
'scale' => $scale,
'fixed' => $fixed,
'unsigned' => false,
'platformDetails' => array(),
'platformDetails' => array(
'autoincrement' => $autoincrement,
),
);
return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options);
......
......@@ -65,7 +65,7 @@ class Schema extends AbstractAsset
*/
protected function _addTable(Table $table)
{
$tableName = $table->getName();
$tableName = strtolower($table->getName());
if(isset($this->_tables[$tableName])) {
throw SchemaException::tableAlreadyExists($tableName);
}
......@@ -78,7 +78,7 @@ class Schema extends AbstractAsset
*/
protected function _addSequence(Sequence $sequence)
{
$seqName = $sequence->getName();
$seqName = strtolower($sequence->getName());
if (isset($this->_sequences[$seqName])) {
throw SchemaException::sequenceAlreadyExists($seqName);
}
......@@ -101,6 +101,7 @@ class Schema extends AbstractAsset
*/
public function getTable($tableName)
{
$tableName = strtolower($tableName);
if (!isset($this->_tables[$tableName])) {
throw SchemaException::tableDoesNotExist($tableName);
}
......@@ -116,6 +117,7 @@ class Schema extends AbstractAsset
*/
public function hasTable($tableName)
{
$tableName = strtolower($tableName);
return isset($this->_tables[$tableName]);
}
......@@ -125,6 +127,7 @@ class Schema extends AbstractAsset
*/
public function hasSequence($sequenceName)
{
$sequenceName = strtolower($sequenceName);
return isset($this->_sequences[$sequenceName]);
}
......@@ -135,6 +138,7 @@ class Schema extends AbstractAsset
*/
public function getSequence($sequenceName)
{
$sequenceName = strtolower($sequenceName);
if(!$this->hasSequence($sequenceName)) {
throw SchemaException::sequenceDoesNotExist($sequenceName);
}
......@@ -187,6 +191,7 @@ class Schema extends AbstractAsset
*/
public function dropTable($tableName)
{
$tableName = strtolower($tableName);
$table = $this->getTable($tableName);
unset($this->_tables[$tableName]);
return $this;
......@@ -213,6 +218,7 @@ class Schema extends AbstractAsset
*/
public function dropSequence($sequenceName)
{
$sequenceName = strtolower($sequenceName);
unset($this->_sequences[$sequenceName]);
return $this;
}
......
......@@ -251,7 +251,7 @@ class SqliteSchemaManager extends AbstractSchemaManager
'precision' => $precision,
'scale' => $scale,
'platformDetails' => array(
'primary' => (bool) $tableColumn['pk'],
'autoincrement' => (bool) $tableColumn['pk'],
),
);
......
......@@ -215,6 +215,7 @@ class Table extends AbstractAsset
*/
public function renameColumn($oldColumnName, $newColumnName)
{
$columnName = strtolower($columnName);
$column = $this->getColumn($oldColumnName);
$this->dropColumn($oldColumnName);
......@@ -231,6 +232,7 @@ class Table extends AbstractAsset
*/
public function changeColumn($columnName, array $options)
{
$columnName = strtolower($columnName);
$column = $this->getColumn($columnName);
$column->setOptions($options);
return $this;
......@@ -244,6 +246,7 @@ class Table extends AbstractAsset
*/
public function dropColumn($columnName)
{
$columnName = strtolower($columnName);
$column = $this->getColumn($columnName);
unset($this->_columns[$columnName]);
return $this;
......@@ -320,7 +323,7 @@ class Table extends AbstractAsset
*/
protected function _addColumn(Column $column)
{
$columnName = $column->getName();
$columnName = strtolower($column->getName());
if (isset($this->_columns[$columnName])) {
throw SchemaException::columnAlreadyExists($this->_name, $columnName);
}
......@@ -415,6 +418,7 @@ class Table extends AbstractAsset
*/
public function hasColumn($columnName)
{
$columnName = strtolower($columnName);
return isset($this->_columns[$columnName]);
}
......@@ -426,6 +430,7 @@ class Table extends AbstractAsset
*/
public function getColumn($columnName)
{
$columnName = strtolower($columnName);
if (!$this->hasColumn($columnName)) {
throw SchemaException::columnDoesNotExist($columnName);
}
......
......@@ -73,6 +73,7 @@ class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->_sm->renameTable('list_tables_test', 'list_tables_test_new_name');
$tables = $this->_sm->listTables();
$this->assertEquals(true, in_array('LIST_TABLES_TEST_NEW_NAME', $tables));
$this->assertHasTable($tables, 'list_tables_test_new_name');
}
}
\ No newline at end of file
......@@ -63,8 +63,19 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$tables = $this->_sm->listTables();
$this->assertType('array', $tables);
$tables = \array_map('strtolower', $tables);
$this->assertEquals(true, in_array('list_tables_test', $tables));
$this->assertTrue(count($tables) > 0);
$foundTable = false;
foreach ($tables AS $table) {
$this->assertType('Doctrine\DBAL\Schema\Table', $table);
if (strtolower($table->getName()) == 'list_tables_test') {
$foundTable = true;
$this->assertTrue($table->hasColumn('id'));
$this->assertTrue($table->hasColumn('test'));
$this->assertTrue($table->hasColumn('foreign_key_test'));
}
}
}
public function testListTableColumns()
......@@ -287,6 +298,15 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$this->assertTrue($found, "'test_create_view' View was not found in listViews().");
}
public function testCreateSchema()
{
$this->createTestTable('test_table');
$schema = $this->_sm->createSchema();
$this->assertTrue($schema->hasTable('test_table'));
}
/**
* @var \Doctrine\DBAL\Schema\AbstractSchemaManager
*/
......@@ -338,4 +358,16 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$this->_sm->dropAndCreateTable($name, $columns, $options);
}
protected function assertHasTable($tables, $tableName)
{
$foundTable = false;
foreach ($tables AS $table) {
$this->assertType('Doctrine\DBAL\Schema\Table', $table, 'No Table instance was found in tables array.');
if (strtolower($table->getName()) == 'list_tables_test_new_name') {
$foundTable = true;
}
}
$this->assertTrue($foundTable, "Could not find new table");
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment