Skip to content

Commit

Permalink
change character set to utf8mb4
Browse files Browse the repository at this point in the history
  • Loading branch information
nojimage committed Sep 2, 2022
1 parent 7a8894c commit 953de48
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]/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:[email protected]/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:[email protected]/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:[email protected]/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
Expand Down
2 changes: 1 addition & 1 deletion config/Migrations/20160527115807_CreateActivityLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions config/Migrations/20180905135501_ChangeEncodings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions config/Migrations/20190122155101_IncreaseIdFieldsLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
62 changes: 62 additions & 0 deletions config/Migrations/20220209074122_AlterCharsetToUtf8mb4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/*
* Copyright 2022 ELASTIC Consultants Inc.
*/

use Cake\Utility\Hash;
use Migrations\AbstractMigration;

/**
* 20220209074122 テーブル、カラムの文字コードをutf8mb4に変更する
*
* @codingStandardsIgnoreStart
*/
class AlterCharsetToUtf8mb4 extends AbstractMigration//@codingStandardsIgnoreEnd
{
/**
* Up Method.
*
* @return void
*/
public function up()
{
$query = $this->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;');
}
}
2 changes: 1 addition & 1 deletion tests/Fixture/ActivityLogsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ActivityLogsFixture extends TestFixture
],
'_options' => [
'engine' => 'InnoDB',
'collation' => 'utf8_general_ci',
'collation' => 'utf8mb4_general_ci',
],
];

Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/ArticlesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]],
];
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/CommentsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/EmptyRecords/ActivityLogsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ActivityLogsFixture extends TestFixture
],
'_options' => [
'engine' => 'InnoDB',
'collation' => 'utf8_general_ci',
'collation' => 'utf8mb4_general_ci',
],
];

Expand Down

0 comments on commit 953de48

Please sign in to comment.