Skip to content

Commit

Permalink
Merge pull request #3418 from AlterTable/column-charset-for-mysql
Browse files Browse the repository at this point in the history
Add column charset for MySQL
  • Loading branch information
morozov authored Dec 30, 2018
2 parents df4071e + 73f38e7 commit f28f3a8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,14 @@ protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
return $this->getUnsignedDeclaration($columnDef) . $autoinc;
}

/**
* {@inheritDoc}
*/
public function getColumnCharsetDeclarationSQL($charset)
{
return 'CHARACTER SET ' . $charset;
}

/**
* {@inheritDoc}
*/
Expand Down
3 changes: 3 additions & 0 deletions lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ protected function _getPortableTableColumnDefinition($tableColumn)

$column = new Column($tableColumn['field'], Type::getType($type), $options);

if (isset($tableColumn['characterset'])) {
$column->setPlatformOption('charset', $tableColumn['characterset']);
}
if (isset($tableColumn['collation'])) {
$column->setPlatformOption('collation', $tableColumn['collation']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,59 @@ public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes()
self::assertFalse($onlineTable->getColumn('def_blob_null')->getNotnull());
}

public function testColumnCharset()
{
$table = new Table('test_column_charset');
$table->addColumn('id', 'integer');
$table->addColumn('no_charset', 'text');
$table->addColumn('foo', 'text')->setPlatformOption('charset', 'ascii');
$table->addColumn('bar', 'text')->setPlatformOption('charset', 'latin1');
$this->schemaManager->dropAndCreateTable($table);

$columns = $this->schemaManager->listTableColumns('test_column_charset');

self::assertFalse($columns['id']->hasPlatformOption('charset'));
self::assertEquals('utf8', $columns['no_charset']->getPlatformOption('charset'));
self::assertEquals('ascii', $columns['foo']->getPlatformOption('charset'));
self::assertEquals('latin1', $columns['bar']->getPlatformOption('charset'));
}

public function testAlterColumnCharset()
{
$tableName = 'test_alter_column_charset';

$table = new Table($tableName);
$table->addColumn('col_text', 'text')->setPlatformOption('charset', 'utf8');

$this->schemaManager->dropAndCreateTable($table);

$diffTable = clone $table;
$diffTable->getColumn('col_text')->setPlatformOption('charset', 'ascii');

$comparator = new Comparator();

$this->schemaManager->alterTable($comparator->diffTable($table, $diffTable));

$table = $this->schemaManager->listTableDetails($tableName);

self::assertEquals('ascii', $table->getColumn('col_text')->getPlatformOption('charset'));
}

public function testColumnCharsetChange()
{
$table = new Table('test_column_charset_change');
$table->addColumn('col_string', 'string')->setLength(100)->setNotnull(true)->setPlatformOption('charset', 'utf8');

$diffTable = clone $table;
$diffTable->getColumn('col_string')->setPlatformOption('charset', 'ascii');

$fromSchema = new Schema([$table]);
$toSchema = new Schema([$diffTable]);

$diff = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform());
self::assertContains('ALTER TABLE test_column_charset_change CHANGE col_string col_string VARCHAR(100) CHARACTER SET ascii NOT NULL', $diff);
}

public function testColumnCollation()
{
$table = new Table('test_collation');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,14 @@ public function testListTableForeignKeysSQLEvaluatesDatabase()
self::assertNotContains('DATABASE()', $sql);
}

public function testColumnCharsetDeclarationSQL() : void
{
self::assertSame(
'CHARACTER SET ascii',
$this->platform->getColumnCharsetDeclarationSQL('ascii')
);
}

public function testSupportsColumnCollation() : void
{
self::assertTrue($this->platform->supportsColumnCollation());
Expand Down

0 comments on commit f28f3a8

Please sign in to comment.