From 99e30508465d7a0c6499f94d4e83bc99f85d0f4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Klabbers?= Date: Wed, 13 Nov 2024 15:42:58 +0100 Subject: [PATCH] fix: cannot cast as json on mariadb 10 Fixes #4109 --- ...24_05_05_000001_convert_preferences_to_json_in_users.php | 6 +++++- ...4_05_07_000001_convert_data_to_json_in_notifications.php | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/framework/core/migrations/2024_05_05_000001_convert_preferences_to_json_in_users.php b/framework/core/migrations/2024_05_05_000001_convert_preferences_to_json_in_users.php index 6079b0e9de..4712306f95 100644 --- a/framework/core/migrations/2024_05_05_000001_convert_preferences_to_json_in_users.php +++ b/framework/core/migrations/2024_05_05_000001_convert_preferences_to_json_in_users.php @@ -22,7 +22,11 @@ $table->json('preferences_json')->nullable(); }); - if ($schema->getConnection()->getDriverName() === 'mysql') { + if ($schema->getConnection()?->isMaria()) { + $schema->getConnection()->table('users')->update([ + 'preferences_json' => $schema->getConnection()->raw("IF(JSON_VALID(CONVERT($preferences USING utf8mb4)), CONVERT($preferences USING utf8mb4), NULL)"), + ]); + } elseif ($schema->getConnection()->getDriverName() === 'mysql') { $schema->getConnection()->table('users')->update([ 'preferences_json' => $schema->getConnection()->raw("CAST(CONVERT($preferences USING utf8mb4) AS JSON)"), ]); diff --git a/framework/core/migrations/2024_05_07_000001_convert_data_to_json_in_notifications.php b/framework/core/migrations/2024_05_07_000001_convert_data_to_json_in_notifications.php index 82ca9e76e8..be1519730f 100644 --- a/framework/core/migrations/2024_05_07_000001_convert_data_to_json_in_notifications.php +++ b/framework/core/migrations/2024_05_07_000001_convert_data_to_json_in_notifications.php @@ -21,7 +21,11 @@ $table->json('data_json')->nullable(); }); - if ($schema->getConnection()->getDriverName() === 'mysql') { + if ($schema->getConnection()?->isMaria()) { + $schema->getConnection()->table('notifications')->update([ + 'data_json' => $schema->getConnection()->raw("IF(JSON_VALID(CONVERT(data USING utf8mb4)), CONVERT(data USING utf8mb4), NULL)"), + ]); + } elseif ($schema->getConnection()->getDriverName() === 'mysql') { $schema->getConnection()->table('notifications')->update([ 'data_json' => $schema->getConnection()->raw('CAST(CONVERT(data USING utf8mb4) AS JSON)'), ]);