|
7 | 7 | * LICENSE file that was distributed with this source code.
|
8 | 8 | */
|
9 | 9 |
|
| 10 | +use Illuminate\Database\MariaDbConnection; |
10 | 11 | use Illuminate\Database\Schema\Blueprint;
|
11 | 12 | use Illuminate\Database\Schema\Builder;
|
12 | 13 |
|
13 | 14 | return [
|
14 | 15 | 'up' => function (Builder $schema) {
|
15 |
| - $preferences = $schema->getConnection()->getSchemaGrammar()->wrap('preferences'); |
| 16 | + $connection = $schema->getConnection(); |
| 17 | + $driver = $connection->getDriverName(); |
16 | 18 |
|
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"); |
20 | 24 | } else {
|
21 | 25 | $schema->table('users', function (Blueprint $table) {
|
22 | 26 | $table->json('preferences_json')->nullable();
|
23 | 27 | });
|
24 | 28 |
|
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)"), |
28 | 36 | ]);
|
29 | 37 | }
|
30 | 38 |
|
|
39 | 47 | },
|
40 | 48 |
|
41 | 49 | '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'); |
43 | 54 |
|
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"); |
47 | 58 | } else {
|
48 | 59 | $schema->table('users', function (Blueprint $table) {
|
49 | 60 | $table->binary('preferences_binary')->nullable();
|
50 | 61 | });
|
51 | 62 |
|
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), |
55 | 66 | ]);
|
56 | 67 | }
|
57 | 68 |
|
|
0 commit comments