Skip to content

Commit a2d146b

Browse files
authored
feat: add last pipeline report on scm table (#375)
Signed-off-by: Olivier Vernin <olivier@vernin.me>
1 parent eb6ccfc commit a2d146b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
BEGIN;
2+
DROP TRIGGER IF EXISTS trg_sync_scms_last_pipeline_report_at ON pipelineReports;
3+
DROP FUNCTION IF EXISTS sync_scms_last_pipeline_report_at();
4+
5+
ALTER TABLE scms
6+
DROP COLUMN IF EXISTS last_pipeline_report_at;
7+
COMMIT;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- This migration adds a timestamp column to the scms table and keeps it
2+
-- synchronized with the latest pipeline report creation time for linked SCMs.
3+
BEGIN;
4+
ALTER TABLE scms
5+
ADD COLUMN IF NOT EXISTS last_pipeline_report_at TIMESTAMP;
6+
7+
CREATE OR REPLACE FUNCTION sync_scms_last_pipeline_report_at()
8+
RETURNS TRIGGER AS $$
9+
BEGIN
10+
UPDATE scms
11+
SET last_pipeline_report_at = NEW.created_at
12+
WHERE id = ANY(NEW.target_db_scm_ids);
13+
14+
RETURN NEW;
15+
END;
16+
$$ LANGUAGE plpgsql;
17+
18+
DROP TRIGGER IF EXISTS trg_sync_scms_last_pipeline_report_at ON pipelineReports;
19+
CREATE TRIGGER trg_sync_scms_last_pipeline_report_at
20+
AFTER INSERT ON pipelineReports
21+
FOR EACH ROW
22+
WHEN (array_length(NEW.target_db_scm_ids, 1) IS NOT NULL)
23+
EXECUTE FUNCTION sync_scms_last_pipeline_report_at();
24+
COMMIT;

0 commit comments

Comments
 (0)