From 41d10d571cdb53c67ba7cf237c4ad92f46453ea0 Mon Sep 17 00:00:00 2001 From: Chenyu Zhang Date: Wed, 6 Jul 2022 17:02:03 +0800 Subject: [PATCH] fix: repair execution status when it inconsistent (#17128) Add migrations sql to repair the execution status when it does not consistent with task status. Closes: #17114 Signed-off-by: chlins --- .../postgresql/0082_2.5.3_schema.up.sql | 28 ++++++++++++++++++ .../postgresql/0090_2.6.0_schema.up.sql | 29 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 make/migrations/postgresql/0082_2.5.3_schema.up.sql diff --git a/make/migrations/postgresql/0082_2.5.3_schema.up.sql b/make/migrations/postgresql/0082_2.5.3_schema.up.sql new file mode 100644 index 000000000..c067aa321 --- /dev/null +++ b/make/migrations/postgresql/0082_2.5.3_schema.up.sql @@ -0,0 +1,28 @@ +/* repair execution status */ +DO $$ +DECLARE + exec RECORD; + status_group RECORD; + status_count int; + final_status varchar(32); +BEGIN + /* iterate all executions */ + FOR exec IN SELECT * FROM execution WHERE status='Running' + LOOP + /* identify incorrect execution status, group tasks belong it by status */ + status_count = 0; + final_status = ''; + FOR status_group IN SELECT status FROM task WHERE execution_id=exec.id GROUP BY status + /* loop here to ensure all the tasks belong to the execution are success */ + LOOP + status_count = status_count + 1; + final_status = status_group.status; + END LOOP; + /* update status and end_time when the tasks are all + success but itself status is not success */ + IF status_count=1 AND final_status='Success' THEN + UPDATE execution SET status='Success', revision=revision+1 WHERE id=exec.id; + UPDATE execution SET end_time=(SELECT MAX(end_time) FROM task WHERE execution_id=exec.id) WHERE id=exec.id; + END IF; + END LOOP; +END $$; \ No newline at end of file diff --git a/make/migrations/postgresql/0090_2.6.0_schema.up.sql b/make/migrations/postgresql/0090_2.6.0_schema.up.sql index a3b23dd66..e0dee146f 100644 --- a/make/migrations/postgresql/0090_2.6.0_schema.up.sql +++ b/make/migrations/postgresql/0090_2.6.0_schema.up.sql @@ -22,3 +22,32 @@ CREATE INDEX IF NOT EXISTS idx_artifact_repository_name ON artifact (repository_ CREATE INDEX IF NOT EXISTS idx_execution_vendor_type_vendor_id ON execution (vendor_type, vendor_id); CREATE INDEX IF NOT EXISTS idx_execution_start_time ON execution(start_time); CREATE INDEX IF NOT EXISTS idx_audit_log_project_id_optime ON audit_log (project_id, op_time); + +/* repair execution status */ +DO $$ +DECLARE + exec RECORD; + status_group RECORD; + status_count int; + final_status varchar(32); +BEGIN + /* iterate all executions */ + FOR exec IN SELECT * FROM execution WHERE status='Running' + LOOP + /* identify incorrect execution status, group tasks belong it by status */ + status_count = 0; + final_status = ''; + FOR status_group IN SELECT status FROM task WHERE execution_id=exec.id GROUP BY status + /* loop here to ensure all the tasks belong to the execution are success */ + LOOP + status_count = status_count + 1; + final_status = status_group.status; + END LOOP; + /* update status and end_time when the tasks are all + success but itself status is not success */ + IF status_count=1 AND final_status='Success' THEN + UPDATE execution SET status='Success', revision=revision+1 WHERE id=exec.id; + UPDATE execution SET end_time=(SELECT MAX(end_time) FROM task WHERE execution_id=exec.id) WHERE id=exec.id; + END IF; + END LOOP; +END $$; \ No newline at end of file