Skip to content

Commit 976eac5

Browse files
authored
Add migrations and JSON info to upgrade guide (#20678)
1 parent 985ff9e commit 976eac5

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

framework/UPGRADE.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@ Upgrade from Yii 2.0.50
107107

108108
* Correcting the behavior for `JSON` column type in `MariaDb`.
109109

110+
Columns that are created as `JSON` now automatically add a Check constraint for `json_valid` to the according column.
111+
If Yii2 detects that a column has this `json_valid` constraint, data passed into or fetched from it is automatically serialized/deserialized.
112+
113+
While this does affect migrations created before the update when running them afterwards, it doesn't retroactively change tables created through migrations before the update!
114+
This means that running a migration script that invokes `yii\db\Migration::json()` for a MariaDb database will create a different database schema after the update than it did before.
115+
116+
To preserve the old behavior, migrations need to be changed to instead create a `LONGTEXT` column without a constraint:
117+
118+
```php
119+
<?php
120+
class m251103_091000_example_migration extends Migration {
121+
public function up() {
122+
// before
123+
$this->addColumn('MyTable', 'json_data', $this->json()->null()->after('other_column'));
124+
// after
125+
$this->addColumn('MyTable', 'json_data', "LONGTEXT NULL DEFAULT NULL COLLATE 'utf8mb4_bin' AFTER other_column");
126+
}
127+
}
128+
```
129+
130+
To make use of the new behavior, add the json_valid constraint to according columns on pre-existing databases
131+
and remove any JSON serialization/deserialization logic that was in place before this change (as (de)serialization is now done by Yii2).
132+
110133
Example usage of `JSON` column type in `db`:
111134

112135
```php

0 commit comments

Comments
 (0)