1
0
mirror of https://github.com/flarum/core.git synced 2025-08-06 16:36:47 +02:00

fix(regression): cannot cast as json on mariadb 10 (#4110)

This commit is contained in:
Daniël Klabbers
2024-11-15 08:46:11 +01:00
committed by GitHub
parent 3b69af2ae6
commit 6323314ad7
2 changed files with 50 additions and 28 deletions

View File

@@ -7,24 +7,32 @@
* LICENSE file that was distributed with this source code. * LICENSE file that was distributed with this source code.
*/ */
use Illuminate\Database\MariaDbConnection;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder; use Illuminate\Database\Schema\Builder;
return [ return [
'up' => function (Builder $schema) { 'up' => function (Builder $schema) {
$preferences = $schema->getConnection()->getSchemaGrammar()->wrap('preferences'); $connection = $schema->getConnection();
$driver = $connection->getDriverName();
if ($schema->getConnection()->getDriverName() === 'pgsql') { $preferences = $connection->getSchemaGrammar()->wrap('preferences');
$users = $schema->getConnection()->getSchemaGrammar()->wrapTable('users');
$schema->getConnection()->statement("ALTER TABLE $users ALTER COLUMN $preferences TYPE JSON USING $preferences::TEXT::JSON"); if ($driver === 'pgsql') {
$users = $connection->getSchemaGrammar()->wrapTable('users');
$connection->statement("ALTER TABLE $users ALTER COLUMN $preferences TYPE JSON USING $preferences::TEXT::JSON");
} else { } else {
$schema->table('users', function (Blueprint $table) { $schema->table('users', function (Blueprint $table) {
$table->json('preferences_json')->nullable(); $table->json('preferences_json')->nullable();
}); });
if ($schema->getConnection()->getDriverName() === 'mysql') { if ($connection instanceof MariaDbConnection) {
$schema->getConnection()->table('users')->update([ $connection->table('users')->update([
'preferences_json' => $schema->getConnection()->raw("CAST(CONVERT($preferences USING utf8mb4) AS JSON)"), 'preferences_json' => $connection->raw("IF(JSON_VALID(CONVERT($preferences USING utf8mb4)), CONVERT($preferences USING utf8mb4), NULL)"),
]);
} elseif ($driver === 'mysql') {
$connection->table('users')->update([
'preferences_json' => $connection->raw("CAST(CONVERT($preferences USING utf8mb4) AS JSON)"),
]); ]);
} }
@@ -39,19 +47,22 @@ return [
}, },
'down' => function (Builder $schema) { 'down' => function (Builder $schema) {
$preferences = $schema->getConnection()->getSchemaGrammar()->wrap('preferences'); $connection = $schema->getConnection();
$driver = $connection->getDriverName();
if ($schema->getConnection()->getDriverName() === 'pgsql') { $preferences = $connection->getSchemaGrammar()->wrap('preferences');
$users = $schema->getConnection()->getSchemaGrammar()->wrapTable('users');
$schema->getConnection()->statement("ALTER TABLE $users ALTER COLUMN $preferences TYPE BYTEA USING preferences::TEXT::BYTEA"); if ($driver === 'pgsql') {
$users = $connection->getSchemaGrammar()->wrapTable('users');
$connection->statement("ALTER TABLE $users ALTER COLUMN $preferences TYPE BYTEA USING preferences::TEXT::BYTEA");
} else { } else {
$schema->table('users', function (Blueprint $table) { $schema->table('users', function (Blueprint $table) {
$table->binary('preferences_binary')->nullable(); $table->binary('preferences_binary')->nullable();
}); });
if ($schema->getConnection()->getDriverName() === 'mysql') { if ($driver === 'mysql') {
$schema->getConnection()->table('users')->update([ $connection->table('users')->update([
'preferences_binary' => $schema->getConnection()->raw($preferences), 'preferences_binary' => $connection->raw($preferences),
]); ]);
} }

View File

@@ -7,23 +7,31 @@
* LICENSE file that was distributed with this source code. * LICENSE file that was distributed with this source code.
*/ */
use Illuminate\Database\MariaDbConnection;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder; use Illuminate\Database\Schema\Builder;
return [ return [
'up' => function (Builder $schema) { 'up' => function (Builder $schema) {
if ($schema->getConnection()->getDriverName() === 'pgsql') { $connection = $schema->getConnection();
$notifications = $schema->getConnection()->getSchemaGrammar()->wrapTable('notifications'); $driver = $connection->getDriverName();
$data = $schema->getConnection()->getSchemaGrammar()->wrap('data');
$schema->getConnection()->statement("ALTER TABLE $notifications ALTER COLUMN $data TYPE JSON USING data::TEXT::JSON"); if ($driver === 'pgsql') {
$notifications = $connection->getSchemaGrammar()->wrapTable('notifications');
$data = $connection->getSchemaGrammar()->wrap('data');
$connection->statement("ALTER TABLE $notifications ALTER COLUMN $data TYPE JSON USING data::TEXT::JSON");
} else { } else {
$schema->table('notifications', function (Blueprint $table) { $schema->table('notifications', function (Blueprint $table) {
$table->json('data_json')->nullable(); $table->json('data_json')->nullable();
}); });
if ($schema->getConnection()->getDriverName() === 'mysql') { if ($connection instanceof MariaDbConnection) {
$schema->getConnection()->table('notifications')->update([ $connection->table('notifications')->update([
'data_json' => $schema->getConnection()->raw('CAST(CONVERT(data USING utf8mb4) AS JSON)'), 'data_json' => $connection->raw('IF(JSON_VALID(CONVERT(data USING utf8mb4)), CONVERT(data USING utf8mb4), NULL)'),
]);
} elseif ($driver === 'mysql') {
$connection->table('notifications')->update([
'data_json' => $connection->raw('CAST(CONVERT(data USING utf8mb4) AS JSON)'),
]); ]);
} }
@@ -38,18 +46,21 @@ return [
}, },
'down' => function (Builder $schema) { 'down' => function (Builder $schema) {
if ($schema->getConnection()->getDriverName() === 'pgsql') { $connection = $schema->getConnection();
$notifications = $schema->getConnection()->getSchemaGrammar()->wrapTable('notifications'); $driver = $connection->getDriverName();
$data = $schema->getConnection()->getSchemaGrammar()->wrap('data');
$schema->getConnection()->statement("ALTER TABLE $notifications ALTER COLUMN $data TYPE BYTEA USING data::TEXT::BYTEA"); if ($driver === 'pgsql') {
$notifications = $connection->getSchemaGrammar()->wrapTable('notifications');
$data = $connection->getSchemaGrammar()->wrap('data');
$connection->statement("ALTER TABLE $notifications ALTER COLUMN $data TYPE BYTEA USING data::TEXT::BYTEA");
} else { } else {
$schema->table('notifications', function (Blueprint $table) { $schema->table('notifications', function (Blueprint $table) {
$table->binary('data_binary')->nullable(); $table->binary('data_binary')->nullable();
}); });
if ($schema->getConnection()->getDriverName() === 'mysql') { if ($driver === 'mysql') {
$schema->getConnection()->table('notifications')->update([ $connection->table('notifications')->update([
'data_binary' => $schema->getConnection()->raw('data'), 'data_binary' => $connection->raw('data'),
]); ]);
} }