From 953de48678199b6f8c46e2e64df106bedfbf5d52 Mon Sep 17 00:00:00 2001 From: nojimage Date: Fri, 2 Sep 2022 23:54:17 +0900 Subject: [PATCH] change character set to utf8mb4 --- .github/workflows/ci.yml | 4 +- .../20160527115807_CreateActivityLogs.php | 2 +- .../20180905135501_ChangeEncodings.php | 6 +- .../20190122155101_IncreaseIdFieldsLength.php | 4 +- ...210325152201_IncreaseModelFieldsLength.php | 4 +- .../20220209074122_AlterCharsetToUtf8mb4.php | 62 +++++++++++++++++++ tests/Fixture/ActivityLogsFixture.php | 2 +- tests/Fixture/ArticlesFixture.php | 4 +- tests/Fixture/CommentsFixture.php | 2 +- .../EmptyRecords/ActivityLogsFixture.php | 2 +- 10 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 config/Migrations/20220209074122_AlterCharsetToUtf8mb4.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a5273ad..ced1903 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -135,8 +135,8 @@ jobs: MEMCACHED_PORT: ${{ job.services.memcached.ports['11211'] }} run: | if [[ ${{ matrix.db-type }} == 'sqlite' ]]; then export DB_URL='sqlite:///:memory:'; fi - if [[ ${{ matrix.db-type }} == 'mysql' ]]; then export DB_DSN='mysql://root:root@127.0.0.1/cakephp_test?init[]=SET sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"'; fi - if [[ ${{ matrix.db-type }} == 'mysql:5.7' ]]; then export DB_DSN='mysql://root:root@127.0.0.1/cakephp_test?init[]=SET sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"'; fi + if [[ ${{ matrix.db-type }} == 'mysql' ]]; then export DB_DSN='mysql://root:root@127.0.0.1/cakephp_test?encoding=utf8mb4&init[]=SET sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"'; fi + if [[ ${{ matrix.db-type }} == 'mysql:5.7' ]]; then export DB_DSN='mysql://root:root@127.0.0.1/cakephp_test?encoding=utf8mb4&init[]=SET sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"'; fi if [[ ${{ matrix.coverage }} == 'coverage' ]]; then export CODECOVERAGE=1 && vendor/bin/phpunit --stderr --verbose --coverage-clover=coverage.xml diff --git a/config/Migrations/20160527115807_CreateActivityLogs.php b/config/Migrations/20160527115807_CreateActivityLogs.php index c845a44..f67e384 100644 --- a/config/Migrations/20160527115807_CreateActivityLogs.php +++ b/config/Migrations/20160527115807_CreateActivityLogs.php @@ -7,7 +7,7 @@ class CreateActivityLogs extends AbstractMigration public function change() { - $table = $this->table('activity_logs', ['id' => false, 'collation' => 'utf8_general_ci']) + $table = $this->table('activity_logs', ['id' => false, 'collation' => 'utf8mb4_general_ci']) ->addColumn('id', 'biginteger', [ 'autoIncrement' => true, 'limit' => 20, diff --git a/config/Migrations/20180905135501_ChangeEncodings.php b/config/Migrations/20180905135501_ChangeEncodings.php index 818a474..3216931 100644 --- a/config/Migrations/20180905135501_ChangeEncodings.php +++ b/config/Migrations/20180905135501_ChangeEncodings.php @@ -7,16 +7,16 @@ class ChangeEncodings extends AbstractMigration public function up() { - $table = $this->table('activity_logs', ['id' => false, 'collation' => 'utf8_general_ci']); + $table = $this->table('activity_logs', ['id' => false, 'collation' => 'utf8mb4_general_ci']); $table ->changeColumn('message', 'text', [ - 'collation' => 'utf8_general_ci', + 'collation' => 'utf8mb4_general_ci', 'default' => null, 'limit' => null, 'null' => true, ]) ->changeColumn('data', 'text', [ - 'collation' => 'utf8_general_ci', + 'collation' => 'utf8mb4_general_ci', 'comment' => 'json encoded data', 'default' => null, 'limit' => null, diff --git a/config/Migrations/20190122155101_IncreaseIdFieldsLength.php b/config/Migrations/20190122155101_IncreaseIdFieldsLength.php index e42ce40..0f22d59 100644 --- a/config/Migrations/20190122155101_IncreaseIdFieldsLength.php +++ b/config/Migrations/20190122155101_IncreaseIdFieldsLength.php @@ -10,7 +10,7 @@ class IncreaseIdFieldsLength extends AbstractMigration public function up() { - $table = $this->table('activity_logs', ['id' => false, 'collation' => 'utf8_general_ci']); + $table = $this->table('activity_logs', ['id' => false, 'collation' => 'utf8mb4_general_ci']); $table ->changeColumn('scope_id', 'string', [ 'default' => null, @@ -32,7 +32,7 @@ public function up() public function down() { - $table = $this->table('activity_logs', ['id' => false, 'collation' => 'utf8_general_ci']); + $table = $this->table('activity_logs', ['id' => false, 'collation' => 'utf8mb4_general_ci']); $table ->changeColumn('scope_id', 'string', [ 'default' => null, diff --git a/config/Migrations/20210325152201_IncreaseModelFieldsLength.php b/config/Migrations/20210325152201_IncreaseModelFieldsLength.php index 9c8e1b1..917295a 100644 --- a/config/Migrations/20210325152201_IncreaseModelFieldsLength.php +++ b/config/Migrations/20210325152201_IncreaseModelFieldsLength.php @@ -12,7 +12,7 @@ class IncreaseModelFieldsLength extends AbstractMigration public function up() { - $table = $this->table('activity_logs', ['id' => false, 'collation' => 'utf8_general_ci']); + $table = $this->table('activity_logs', ['id' => false, 'collation' => 'utf8mb4_general_ci']); $table ->changeColumn('scope_model', 'string', [ 'default' => null, @@ -34,7 +34,7 @@ public function up() public function down() { - $table = $this->table('activity_logs', ['id' => false, 'collation' => 'utf8_general_ci']); + $table = $this->table('activity_logs', ['id' => false, 'collation' => 'utf8mb4_general_ci']); $table ->changeColumn('scope_model', 'string', [ 'default' => null, diff --git a/config/Migrations/20220209074122_AlterCharsetToUtf8mb4.php b/config/Migrations/20220209074122_AlterCharsetToUtf8mb4.php new file mode 100644 index 0000000..eff500b --- /dev/null +++ b/config/Migrations/20220209074122_AlterCharsetToUtf8mb4.php @@ -0,0 +1,62 @@ +getQueryBuilder(); + $schemaCollection = $query->getConnection()->getSchemaCollection(); + if ($schemaCollection === null) { + throw new Exception('$schemaCollection not exists.'); + } + + $this->execute('SET FOREIGN_KEY_CHECKS=0;'); + + $tableNames = $schemaCollection->listTables(); + $tableNames = array_filter($tableNames, static function ($tableName) { + return in_array($tableName, ['activity_logs'], true) + && !preg_match('/_phinxlog$/', $tableName); + }); + foreach ($tableNames as $tableName) { + // テーブルの文字照合順変更 + $tableSchema = $schemaCollection->describe($tableName); + $tableCollation = Hash::get($tableSchema->getOptions(), 'collation', ''); + if (preg_match('/\Autf8_/', $tableCollation)) { + $this->execute(sprintf('ALTER TABLE `%s` CHARACTER SET %s COLLATE %s', $tableName, 'utf8mb4', 'utf8mb4_general_ci')); + } + + // カラムの文字照合順序変更 + $table = $this->table($tableName); + $colNames = $tableSchema->columns(); + foreach ($colNames as $colName) { + $column = $tableSchema->getColumn($colName); + $columnCollation = Hash::get($column, 'collate', ''); + if (preg_match('/\Autf8_/', $columnCollation)) { + $colType = $column['type']; + $colOpts = $column; + unset($colOpts['type'], $colOpts['collate'], $colOpts['fixed']); + $table->changeColumn($colName, $colType, ['collation' => 'utf8mb4_general_ci', 'encoding' => 'utf8mb4'] + $colOpts); + } + } + $table->update(); + } + + $this->execute('SET FOREIGN_KEY_CHECKS=1;'); + } +} diff --git a/tests/Fixture/ActivityLogsFixture.php b/tests/Fixture/ActivityLogsFixture.php index 29be3cb..578175d 100644 --- a/tests/Fixture/ActivityLogsFixture.php +++ b/tests/Fixture/ActivityLogsFixture.php @@ -36,7 +36,7 @@ class ActivityLogsFixture extends TestFixture ], '_options' => [ 'engine' => 'InnoDB', - 'collation' => 'utf8_general_ci', + 'collation' => 'utf8mb4_general_ci', ], ]; diff --git a/tests/Fixture/ArticlesFixture.php b/tests/Fixture/ArticlesFixture.php index c2ef48f..fd83c69 100644 --- a/tests/Fixture/ArticlesFixture.php +++ b/tests/Fixture/ArticlesFixture.php @@ -17,8 +17,8 @@ class ArticlesFixture extends TestFixture public $fields = [ 'id' => ['type' => 'integer'], 'author_id' => ['type' => 'integer', 'null' => true], - 'title' => ['type' => 'string', 'null' => true], - 'body' => 'text', + 'title' => ['type' => 'string', 'null' => true, 'collation' => 'utf8mb4_general_ci'], + 'body' => ['type' => 'text', 'null' => true, 'collation' => 'utf8mb4_general_ci'], 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'], '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]], ]; diff --git a/tests/Fixture/CommentsFixture.php b/tests/Fixture/CommentsFixture.php index a3c02a6..43792d9 100644 --- a/tests/Fixture/CommentsFixture.php +++ b/tests/Fixture/CommentsFixture.php @@ -18,7 +18,7 @@ class CommentsFixture extends TestFixture 'id' => ['type' => 'integer'], 'article_id' => ['type' => 'integer', 'null' => false], 'user_id' => ['type' => 'integer', 'null' => false], - 'comment' => ['type' => 'text'], + 'comment' => ['type' => 'text', 'collation' => 'utf8mb4_general_ci'], 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'], 'created' => ['type' => 'datetime'], 'updated' => ['type' => 'datetime'], diff --git a/tests/Fixture/EmptyRecords/ActivityLogsFixture.php b/tests/Fixture/EmptyRecords/ActivityLogsFixture.php index e34cc61..151d58a 100644 --- a/tests/Fixture/EmptyRecords/ActivityLogsFixture.php +++ b/tests/Fixture/EmptyRecords/ActivityLogsFixture.php @@ -34,7 +34,7 @@ class ActivityLogsFixture extends TestFixture ], '_options' => [ 'engine' => 'InnoDB', - 'collation' => 'utf8_general_ci', + 'collation' => 'utf8mb4_general_ci', ], ];