Skip to content

Conversation

@iamgergo
Copy link
Contributor

@iamgergo iamgergo commented Feb 15, 2020

This is a retry of #31472, but using json_patch instead of json_set.


  • The currently compiled SQL is invalid when trying to update nested JSON fields (details in the previous PR).
  • SQLite is a very nice solution when testing small packages or very light applications that may still use JSON columns. In those cases using SQLite for testing purposes is a huge time saver.
  • No one expects a robust JSON support for SQLite, but for smaller tests, this can serve perfectly and no need to set up a MySQL or a PostgreSQL database. So, it might be a good idea, supporting JSON column updates in SQLite's Grammar (especially, since there are native functions we can use, this is not a regex replacer or something like that).

By grouping the nested JSON updates (selectors using ->), we can use a single json_patch to update the JSON column. Also, filtering JSON selectors, prevents generating invalid SQL.

The json_patch will merge the grouped attributes to the currently stored JSON in the database, so it updates only the values we want to update. Also, this approach guards the parameter order.

An Eloquent update query like the following will update the desired columns properly:

Customer::whereIn('id', [1, 2, 3])->update([
    'meta->size' => 'L',
    'options->security' => ['2fa'],
    'group_id' => 30,
    'meta->color' => 'Yellow',
]);

This PR contains no beaking change as far as I know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants