Say I have a model Test
using JSONB, with a column data
In the database I have data equal to: { "key_1": 1, "key_2": { "foo": "bar" }
Now say there are two employees working on info that updates one of the two keys:
employee_a submits an update that sets { "key_1": 2 }
At the same time, employee_b submits an update that sets {"key_2": { "bar": "foo" }
Now, ideally, what the data
column would hold is: { "key_1": 2, "key_2": { "bar": "foo" }
However, because this is all saved with serializers, what is going to be set in the database will depend entirely on which update was submitted last
How would I start creating a patch to use postgresql’s native functions to update specific keys in a json store? What I was hoping to create is functionality within the update
process, as well as within the attributes API, to detect specifically what keys (including nested) have been changed, and generate the proper update query to only modify those keys
i.e.
employee_a causes the following query: UPDATE tests SET data = jsonb_set(data, '{key_1}', '2', true)
and
employee_b causes the following query: UPDATE tests SET data = jsonb_set(data #- '{key_2,foo}', '{key_2,bar}', '"foo"', true)