Save time series strategies: simplify SQL upgrade script

This commit is contained in:
Dmytro Skarzhynets 2025-01-21 11:14:22 +02:00
parent 71d43f3af2
commit 996b8997fd

View File

@ -221,60 +221,36 @@ DO $$
WHERE table_name = 'rule_node' WHERE table_name = 'rule_node'
) THEN ) THEN
-- CREATE JSON validation function UPDATE rule_node
CREATE OR REPLACE FUNCTION is_valid_jsonb(input text) SET configuration = (
RETURNS boolean (configuration::jsonb - 'skipLatestPersistence')
LANGUAGE plpgsql || jsonb_build_object(
AS $func$ 'persistenceSettings', jsonb_build_object(
DECLARE 'type', 'ADVANCED',
dummy JSONB; 'timeseries', jsonb_build_object('type', 'ON_EVERY_MESSAGE'),
BEGIN 'latest', jsonb_build_object('type', 'SKIP'),
dummy := input::jsonb; 'webSockets', jsonb_build_object('type', 'ON_EVERY_MESSAGE')
RETURN true; )
EXCEPTION )
WHEN others THEN )::text,
RETURN false; configuration_version = 1
END; WHERE type = 'org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode'
$func$; AND configuration_version = 0
AND configuration::jsonb ->> 'skipLatestPersistence' = 'true';
UPDATE rule_node UPDATE rule_node
SET configuration = CASE SET configuration = (
-- Case 1: If configuration is NULL, invalid JSON, or not a JSON object - set default configuration (configuration::jsonb - 'skipLatestPersistence')
WHEN configuration IS NULL || jsonb_build_object(
OR NOT is_valid_jsonb(configuration) 'persistenceSettings', jsonb_build_object(
OR jsonb_typeof(configuration::jsonb) <> 'object' 'type', 'ON_EVERY_MESSAGE'
THEN jsonb_build_object( )
'defaultTTL', 0, )
'useServerTs', false, )::text,
'persistenceSettings', jsonb_build_object('type', 'ON_EVERY_MESSAGE')
)
-- Case 2: If a valid JSON object with persistenceSettings (rule node was already upgraded) - leave unchanged
WHEN configuration::jsonb ? 'persistenceSettings'
THEN configuration::jsonb
-- Case 3: If a valid JSON object without persistenceSettings and skipLatestPersistence = 'true' (string 'true' or boolean true) - set latest to SKIP
WHEN configuration::jsonb ->> 'skipLatestPersistence' = 'true'
THEN (configuration::jsonb - 'skipLatestPersistence')
|| jsonb_build_object(
'persistenceSettings', jsonb_build_object(
'type', 'ADVANCED',
'timeseries', jsonb_build_object('type', 'ON_EVERY_MESSAGE'),
'latest', jsonb_build_object('type', 'SKIP'),
'webSockets', jsonb_build_object('type', 'ON_EVERY_MESSAGE')
)
)
-- Case 4: If a valid JSON object without persistenceSettings and skipLatestPersistence not 'true' (everything else) - set all to ON_EVERY_MESSAGE
ELSE (configuration::jsonb - 'skipLatestPersistence')
|| jsonb_build_object(
'persistenceSettings', jsonb_build_object(
'type', 'ON_EVERY_MESSAGE'
)
)
END::text,
configuration_version = 1 configuration_version = 1
WHERE type = 'org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode' AND configuration_version = 0; WHERE type = 'org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode'
AND configuration_version = 0
-- Drop the helper function AND (configuration::jsonb ->> 'skipLatestPersistence' != 'true' OR configuration::jsonb ->> 'skipLatestPersistence' IS NULL);
DROP FUNCTION is_valid_jsonb(text);
END IF; END IF;
END; END;