Browse Source

Save time series strategies: simplify SQL upgrade script

pull/12413/head
Dmytro Skarzhynets 2 years ago
parent
commit
996b8997fd
  1. 78
      application/src/main/data/upgrade/basic/schema_update.sql

78
application/src/main/data/upgrade/basic/schema_update.sql

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

Loading…
Cancel
Save