diff --git a/db/migrations/2020_04_07_000000_change_mysql_database_encoding_to_utf8mb4.php b/db/migrations/2020_04_07_000000_change_mysql_database_encoding_to_utf8mb4.php new file mode 100644 index 00000000..575cab30 --- /dev/null +++ b/db/migrations/2020_04_07_000000_change_mysql_database_encoding_to_utf8mb4.php @@ -0,0 +1,38 @@ +schema->getConnection(); + if (!$connection->getQueryGrammar() instanceof MySqlGrammar) { + return; + } + + $connection->unprepared('ALTER DATABASE CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci'); + + foreach ($connection->getDoctrineSchemaManager()->listTableNames() as $table) { + $connection->unprepared( + 'ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci' + ); + } + } + + /** + * Reverse the migration + */ + public function down(): void + { + // As utf8mb4 is a superset of utf8, there is nothing to do here + } +} diff --git a/src/Database/DatabaseServiceProvider.php b/src/Database/DatabaseServiceProvider.php index b5225015..715b04a4 100644 --- a/src/Database/DatabaseServiceProvider.php +++ b/src/Database/DatabaseServiceProvider.php @@ -24,8 +24,8 @@ class DatabaseServiceProvider extends ServiceProvider 'database' => '', 'username' => '', 'password' => '', - 'charset' => 'utf8', - 'collation' => 'utf8_unicode_ci', + 'charset' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', 'timezone' => $now->format('P'), 'prefix' => '', ], $dbConfig)); diff --git a/tests/Unit/HasDatabase.php b/tests/Unit/HasDatabase.php index 3fe29e4a..9729af79 100644 --- a/tests/Unit/HasDatabase.php +++ b/tests/Unit/HasDatabase.php @@ -45,6 +45,7 @@ trait HasDatabase ['migration' => '2018_01_01_000005_add_angel_supporter_permissions'], ['migration' => '2018_12_27_000000_fix_missing_arrival_dates'], ['migration' => '2019_09_07_000000_migrate_admin_schedule_permissions'], + ['migration' => '2020_04_07_000000_change_mysql_database_encoding_to_utf8mb4'], ] );