File: v21-clin-procedure-dynamic.sql

package info (click to toggle)
gnumed-server 22.19-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,148 kB
  • sloc: sql: 1,217,279; python: 15,659; sh: 1,582; makefile: 20
file content (54 lines) | stat: -rw-r--r-- 1,380 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- ==============================================================
-- GNUmed database schema change script
--
-- License: GPL v2 or later
-- Author: karsten.hilbert@gmx.net
--
-- ==============================================================
\set ON_ERROR_STOP 1
--set default_transaction_read_only to off;

-- --------------------------------------------------------------
update clin.procedure set
	is_ongoing = False
where
	is_ongoing is True
		and
	clin_end is not null
		and
	clin_end < (now() + '15 minutes'::interval)
;

-- --------------------------------------------------------------
alter table clin.procedure
	drop constraint if exists
		procedure_sane_ongoing
;

-- --------------------------------------------------------------
drop function if exists clin.trf_normalize_proc_is_ongoing() cascade;

create function clin.trf_normalize_proc_is_ongoing()
	returns trigger
	language 'plpgsql'
	as '
BEGIN
	if NEW.clin_end > clock_timestamp() then
		NEW.is_ongoing := TRUE;
	else
		NEW.is_ongoing := FALSE;
	end if;

	return NEW;
END;';


create trigger tr_normalize_proc_is_ongoing
	before update or insert on clin.procedure
	for each row
	when (NEW.clin_end IS NOT NULL)
	execute procedure clin.trf_normalize_proc_is_ongoing()
;

-- --------------------------------------------------------------
select gm.log_script_insertion('v21-clin-procedure-dynamic.sql', '21.0');