File: v16-clin-lnk_code2narrative-dynamic.sql

package info (click to toggle)
gnumed-server 22.31-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 46,268 kB
  • sloc: sql: 1,217,633; python: 15,878; sh: 1,590; makefile: 20
file content (78 lines) | stat: -rw-r--r-- 2,490 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
-- ==============================================================
-- GNUmed database schema change script
--
-- License: GPL v2 or later
-- Author: karsten.hilbert@gmx.net
-- 
-- ==============================================================
\set ON_ERROR_STOP 1

-- --------------------------------------------------------------
comment on table clin.lnk_code2narrative is
'Links codes to SOAP narrative.';


select gm.register_notifying_table('clin', 'lnk_code2narrative', 'narrative_code');
select audit.register_table_for_auditing('clin', 'lnk_code2narrative');


grant select on clin.lnk_code2narrative to group "gm-public";
grant insert, update, delete on clin.lnk_code2narrative to group "gm-doctors";
grant usage on clin.lnk_code2narrative_pk_seq to group "gm-doctors";

\unset ON_ERROR_STOP
alter table clin.lnk_code2narrative drop constraint clin_lc2narr_code_uniq_per_item cascade;
\set ON_ERROR_STOP 1

alter table clin.lnk_code2narrative
	add constraint clin_lc2narr_code_uniq_per_item
		unique(fk_generic_code, fk_item);

-- --------------------------------------------------------------
-- .fk_item
comment on column clin.lnk_code2narrative.fk_item is
'Foreign key to clin.clin_narrative';


\unset ON_ERROR_STOP
alter table clin.lnk_code2narrative drop constraint lnk_code2narrative_fk_item_fkey cascade;
\set ON_ERROR_STOP 1


alter table clin.lnk_code2narrative
	add foreign key (fk_item)
		references clin.clin_narrative(pk)
		on update cascade				-- update if narrative is updated
		on delete cascade;				-- delete if narrative is deleted


\unset ON_ERROR_STOP
drop index idx_c_lc2narr_fk_item cascade;
\set ON_ERROR_STOP 1

create index idx_c_lc2narr_fk_item on clin.lnk_code2narrative(fk_item);

-- --------------------------------------------------------------
-- .fk_generic_code
comment on column clin.lnk_code2narrative.fk_generic_code is
'Custom foreign key to ref.coding_system_root.';


alter table clin.lnk_code2narrative
	alter column fk_generic_code
		set not null;


-- INSERT
create trigger tr_ins_lc2sth_fk_generic_code
	before insert on clin.lnk_code2narrative
		for each row execute procedure clin.trf_ins_lc2sth_fk_generic_code();


-- UPDATE
create trigger tr_upd_lc2sth_fk_generic_code
	before update on clin.lnk_code2narrative
		for each row execute procedure clin.trf_upd_lc2sth_fk_generic_code();

-- --------------------------------------------------------------
select gm.log_script_insertion('v16-clin-lnk_code2narrative-dynamic.sql', '1.0');