Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enh. #20171 - possible breaking changes: Support JSON columns for MariaDB 10.4 or higher. #20184

Closed
ldkafka opened this issue Jun 4, 2024 · 6 comments · Fixed by #20200
Closed
Labels
type:docs Documentation
Milestone

Comments

@ldkafka
Copy link

ldkafka commented Jun 4, 2024

If prior to Yii2 2.0.50 a MariaDB JSON field was used in a model, the contents needed manual json_encode before save (otherwise MySQL errored because of invalid JSON data).

Issue 1: Since this change, a manual json_encode on the field before assigning this to an AR attribute is basically double-encoding the data - data_type -> JSON string -> JSON.
Issue 2: If the JSON attribute used to have a 'string' validator in the model (which was the closest to no validator at all), now the validation fails if the original data type is not a string.

I realize these are not directly Yii issues, but they are Yii changes that break existing code behavior.

Short of testing if the attribute already is a valid JSON string before the encoding in the query builder, I'm not sure what the solution may be...

@manky
Copy link

manky commented Jun 6, 2024

The change also impact the yii\behaviors\AttributeTypecastBehavior on line 269 with the following error Array to string conversion

@terabytesoftw
Copy link
Member

terabytesoftw commented Jun 6, 2024

The JSON column behavior in MariaDB was wrong, it was corrected and now it works the same as MySQL, you can check it in the tests, can you explain to me how this affects AttributeTypeCastBehavior::class.

2.0.49.4 will also be released, with the security patches, if you do not need to change anything in your application you can use 2.0.49.4 when it is released, if you want to use 2.0.50, then you must update the code for the use of the JSON column in MariaDB.

It is certainly BC, but it is a necessary error correction, now all MySQL/MariaDB tests pass correctly.

Related: #18390

@ldkafka
Copy link
Author

ldkafka commented Jun 6, 2024

I don't thing we should revert the change - it is definitely a step in the right direction, however, it does break existing code. Maybe all it needs is a warning in big letters that this may break code...

@terabytesoftw
Copy link
Member

I don't thing we should revert the change - it is definitely a step in the right direction, however, it does break existing code. Maybe all it needs is a warning in big letters that this may break code...

Thanks for the suggestion, i'll add the note.

@manky
Copy link

manky commented Jun 6, 2024

The JSON column behavior in MariaDB was wrong, it was corrected and now it works the same as MySQL, you can check it in the tests, can you explain to me how this affects AttributeTypeCastBehavior::class.

2.0.49.4 will also be released, with the security patches, if you do not need to change anything in your application you can use 2.0.49.4 when it is released, if you want to use 2.0.50, then you must update the code for the use of the JSON column in MariaDB.

It is certainly BC, but it is a necessary error correction, now all MySQL/MariaDB tests pass correctly.

Related: #18390

I will do some more tests and will give you an update about the AttributeTypecastBehavior

@manky
Copy link

manky commented Jun 6, 2024

The problem is caused by the default validation rules in the model.
In the previous version of the Yii2, using GII, JSON fields in the database was validated by yii\validators\StringValidator, from version 2.0.50 they are validated by the yii\validators\SafeValidator.

Depending on the configuration of the AttributeTypecastBehavior, the value will be detected automatically based on validation rules.

So everyone needs to make changes on their models, before updating to v2.0.50.

@samdark samdark added the type:docs Documentation label Jun 6, 2024
@samdark samdark added this to the 2.0.50.1 milestone Jun 7, 2024
@terabytesoftw terabytesoftw linked a pull request Jun 12, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:docs Documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants