-- Ensure all graphs have standard config keys

-- PostgreSQL
UPDATE graphs
SET config = jsonb_set(
    jsonb_set(
        jsonb_set(COALESCE(config, '{}'::jsonb), '{groupBy}', COALESCE(config->'groupBy','""'::jsonb)),
        '{aggregateFunc}', COALESCE(config->'aggregateFunc','""'::jsonb)),
    '{timeUnit}', COALESCE(config->'timeUnit','""'::jsonb));

-- MySQL
UPDATE graphs
SET config = JSON_SET(
    JSON_SET(
        JSON_SET(IFNULL(config, JSON_OBJECT()),
                 '$.groupBy', IFNULL(JSON_EXTRACT(config,'$.groupBy'), JSON_QUOTE(''))),
        '$.aggregateFunc', IFNULL(JSON_EXTRACT(config,'$.aggregateFunc'), JSON_QUOTE(''))),
    '$.timeUnit', IFNULL(JSON_EXTRACT(config,'$.timeUnit'), JSON_QUOTE('')));
