@@ -338,7 +338,7 @@ LANGUAGE plpgsql set search_path FROM CURRENT;
338338CREATE FUNCTION _possible_args () RETURNS jsonbAS
339339$BODY$
340340BEGIN
341- RETURNjson_build_object (
341+ RETURNjsonb_build_object (
342342' node' ,' node name (default: master)' ,
343343' name' ,' job name' ,
344344' comments' ,' some comments on job' ,
@@ -396,34 +396,43 @@ END
396396$BODY$
397397LANGUAGE plpgsqlset search_pathFROM CURRENT;
398398
399- CREATE FUNCTION _get_cron_from_attrs (params jsonb) RETURNS jsonbAS
399+ CREATE FUNCTION _get_cron_from_attrs (params jsonb, prev jsonb ) RETURNS jsonbAS
400400$BODY$
401401DECLARE
402402datestext [];
403403cron jsonb;
404+ rule jsonb;
404405clean_cron jsonb;
405406Ninteger ;
406407nametext ;
408+ updatePrevboolean ;
407409BEGIN
408410
411+ updatePrev := true;
412+
413+ IF NOT params?' cron' AND NOT params?' rule' AND NOT params?' date' AND NOT params?' dates' THEN
414+ RAISE EXCEPTION' There is no information about job' ' s schedule'
415+ USING HINT= ' Use' ' cron' ' - cron string,' ' rule' ' - json to set schedule rules or' ' date' ' and' ' dates' ' to set exact date(s)' ;
416+ END IF;
417+
409418IF params?' cron' THEN
410419EXECUTE' SELECT cron2jsontext($1::cstring)::jsonb'
411420INTO cron
412421USING params- >> ' cron' ;
413- ELSIF params?' rule' THEN
414- cron := params- > ' rule' ;
415- ELSIF NOT params?' date' AND NOT params?' dates' THEN
416- RAISE EXCEPTION' There is no information about job' ' s schedule'
417- USING HINT= ' Use' ' cron' ' - cron string,' ' rule' ' - json to set schedule rules or' ' date' ' and' ' dates' ' to set exact date(s)' ;
418422END IF;
419423
420- IF cronIS NOT NULL THEN
421- IF cron?' date' THEN
422- dates := _get_array_from_jsonb(dates, cron- > ' date' );
423- END IF;
424- IF cron?' dates' THEN
425- dates := _get_array_from_jsonb(dates, cron- > ' dates' );
426- END IF;
424+ IF params?' rule' THEN
425+ rule := params- > ' rule' ;
426+ updatePrev := false;
427+ END IF;
428+
429+ cron := coalesce(cron,' {}' ::jsonb)|| coalesce(rule,' {}' ::jsonb);
430+
431+ IF cron?' date' THEN
432+ dates := _get_array_from_jsonb(dates, cron- > ' date' );
433+ END IF;
434+ IF cron?' dates' THEN
435+ dates := _get_array_from_jsonb(dates, cron- > ' dates' );
427436END IF;
428437
429438IF params?' date' THEN
@@ -437,9 +446,13 @@ BEGIN
437446IF N> 0 THEN
438447EXECUTE' SELECT array_agg(lll) FROM (SELECT distinct(date_trunc(' ' min' ' , unnest::timestamp with time zone)) as lll FROM unnest($1) ORDER BY date_trunc(' ' min' ' , unnest::timestamp with time zone)) as Z'
439448INTO dates USING dates;
440- cron := COALESCE( cron, ' {} ' ::jsonb) || json_build_object (' dates' , array_to_json(dates))::jsonb ;
449+ cron := cron|| jsonb_build_object (' dates' , array_to_json(dates));
441450END IF;
442-
451+
452+ IF updatePrevAND prevIS NOT NULL THEN
453+ cron := prev|| cron;
454+ END IF;
455+
443456clean_cron := ' {}' ::jsonb;
444457FOR nameIN SELECT * FROM unnest(' {dates, crontab, onstart, days, hours, wdays, months, minutes}' ::text [])
445458LOOP
@@ -564,7 +577,7 @@ BEGIN
564577 RAISE WARNING' You used excess keys in params: %.' , array_to_string(excess,' ,' );
565578 END IF;
566579
567- cron := _get_cron_from_attrs(params);
580+ cron := _get_cron_from_attrs(params, NULL );
568581 commands := _get_commands_from_attrs(params);
569582 executor := _get_executor_from_attrs(params);
570583 node := ' master' ;
@@ -635,7 +648,7 @@ LANGUAGE plpgsql
635648CREATE FUNCTION create_job (crontext , commandtext , nodetext DEFAULTNULL ) RETURNSinteger AS
636649$BODY$
637650BEGIN
638- RETURN create_job(json_build_object (' cron' , cron,' command' , command,' node' , node)::jsonb );
651+ RETURN create_job(jsonb_build_object (' cron' , cron,' command' , command,' node' , node));
639652END
640653$BODY$
641654LANGUAGE plpgsql
@@ -644,7 +657,7 @@ LANGUAGE plpgsql
644657CREATE FUNCTION create_job (dttimestamp with time zone , commandtext , nodetext DEFAULTNULL ) RETURNSinteger AS
645658$BODY$
646659BEGIN
647- RETURN create_job(json_build_object (' date' , dt::text ,' command' , command,' node' , node)::jsonb );
660+ RETURN create_job(jsonb_build_object (' date' , dt::text ,' command' , command,' node' , node));
648661END
649662$BODY$
650663LANGUAGE plpgsql
@@ -653,7 +666,7 @@ LANGUAGE plpgsql
653666CREATE FUNCTION create_job (dtstimestamp with time zone [], commandtext , nodetext DEFAULTNULL ) RETURNSinteger AS
654667$BODY$
655668BEGIN
656- RETURN create_job(json_build_object (' dates' , array_to_json(dts),' command' , command,' node' , node)::jsonb );
669+ RETURN create_job(jsonb_build_object (' dates' , array_to_json(dts),' command' , command,' node' , node));
657670END
658671$BODY$
659672LANGUAGE plpgsql
@@ -662,7 +675,7 @@ LANGUAGE plpgsql
662675CREATE FUNCTION create_job (crontext , commandstext [], nodetext DEFAULTNULL ) RETURNSinteger AS
663676$BODY$
664677BEGIN
665- RETURN create_job(json_build_object (' cron' , cron,' commands' , array_to_json(commands),' node' , node)::jsonb );
678+ RETURN create_job(jsonb_build_object (' cron' , cron,' commands' , array_to_json(commands),' node' , node));
666679END
667680$BODY$
668681LANGUAGE plpgsql
@@ -671,7 +684,7 @@ LANGUAGE plpgsql
671684CREATE FUNCTION create_job (dttimestamp with time zone , commandstext [], nodetext DEFAULTNULL ) RETURNSinteger AS
672685$BODY$
673686BEGIN
674- RETURN create_job(json_build_object (' date' , dt::text ,' commands' , array_to_json(commands),' node' , node)::jsonb );
687+ RETURN create_job(jsonb_build_object (' date' , dt::text ,' commands' , array_to_json(commands),' node' , node));
675688END
676689$BODY$
677690LANGUAGE plpgsql
@@ -680,7 +693,7 @@ LANGUAGE plpgsql
680693CREATE FUNCTION create_job (dtstimestamp with time zone [], commandstext [], nodetext DEFAULTNULL ) RETURNSinteger AS
681694$BODY$
682695BEGIN
683- RETURN create_job(json_build_object (' dates' , array_to_json(dts),' commands' , array_to_json(commands),' node' , node)::jsonb );
696+ RETURN create_job(jsonb_build_object (' dates' , array_to_json(dts),' commands' , array_to_json(commands),' node' , node));
684697END
685698$BODY$
686699LANGUAGE plpgsql
@@ -711,7 +724,7 @@ BEGIN
711724
712725 IF attrs?' cron' OR attrs?' date' OR attrs?' dates' OR attrs?' rule' THEN
713726 cmd := cmd|| ' rule =' ||
714- quote_literal(_get_cron_from_attrs(attrs))|| ' ::jsonb,' ;
727+ quote_literal(_get_cron_from_attrs(attrs, job . rule ))|| ' ::jsonb,' ;
715728 END IF;
716729
717730 IF attrs?' command' OR attrs?' commands' THEN
@@ -796,14 +809,27 @@ $BODY$
796809LANGUAGE plpgsql
797810 SECURITY DEFINERset search_pathFROM CURRENT;
798811
812+ CREATE FUNCTION set_job_attribute (jobIdinteger , nametext , value jsonb) RETURNSboolean AS
813+ $BODY$
814+ BEGIN
815+ IF name<> ' rule' THEN
816+ RAISE EXCEPTION' key % cannot have a jsonb value. Only "rule" allowed' , name;
817+ END IF;
818+
819+ RETURN set_job_attributes(jobId, jsonb_build_object(name, value));
820+ END
821+ $BODY$
822+ LANGUAGE plpgsql
823+ SECURITY DEFINERset search_pathFROM CURRENT;
824+
799825CREATE FUNCTION set_job_attribute (jobIdinteger , nametext , value anyarray) RETURNSboolean AS
800826$BODY$
801827BEGIN
802828 IF name<> ' dates' AND name<> ' commands' THEN
803829 RAISE EXCEPTION' key % cannot have an array value. Only dates, commands allowed' , name;
804830 END IF;
805831
806- RETURN set_job_attributes(jobId,json_build_object (name, array_to_json(value))::jsonb );
832+ RETURN set_job_attributes(jobId,jsonb_build_object (name, array_to_json(value)));
807833END
808834$BODY$
809835LANGUAGE plpgsql
@@ -815,9 +841,11 @@ DECLARE
815841 attrs jsonb;
816842BEGIN
817843 IF name= ' dates' OR name= ' commands' THEN
818- attrs := json_build_object(name, array_to_json(value::text []));
844+ attrs := jsonb_build_object(name, array_to_json(value::text []));
845+ ELSIF name= ' rule' THEN
846+ attrs := jsonb_build_object(' rule' , value::jsonb);
819847 ELSE
820- attrs := json_build_object (name, value);
848+ attrs := jsonb_build_object (name, value);
821849 END IF;
822850 RETURN set_job_attributes(jobId, attrs);
823851END