diff --git a/internal/pgengine/migration.go b/internal/pgengine/migration.go index bb382b85..7b93c8d9 100644 --- a/internal/pgengine/migration.go +++ b/internal/pgengine/migration.go @@ -141,6 +141,12 @@ var Migrations func() migrator.Option = func() migrator.Option { return ExecuteMigrationScript(ctx, tx, "00629.sql") }, }, + &migrator.Migration{ + Name: "00721 Add more job control functions", + Func: func(ctx context.Context, tx pgx.Tx) error { + return ExecuteMigrationScript(ctx, tx, "00721.sql") + }, + }, // adding new migration here, update "timetable"."migration" in "sql/init.sql" // and "dbapi" variable in main.go! diff --git a/internal/pgengine/sql/job_functions.sql b/internal/pgengine/sql/job_functions.sql index b8fa821e..90acc325 100644 --- a/internal/pgengine/sql/job_functions.sql +++ b/internal/pgengine/sql/job_functions.sql @@ -125,6 +125,14 @@ $$ LANGUAGE SQL; COMMENT ON FUNCTION timetable.move_task_down IS 'Switch the order of the task execution with a following task within the chain'; +-- delete_task() will delete the task from a chain +CREATE OR REPLACE FUNCTION timetable.delete_task(IN task_id BIGINT) RETURNS boolean AS $$ + WITH del_task AS (DELETE FROM timetable.task WHERE task_id = $1 RETURNING task_id) + SELECT EXISTS(SELECT 1 FROM del_task) +$$ LANGUAGE SQL; + +COMMENT ON FUNCTION timetable.delete_task IS 'Delete the task from a chain'; + -- delete_job() will delete the chain and its tasks from the system CREATE OR REPLACE FUNCTION timetable.delete_job(IN job_name TEXT) RETURNS boolean AS $$ WITH del_chain AS (DELETE FROM timetable.chain WHERE chain.chain_name = $1 RETURNING chain_id) @@ -133,10 +141,18 @@ $$ LANGUAGE SQL; COMMENT ON FUNCTION timetable.delete_job IS 'Delete the chain and its tasks from the system'; --- delete_task() will delete the task from a chain -CREATE OR REPLACE FUNCTION timetable.delete_task(IN task_id BIGINT) RETURNS boolean AS $$ - WITH del_task AS (DELETE FROM timetable.task WHERE task_id = $1 RETURNING task_id) - SELECT EXISTS(SELECT 1 FROM del_task) +-- pause_job() will pause the chain (set live = false) +CREATE OR REPLACE FUNCTION timetable.pause_job(IN job_name TEXT) RETURNS boolean AS $$ + WITH upd_chain AS (UPDATE timetable.chain SET live = false WHERE chain.chain_name = $1 RETURNING chain_id) + SELECT EXISTS(SELECT 1 FROM upd_chain) +$$ LANGUAGE SQL; + +COMMENT ON FUNCTION timetable.pause_job IS 'Pause the chain (set live = false)'; + +-- resume_job() will resume the chain (set live = true) +CREATE OR REPLACE FUNCTION timetable.resume_job(IN job_name TEXT) RETURNS boolean AS $$ + WITH upd_chain AS (UPDATE timetable.chain SET live = true WHERE chain.chain_name = $1 RETURNING chain_id) + SELECT EXISTS(SELECT 1 FROM upd_chain) $$ LANGUAGE SQL; -COMMENT ON FUNCTION timetable.delete_task IS 'Delete the task from a chain'; \ No newline at end of file +COMMENT ON FUNCTION timetable.resume_job IS 'Resume the chain (set live = true)'; diff --git a/internal/pgengine/sql/migrations/00721.sql b/internal/pgengine/sql/migrations/00721.sql new file mode 100644 index 00000000..9883b97b --- /dev/null +++ b/internal/pgengine/sql/migrations/00721.sql @@ -0,0 +1,15 @@ +-- pause_job() will pause the chain (set live = false) +CREATE OR REPLACE FUNCTION timetable.pause_job(IN job_name TEXT) RETURNS boolean AS $$ + WITH upd_chain AS (UPDATE timetable.chain SET live = false WHERE chain.chain_name = $1 RETURNING chain_id) + SELECT EXISTS(SELECT 1 FROM upd_chain) +$$ LANGUAGE SQL; + +COMMENT ON FUNCTION timetable.pause_job IS 'Pause the chain (set live = false)'; + +-- resume_job() will resume the chain (set live = true) +CREATE OR REPLACE FUNCTION timetable.resume_job(IN job_name TEXT) RETURNS boolean AS $$ + WITH upd_chain AS (UPDATE timetable.chain SET live = true WHERE chain.chain_name = $1 RETURNING chain_id) + SELECT EXISTS(SELECT 1 FROM upd_chain) +$$ LANGUAGE SQL; + +COMMENT ON FUNCTION timetable.resume_job IS 'Resume the chain (set live = true)'; \ No newline at end of file