Commit 14333d9c authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge pull request #514 from deeky666/DBAL-789

[DBAL-789] Fix default values for TEXT/BLOB column type on MySQL
parents 0a7df7c5 30359f81
......@@ -23,6 +23,8 @@ use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\BlobType;
use Doctrine\DBAL\Types\TextType;
/**
* The MySqlPlatform provides the behavior, features and SQL dialect of the
......@@ -463,6 +465,19 @@ class MySqlPlatform extends AbstractPlatform
return $sql;
}
/**
* {@inheritdoc}
*/
public function getDefaultValueDeclarationSQL($field)
{
// Unset the default value if the given field definition does not allow default values.
if ($field['type'] instanceof TextType || $field['type'] instanceof BlobType) {
$field['default'] = null;
}
return parent::getDefaultValueDeclarationSQL($field);
}
/**
* Build SQL for table options
*
......@@ -570,6 +585,15 @@ class MySqlPlatform extends AbstractPlatform
/* @var $columnDiff \Doctrine\DBAL\Schema\ColumnDiff */
$column = $columnDiff->column;
$columnArray = $column->toArray();
// Don't propagate default value changes for unsupported column types.
if ($columnDiff->hasChanged('default') &&
count($columnDiff->changedProperties) === 1 &&
($columnArray['type'] instanceof TextType || $columnArray['type'] instanceof BlobType)
) {
continue;
}
$columnArray['comment'] = $this->getColumnComment($column);
$queryParts[] = 'CHANGE ' . ($columnDiff->getOldColumnName()->getQuotedName($this)) . ' '
. $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
......
......@@ -117,4 +117,40 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->assertFalse($table->hasPrimaryKey());
$this->assertFalse($table->getColumn('id')->getAutoincrement());
}
/**
* @group DBAL-789
*/
public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes()
{
$table = new Table("text_blob_default_value");
$table->addColumn('def_text', 'text', array('default' => 'def'));
$table->addColumn('def_text_null', 'text', array('notnull' => false, 'default' => 'def'));
$table->addColumn('def_blob', 'blob', array('default' => 'def'));
$table->addColumn('def_blob_null', 'blob', array('notnull' => false, 'default' => 'def'));
$this->_sm->dropAndCreateTable($table);
$onlineTable = $this->_sm->listTableDetails("text_blob_default_value");
$this->assertNull($onlineTable->getColumn('def_text')->getDefault());
$this->assertNull($onlineTable->getColumn('def_text_null')->getDefault());
$this->assertFalse($onlineTable->getColumn('def_text_null')->getNotnull());
$this->assertNull($onlineTable->getColumn('def_blob')->getDefault());
$this->assertNull($onlineTable->getColumn('def_blob_null')->getDefault());
$this->assertFalse($onlineTable->getColumn('def_blob_null')->getNotnull());
$comparator = new Comparator();
$this->_sm->alterTable($comparator->diffTable($table, $onlineTable));
$onlineTable = $this->_sm->listTableDetails("text_blob_default_value");
$this->assertNull($onlineTable->getColumn('def_text')->getDefault());
$this->assertNull($onlineTable->getColumn('def_text_null')->getDefault());
$this->assertFalse($onlineTable->getColumn('def_text_null')->getNotnull());
$this->assertNull($onlineTable->getColumn('def_blob')->getDefault());
$this->assertNull($onlineTable->getColumn('def_blob_null')->getDefault());
$this->assertFalse($onlineTable->getColumn('def_blob_null')->getNotnull());
}
}
......@@ -501,4 +501,28 @@ abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase
'CREATE INDEX `bar` ON `table` (id)',
);
}
public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes()
{
$table = new Table("text_blob_default_value");
$table->addColumn('def_text', 'text', array('default' => 'def'));
$table->addColumn('def_text_null', 'text', array('notnull' => false, 'default' => 'def'));
$table->addColumn('def_blob', 'blob', array('default' => 'def'));
$table->addColumn('def_blob_null', 'blob', array('notnull' => false, 'default' => 'def'));
$this->assertSame(
array('CREATE TABLE text_blob_default_value (def_text LONGTEXT NOT NULL, def_text_null LONGTEXT DEFAULT NULL, def_blob LONGBLOB NOT NULL, def_blob_null LONGBLOB DEFAULT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'),
$this->_platform->getCreateTableSQL($table)
);
$diffTable = clone $table;
$diffTable->changeColumn('def_text', array('default' => null));
$diffTable->changeColumn('def_text_null', array('default' => null));
$diffTable->changeColumn('def_blob', array('default' => null));
$diffTable->changeColumn('def_blob_null', array('default' => null));
$comparator = new Comparator();
$this->assertEmpty($this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable)));
}
}
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