17baac57e425a3be3b520c2544e473ac07613488
[dcaegen2/platform.git] / oti / event-handler / create_schema.sql
1 -- ================================================================================
2 -- Copyright (c) 2019-2020 AT&T Intellectual Property. All rights reserved.
3 -- ================================================================================
4 -- Licensed under the Apache License, Version 2.0 (the "License");
5 -- you may not use this file except in compliance with the License.
6 -- You may obtain a copy of the License at
7 --
8 --      http://www.apache.org/licenses/LICENSE-2.0
9 --
10 -- Unless required by applicable law or agreed to in writing, software
11 -- distributed under the License is distributed on an "AS IS" BASIS,
12 -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 -- See the License for the specific language governing permissions and
14 -- limitations under the License.
15 -- ============LICENSE_END=========================================================
16
17 CREATE SCHEMA IF NOT EXISTS dti AUTHORIZATION dti_admin;
18
19 CREATE SEQUENCE IF NOT EXISTS dti.dtih_event_event_id_seq;
20
21 CREATE TABLE IF NOT EXISTS dti.dtih_event
22 (
23     dtih_event_id integer NOT NULL DEFAULT nextval('dti.dtih_event_event_id_seq'::regclass),
24     create_ts timestamp with time zone DEFAULT now(),
25     event jsonb,
26     target_name character varying(80),
27     target_type character varying(50),
28     last_modified_ts timestamp with time zone,
29     location_clli character varying(50),
30     CONSTRAINT dtih_event_pkey PRIMARY KEY (dtih_event_id)
31 );
32
33 CREATE UNIQUE INDEX IF NOT EXISTS "dtih_event_UK"
34     ON dti.dtih_event USING btree
35     (target_name, target_type)
36     TABLESPACE pg_default;
37
38 CREATE SEQUENCE IF NOT EXISTS dti.dtih_event_ack_event_ack_id_seq;
39
40 CREATE TABLE IF NOT EXISTS dti.dtih_event_ack
41 (
42     dtih_event_ack_id integer NOT NULL DEFAULT nextval('dti.dtih_event_ack_event_ack_id_seq'::regclass),
43     last_modified_ts timestamp with time zone DEFAULT now(),
44     k8s_cluster_fqdn character varying(80),
45     k8s_proxy_fqdn character varying(80),
46     k8s_pod_id character varying(80),
47     dtih_event_id integer,
48     k8s_namespace character varying(100),
49     k8s_service_name character varying(120),
50     k8s_service_port character varying(6),
51     create_ts timestamp with time zone,
52     action character varying(10),
53     service_component character varying(120),
54     deployment_id character varying(120),
55     container_type character varying(10),
56     docker_host character varying(120),
57     container_id character varying(120),
58     reconfig_script character varying(100),
59     CONSTRAINT dtih_event_ack_pkey PRIMARY KEY (dtih_event_ack_id),
60     CONSTRAINT event_ack_fk FOREIGN KEY (dtih_event_id)
61         REFERENCES dti.dtih_event (dtih_event_id) MATCH SIMPLE
62         ON UPDATE NO ACTION
63         ON DELETE RESTRICT
64 );
65
66 CREATE INDEX IF NOT EXISTS fki_dtih_event_ack_fk
67     ON dti.dtih_event_ack USING btree
68     (dtih_event_id)
69     TABLESPACE pg_default;
70
71 CREATE OR REPLACE FUNCTION dti.trigger_set_timestamp()
72 RETURNS TRIGGER AS $$
73 BEGIN
74   NEW.last_modified_ts = NOW();
75   RETURN NEW;
76 END;
77 $$ LANGUAGE plpgsql;
78
79 CREATE TRIGGER set_timestamp
80 BEFORE UPDATE ON dti.dtih_event_ack
81 FOR EACH ROW
82 EXECUTE PROCEDURE dti.trigger_set_timestamp();
83
84 CREATE TRIGGER set_timestamp_evt
85 BEFORE UPDATE ON dti.dtih_event
86 FOR EACH ROW
87 EXECUTE PROCEDURE dti.trigger_set_timestamp();