Commit cb440e1e authored by Martin Hasoň's avatar Martin Hasoň

Added support for simple ALTER TABLE command (only rename table and add columns) in Sqlite platform

parent 33555d36
...@@ -1833,6 +1833,10 @@ abstract class AbstractPlatform ...@@ -1833,6 +1833,10 @@ abstract class AbstractPlatform
$default = " DEFAULT ".$field['default']; $default = " DEFAULT ".$field['default'];
} else if ((string)$field['type'] == 'DateTime' && $field['default'] == $this->getCurrentTimestampSQL()) { } else if ((string)$field['type'] == 'DateTime' && $field['default'] == $this->getCurrentTimestampSQL()) {
$default = " DEFAULT ".$this->getCurrentTimestampSQL(); $default = " DEFAULT ".$this->getCurrentTimestampSQL();
} else if ((string)$field['type'] == 'Time' && $field['default'] == $this->getCurrentTimeSQL()) {
$default = " DEFAULT ".$this->getCurrentTimeSQL();
} else if ((string)$field['type'] == 'Date' && $field['default'] == $this->getCurrentDateSQL()) {
$default = " DEFAULT ".$this->getCurrentDateSQL();
} else if ((string) $field['type'] == 'Boolean') { } else if ((string) $field['type'] == 'Boolean') {
$default = " DEFAULT '" . $this->convertBooleans($field['default']) . "'"; $default = " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
} }
......
...@@ -24,6 +24,7 @@ use Doctrine\DBAL\Schema\TableDiff; ...@@ -24,6 +24,7 @@ use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Constraint;
/** /**
* The SqlitePlatform class describes the specifics and dialects of the SQLite * The SqlitePlatform class describes the specifics and dialects of the SQLite
...@@ -32,6 +33,7 @@ use Doctrine\DBAL\Schema\Index; ...@@ -32,6 +33,7 @@ use Doctrine\DBAL\Schema\Index;
* @since 2.0 * @since 2.0
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
* @author Benjamin Eberlei <kontakt@beberlei.de> * @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Martin Hasoň <martin.hason@gmail.com>
* @todo Rename: SQLitePlatform * @todo Rename: SQLitePlatform
*/ */
class SqlitePlatform extends AbstractPlatform class SqlitePlatform extends AbstractPlatform
...@@ -599,6 +601,14 @@ class SqlitePlatform extends AbstractPlatform ...@@ -599,6 +601,14 @@ class SqlitePlatform extends AbstractPlatform
throw new DBALException('Sqlite platform does not support alter foreign key.'); throw new DBALException('Sqlite platform does not support alter foreign key.');
} }
/**
* {@inheritDoc}
*/
public function getCreateConstraintSQL(Constraint $constraint, $table)
{
throw new DBALException('Sqlite platform does not support alter constraint.');
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
...@@ -614,6 +624,11 @@ class SqlitePlatform extends AbstractPlatform ...@@ -614,6 +624,11 @@ class SqlitePlatform extends AbstractPlatform
*/ */
public function getAlterTableSQL(TableDiff $diff) public function getAlterTableSQL(TableDiff $diff)
{ {
$sql = $this->getSimpleAlterTableSQL($diff);
if (false !== $sql) {
return $sql;
}
$fromTable = $diff->fromTable; $fromTable = $diff->fromTable;
if ( ! $fromTable instanceof Table) { if ( ! $fromTable instanceof Table) {
throw new DBALException('Sqlite platform requires for alter table the table diff with reference to original table schema'); throw new DBALException('Sqlite platform requires for alter table the table diff with reference to original table schema');
...@@ -704,6 +719,54 @@ class SqlitePlatform extends AbstractPlatform ...@@ -704,6 +719,54 @@ class SqlitePlatform extends AbstractPlatform
return array_merge($sql, $tableSql, $columnSql); return array_merge($sql, $tableSql, $columnSql);
} }
private function getSimpleAlterTableSQL(TableDiff $diff)
{
if ( ! empty($diff->renamedColumns) || ! empty($diff->addedForeignKeys) || ! empty($diff->addedIndexes)
|| ! empty($diff->changedColumns) || ! empty($diff->changedForeignKeys) || ! empty($diff->changedIndexes)
|| ! empty($diff->removedColumns) || ! empty($diff->removedForeignKeys) || ! empty($diff->removedIndexes)
) {
return false;
}
$table = new Table($diff->name);
$sql = array();
$tableSql = array();
$columnSql = array();
foreach ($diff->addedColumns as $columnName => $column) {
if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
continue;
}
$field = array_merge(array('unique' => null, 'autoincrement' => null, 'default' => null), $column->toArray());
$type = (string) $field['type'];
switch (true) {
case isset($field['columnDefinition']) || $field['autoincrement'] || $field['unique']:
case $type == 'DateTime' && $field['default'] == $this->getCurrentTimestampSQL():
case $type == 'Date' && $field['default'] == $this->getCurrentDateSQL():
case $type == 'Time' && $field['default'] == $this->getCurrentTimeSQL():
return false;
}
$field['name'] = $column->getQuotedName($this);
if (strtolower($field['type']) == 'string' && $field['length'] === null) {
$field['length'] = 255;
}
$sql[] = 'ALTER TABLE '.$table->getQuotedName($this).' ADD COLUMN '.$this->getColumnDeclarationSQL($field['name'], $field);
}
if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
if ($diff->newName !== false) {
$newTable = new Table($diff->newName);
$sql[] = 'ALTER TABLE '.$table->getQuotedName($this).' RENAME TO '.$newTable->getQuotedName($this);
}
}
return array_merge($sql, $tableSql, $columnSql);
}
private function getPrimaryIndex(TableDiff $diff) private function getPrimaryIndex(TableDiff $diff)
{ {
$primaryIndex = array(); $primaryIndex = array();
......
...@@ -2,8 +2,11 @@ ...@@ -2,8 +2,11 @@
namespace Doctrine\Tests\DBAL\Platforms; namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Platforms\SqlitePlatform; use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\DBALException;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
...@@ -113,6 +116,14 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -113,6 +116,14 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
parent::testGeneratesForeignKeyCreationSql(); parent::testGeneratesForeignKeyCreationSql();
} }
/**
* @expectedException \Doctrine\DBAL\DBALException
*/
public function testGeneratesConstraintCreationSql()
{
parent::testGeneratesConstraintCreationSql();
}
public function getGenerateForeignKeySql() public function getGenerateForeignKeySql()
{ {
return null; return null;
...@@ -156,6 +167,41 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -156,6 +167,41 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
); );
} }
public function testAlterTableAddColumns()
{
$diff = new TableDiff('user');
$diff->addedColumns['foo'] = new Column('foo', Type::getType('string'));
$diff->addedColumns['count'] = new Column('count', Type::getType('integer'), array('notnull' => false, 'default' => 1));
$expected = array(
'ALTER TABLE user ADD COLUMN foo VARCHAR(255) NOT NULL',
'ALTER TABLE user ADD COLUMN count INTEGER DEFAULT 1',
);
$this->assertEquals($expected, $this->_platform->getAlterTableSQL($diff));
}
public function testAlterTableAddComplexColumns()
{
$diff = new TableDiff('user');
$diff->addedColumns['time'] = new Column('time', Type::getType('date'), array('default' => 'CURRENT_DATE'));
try {
$this->_platform->getAlterTableSQL($diff);
$this->fail();
} catch (DBALException $e) {
}
$diff = new TableDiff('user');
$diff->addedColumns['id'] = new Column('id', Type::getType('integer'), array('autoincrement' => true));
try {
$this->_platform->getAlterTableSQL($diff);
$this->fail();
} catch (DBALException $e) {
}
}
protected function getQuotedColumnInPrimaryKeySQL() protected function getQuotedColumnInPrimaryKeySQL()
{ {
return array( return array(
......
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