Skip to content

Commit 6323314

Browse files
authored
fix(regression): cannot cast as json on mariadb 10 (#4110)
1 parent 3b69af2 commit 6323314

File tree

2 files changed

+50
-28
lines changed

2 files changed

+50
-28
lines changed

framework/core/migrations/2024_05_05_000001_convert_preferences_to_json_in_users.php

+25-14
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,32 @@
77
* LICENSE file that was distributed with this source code.
88
*/
99

10+
use Illuminate\Database\MariaDbConnection;
1011
use Illuminate\Database\Schema\Blueprint;
1112
use Illuminate\Database\Schema\Builder;
1213

1314
return [
1415
'up' => function (Builder $schema) {
15-
$preferences = $schema->getConnection()->getSchemaGrammar()->wrap('preferences');
16+
$connection = $schema->getConnection();
17+
$driver = $connection->getDriverName();
1618

17-
if ($schema->getConnection()->getDriverName() === 'pgsql') {
18-
$users = $schema->getConnection()->getSchemaGrammar()->wrapTable('users');
19-
$schema->getConnection()->statement("ALTER TABLE $users ALTER COLUMN $preferences TYPE JSON USING $preferences::TEXT::JSON");
19+
$preferences = $connection->getSchemaGrammar()->wrap('preferences');
20+
21+
if ($driver === 'pgsql') {
22+
$users = $connection->getSchemaGrammar()->wrapTable('users');
23+
$connection->statement("ALTER TABLE $users ALTER COLUMN $preferences TYPE JSON USING $preferences::TEXT::JSON");
2024
} else {
2125
$schema->table('users', function (Blueprint $table) {
2226
$table->json('preferences_json')->nullable();
2327
});
2428

25-
if ($schema->getConnection()->getDriverName() === 'mysql') {
26-
$schema->getConnection()->table('users')->update([
27-
'preferences_json' => $schema->getConnection()->raw("CAST(CONVERT($preferences USING utf8mb4) AS JSON)"),
29+
if ($connection instanceof MariaDbConnection) {
30+
$connection->table('users')->update([
31+
'preferences_json' => $connection->raw("IF(JSON_VALID(CONVERT($preferences USING utf8mb4)), CONVERT($preferences USING utf8mb4), NULL)"),
32+
]);
33+
} elseif ($driver === 'mysql') {
34+
$connection->table('users')->update([
35+
'preferences_json' => $connection->raw("CAST(CONVERT($preferences USING utf8mb4) AS JSON)"),
2836
]);
2937
}
3038

@@ -39,19 +47,22 @@
3947
},
4048

4149
'down' => function (Builder $schema) {
42-
$preferences = $schema->getConnection()->getSchemaGrammar()->wrap('preferences');
50+
$connection = $schema->getConnection();
51+
$driver = $connection->getDriverName();
52+
53+
$preferences = $connection->getSchemaGrammar()->wrap('preferences');
4354

44-
if ($schema->getConnection()->getDriverName() === 'pgsql') {
45-
$users = $schema->getConnection()->getSchemaGrammar()->wrapTable('users');
46-
$schema->getConnection()->statement("ALTER TABLE $users ALTER COLUMN $preferences TYPE BYTEA USING preferences::TEXT::BYTEA");
55+
if ($driver === 'pgsql') {
56+
$users = $connection->getSchemaGrammar()->wrapTable('users');
57+
$connection->statement("ALTER TABLE $users ALTER COLUMN $preferences TYPE BYTEA USING preferences::TEXT::BYTEA");
4758
} else {
4859
$schema->table('users', function (Blueprint $table) {
4960
$table->binary('preferences_binary')->nullable();
5061
});
5162

52-
if ($schema->getConnection()->getDriverName() === 'mysql') {
53-
$schema->getConnection()->table('users')->update([
54-
'preferences_binary' => $schema->getConnection()->raw($preferences),
63+
if ($driver === 'mysql') {
64+
$connection->table('users')->update([
65+
'preferences_binary' => $connection->raw($preferences),
5566
]);
5667
}
5768

framework/core/migrations/2024_05_07_000001_convert_data_to_json_in_notifications.php

+25-14
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,31 @@
77
* LICENSE file that was distributed with this source code.
88
*/
99

10+
use Illuminate\Database\MariaDbConnection;
1011
use Illuminate\Database\Schema\Blueprint;
1112
use Illuminate\Database\Schema\Builder;
1213

1314
return [
1415
'up' => function (Builder $schema) {
15-
if ($schema->getConnection()->getDriverName() === 'pgsql') {
16-
$notifications = $schema->getConnection()->getSchemaGrammar()->wrapTable('notifications');
17-
$data = $schema->getConnection()->getSchemaGrammar()->wrap('data');
18-
$schema->getConnection()->statement("ALTER TABLE $notifications ALTER COLUMN $data TYPE JSON USING data::TEXT::JSON");
16+
$connection = $schema->getConnection();
17+
$driver = $connection->getDriverName();
18+
19+
if ($driver === 'pgsql') {
20+
$notifications = $connection->getSchemaGrammar()->wrapTable('notifications');
21+
$data = $connection->getSchemaGrammar()->wrap('data');
22+
$connection->statement("ALTER TABLE $notifications ALTER COLUMN $data TYPE JSON USING data::TEXT::JSON");
1923
} else {
2024
$schema->table('notifications', function (Blueprint $table) {
2125
$table->json('data_json')->nullable();
2226
});
2327

24-
if ($schema->getConnection()->getDriverName() === 'mysql') {
25-
$schema->getConnection()->table('notifications')->update([
26-
'data_json' => $schema->getConnection()->raw('CAST(CONVERT(data USING utf8mb4) AS JSON)'),
28+
if ($connection instanceof MariaDbConnection) {
29+
$connection->table('notifications')->update([
30+
'data_json' => $connection->raw('IF(JSON_VALID(CONVERT(data USING utf8mb4)), CONVERT(data USING utf8mb4), NULL)'),
31+
]);
32+
} elseif ($driver === 'mysql') {
33+
$connection->table('notifications')->update([
34+
'data_json' => $connection->raw('CAST(CONVERT(data USING utf8mb4) AS JSON)'),
2735
]);
2836
}
2937

@@ -38,18 +46,21 @@
3846
},
3947

4048
'down' => function (Builder $schema) {
41-
if ($schema->getConnection()->getDriverName() === 'pgsql') {
42-
$notifications = $schema->getConnection()->getSchemaGrammar()->wrapTable('notifications');
43-
$data = $schema->getConnection()->getSchemaGrammar()->wrap('data');
44-
$schema->getConnection()->statement("ALTER TABLE $notifications ALTER COLUMN $data TYPE BYTEA USING data::TEXT::BYTEA");
49+
$connection = $schema->getConnection();
50+
$driver = $connection->getDriverName();
51+
52+
if ($driver === 'pgsql') {
53+
$notifications = $connection->getSchemaGrammar()->wrapTable('notifications');
54+
$data = $connection->getSchemaGrammar()->wrap('data');
55+
$connection->statement("ALTER TABLE $notifications ALTER COLUMN $data TYPE BYTEA USING data::TEXT::BYTEA");
4556
} else {
4657
$schema->table('notifications', function (Blueprint $table) {
4758
$table->binary('data_binary')->nullable();
4859
});
4960

50-
if ($schema->getConnection()->getDriverName() === 'mysql') {
51-
$schema->getConnection()->table('notifications')->update([
52-
'data_binary' => $schema->getConnection()->raw('data'),
61+
if ($driver === 'mysql') {
62+
$connection->table('notifications')->update([
63+
'data_binary' => $connection->raw('data'),
5364
]);
5465
}
5566

0 commit comments

Comments
 (0)