I have a table which has a column named "data" of type JSONB. I am trying to create a migration script based on the recent model changes. (postgres 9.4)
"gstNotRegisteredReason","gstRegistered","isIndiaGSTComposition" in old model needs to be changed as "vatExemptionReason","vatExemption","auxiliaryProperties" in the new model.
Old Model:
{
"requestId":531956,
"requestVersion":5,
"supplier":{
"taxClassificationId":null,
"supplierScid":null,
"gstNotRegisteredReason":null, (boolean)
"gstRegistered":null, (boolean)
"isIndiaGSTComposition":true (boolean)
},
"createTime":"2017-07-17T06:48:52",
}
New Model:
{
"requestId":531956,
"requestVersion":5,
"supplier":{
"taxClassificationId":null,
"supplierScid":null,
"vatExemptionReason":null, (boolean)
"vatExemption":null, (boolean)
"AuxiliaryProperties":[
{
"id":"indiaGSTComposition",
"booleanValue":true
}
]
},
"createTime":"2017-07-17T06:48:52",
}
I came up with this query
update requests set data=regexp_replace(data::text, '"gstRegistered": ', '"vatExemption":', 'g')::jsonb;
update requests set data=regexp_replace(data::text, '"gstNotRegisteredReason": ', '"vatExemptionReason":', 'g')::jsonb;
But i am not sure how to convert isIndiaGSTComposition(boolean) to auxiliaryProperties(Array).
Please note that, if isIndiaGSTComposition is null, it should be auxiliaryProperties : null. But if it has true or false, it needs to be in the above data structure format(like in "new model" example above).
Any help is appreciated. Thanks in advance !