-- Update incident forms to store custom fields in form_data

UPDATE forms
SET elements = (
    SELECT jsonb_agg(
        CASE
            WHEN e->>'data_column' IN ('date','time','classification','location','investigator_id','status')
                THEN e
            ELSE (e - 'data_column') || jsonb_build_object('json_column','form_data')
        END ORDER BY ord
    )
    FROM jsonb_array_elements(elements) WITH ORDINALITY AS t(e, ord)
)
WHERE id BETWEEN 13 AND 20;
