From 73f38e722421fed5f00ff3fba76c80cf1a7545b4 Mon Sep 17 00:00:00 2001 From: AlterTable Date: Fri, 28 Dec 2018 11:09:50 +0800 Subject: [PATCH] Add column charset for MySql --- lib/Doctrine/DBAL/Platforms/MySqlPlatform.php | 8 +++ .../DBAL/Schema/MySqlSchemaManager.php | 3 ++ .../Schema/MySqlSchemaManagerTest.php | 53 +++++++++++++++++++ .../AbstractMySQLPlatformTestCase.php | 8 +++ 4 files changed, 72 insertions(+) diff --git a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php index 777d11216f8..289dca9e7d7 100644 --- a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php @@ -953,6 +953,14 @@ protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef) return $this->getUnsignedDeclaration($columnDef) . $autoinc; } + /** + * {@inheritDoc} + */ + public function getColumnCharsetDeclarationSQL($charset) + { + return 'CHARACTER SET ' . $charset; + } + /** * {@inheritDoc} */ diff --git a/lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php b/lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php index 7bcd5533efc..85b5b5c4734 100644 --- a/lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php @@ -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']); } diff --git a/tests/Doctrine/Tests/DBAL/Functional/Schema/MySqlSchemaManagerTest.php b/tests/Doctrine/Tests/DBAL/Functional/Schema/MySqlSchemaManagerTest.php index 90ade2e86b8..ef457f0c590 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Schema/MySqlSchemaManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Schema/MySqlSchemaManagerTest.php @@ -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'); diff --git a/tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php b/tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php index 63148215f22..6dec263df34 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php @@ -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());