File: v15-clin-procedure-dynamic.sql

package info (click to toggle)
gnumed-server 19.12-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 43,908 kB
  • ctags: 3,690
  • sloc: sql: 1,200,732; python: 12,836; sh: 1,145; makefile: 19
file content (110 lines) | stat: -rw-r--r-- 2,190 bytes parent folder | download | duplicates (7)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
-- ==============================================================
-- GNUmed database schema change script
--
-- License: GPL v2 or later
-- Author: karsten.hilbert@gmx.net
-- 
-- ==============================================================
\set ON_ERROR_STOP 1

-- --------------------------------------------------------------
-- .soap_cat

-- fix NULL values
update clin.procedure set
	soap_cat = 'p'::text
where
	soap_cat is NULL;


alter table clin.procedure
	alter column soap_cat
		set default 'p'::text;



-- .end
comment on column clin.procedure.clin_end is
'When did this procedure end/is expected to end.

- NULL if unknown or .clin_when (=start) is sufficient (eg. insignificant duration)';


\unset ON_ERROR_STOP
alter table clin.procedure drop constraint procedure_sane_end cascade;
\set ON_ERROR_STOP 1


alter table clin.procedure
	add constraint procedure_sane_end check (
		(clin_end is NULL)
			OR
		(clin_end >= clin_when)
	);



-- .is_ongoing
comment on column clin.procedure.is_ongoing is
'Whether this procedure is still going on (such as desensibilisation, chemotherapy, etc).';


alter table clin.procedure
	alter column is_ongoing
		set default false;

update clin.procedure set
	is_ongoing = DEFAULT
where
	is_ongoing is NULL
;


alter table clin.procedure
	alter column is_ongoing
		set not null;


alter table clin.procedure
	add constraint procedure_sane_ongoing check (
		(is_ongoing is FALSE)
			OR
		(
			(clin_end is NULL)
				OR
			(clin_end > now())
		)
	);



\unset ON_ERROR_STOP
drop function clin.trf_normalize_proc_is_ongoing() cascade;
\set ON_ERROR_STOP 1

create function clin.trf_normalize_proc_is_ongoing()
	returns trigger
	language 'plpgsql'
	as '
BEGIN
	if NEW.clin_end is NULL then
		return NEW;
	end if;

	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 on clin.procedure
	for each row execute procedure clin.trf_normalize_proc_is_ongoing()
;

-- --------------------------------------------------------------
select gm.log_script_insertion('v15-clin-procedure-dynamic.sql', 'Revision: 1.1');